How to print an array after inserting an element?

+1 vote
asked Jun 7 by Rahul Krishnan Tr (130 points)

1 Answer

+1 vote
answered Jun 7 by xDELLx (2,950 points)
assuming the query is for c/C++

In general to print elements of an array , its necessary to know about the maximum elements the array can hold.If this value(N , no of elements array can hold) is known,the below will help.

for (int i=0;i<N;i++)  printf("%d",a[i]); //assuming array ,a, of int s

Make sure the array has enough space to hold newly inserted elements,else it causes seg fault.

Also using std::vector (from c++) will save a lot of efforts of us implementing the above validations.

Now ,my assumption about the question :- "is print valid elements of an array after insertion sequentially "

So lets say invlaid elememts are always set -1

array always inserts at offset "valid_position"

so implementation below:

int a[100];

int valid_position=0;

for (int i=0;i<100;i++) a[i]=-1;

//Simple logic wjich willinsert first 10 elements,

for (int i=0;i<10;i++){




//Insert 1 element as question asks


//print array after inserting an element

for (int i=0;i<valid_position;i++){

printf("%d ",a[i]);

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.