Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to print an array after inserting an element?

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

1 Answer

+1 vote
answered Jun 7, 2020 by xDELLx (10,500 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++){

a[valid_position]=i;

valid_position++;

}

//Insert 1 element as question asks

a[valid_position]=123;

valid_position++;
//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.
...