write a program to insert an element into an array
Answer Posted / meena
/* Program to insert element into array */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[50],i,n,pos,num;
clrscr();
printf("How Many Elements:-");
scanf("%d",&n);
printf("\n\tEnter Elements into Array");
for(i=0;i<n;i++)
{
scanf("\t%d",&arr[i]);
}
printf("\nArray Elements before inserting");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
printf("\nEnter the position");
scanf("%d",&pos);
if(pos==0 || pos>=n)
{
printf("Invalid position");
}
else
{
printf("\nEnter the number you want to insert into an
array");
scanf("%d",&num);
/*shift the existing elements*/
for(i=n;i>=pos;i--)
arr[i]=arr[i-1];
arr[pos-1]=num;
printf("\nArray elements after insertion");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
}
getch();
}
| Is This Answer Correct ? | 57 Yes | 17 No |
Post New Answer View All Answers
How does a C++ structure differ from a C++ class?
Explain 'this' pointer and what would happen if a pointer is deleted twice?
What is the oldest programming language?
What are the advantages of using a pointer?
Assume studentNames and studentIDs are two parallel arrays of size N that hold student data. Write a pseudocode algorithm that sorts studentIDs array in ascending ID number order such that the two arrays remain parallel.
Explain one-definition rule (odr).
What is null pointer and void pointer?
What is difference between c++ 11 and c++ 14?
Do you need a main function in c++?
What is class syntax c++?
What is the type of 'this' pointer?
What does h mean in maths?
How do you differentiate between overloading the prefix and postfix increments?
What is diamond problem in c++?
Write a program using shift_half( ) function to shift the elements of first half array to second half and vice versa.