answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],i,j,n;

clrscr();

printf("Enter the no of element you want to insert in the array : ");

scanf("%d",&n);

printf(" Enter the Array element : ");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("The array is : ");

for(i=0;i<n;i++)

{

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

}

printf(" Enter the element which you want to insert");

scanf("%d",&j);

printf("The array is : ");

for(i=0;i<n+1;i++)

{

if(i==n)

a[i]=j;

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

}

getch();

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

#include<iostream.h> #include<conio.h> void main() { clrscr(); int stack[100],loc,top,item,max=100; cout<<"\n------ Stack Insertion using Array ------"; cout<<"\n\nEnter Value of Stack Top : "; cin>>top; if(top>=max) { cout<<"\nStack is Full"; getch(); return; } cout<<"\nEnter Elements in Stack :\n"; for(loc=1;loc<=top;loc++) { cin>>stack[loc]; } ins: cout<<"\nEnter Item you want to Insert : "; cin>>item; top=top+1; //Increment the Top stack[top]=item; //Insert Element cout<<"\nStack After Insertion :\n"; for(loc=1;loc<=top;loc++) { cout<<stack[loc]<<endl; } cout<<"\nTop : "<<top; cout<<"\n\nItem "<<item<<" is Inserted at Top\n\n"; cout<<"\n\nWant to Insert more elements ?????\n\n"; cout<<" -----> Press 1 to Continue\n"; cout<<" -----> Press any Key to Exit\n"; char choice; cin>>choice; if(choice=='1' && top<max) { cout<<endl<<endl; goto ins; } else if(choice=='1' && top>=max) { cout<<"\nStack is Full"; getch(); return; } else { return; } }

Output :-

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In C, you have to move elements to make pace:

Insert (int into[], int at, int *used, int value)
{
if (at>*used) exit (33);
if (at<*used) memmove (&into[at+1], &into[at], (*used-at)*sizeof(into[0])));
into[at]= value;
++*used;


}

int main (void)
{
int a[32];
int an;
int i;

an= 0;
Insert (a, 0, &an, 3);
Insert (a, 0, &an, 1);
Insert (a, 1, &an, 2);
for (i=0; iprintf ("\n");
return 0;


}
...........................................................................................................................................
Rjames007:-
Hi I read the program and it's short so it is good but there is an another method it looks larger but it is so simple , here we go:


#include
#include
int a[30],i,pos,num,len;
void main()
{
void insert(int a[],int);
clrscr();
printf("the length of array is=");
scanf("%d",& len);
printf("the elements of array are=");
for(i=0;iscanf("%d", & a[i]);
printf("the array is=");
for(i=0;iprintf("%d", a[i]);


insert(a,len);
getch();
}
void insert(int a[],int len)
{
printf("the number which is to be inserted is=");
scanf("%d",& num);
printf("the position at which no. is going to be inserted=");
scanf("%d", & pos);
--pos;


if pos{
for(i=len;i>=pos;i--)
a[i+1]=a[i];
a[pos]=num;
len++;
}
else
printf("\n outside the array");


printf("the new array is=");
for(i=0;iprintf("%d", a[i]);
}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

hi, after you declare the array, you simply do as follows:

int i[10];

i[4] = 4;

this would declare an array called i of type int with 10 spaces. the fith space (space 4 because we count 0 as the first) would be set to "4" in the above example

Lewis

www.bastmonitoring.com

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In Java, this is a three step process.

  1. Find the correct position for the element. Arrays.binarySearch(array, element) will find the correct position for the element. It returns one of two things: a positive number if the element already exists in the array, or a negative number that represents where it should be placed in the array. Quick example: Arrays.binarySearch(new double[]{1,2,3},2) = 1, Arrays.binarySearch(new double[]{1,2,3},2.5) = -3 (element's correct position-1).
  2. Shift the array to insert the element. This can be done with a simple for loop: for(int i=insert_position; i
  3. Insert the element. Assign the element to its new position. arr[insert_position] = element.
This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Before you can insert an element into an array you must first make space for the new element. In other words, you must reallocate the array. If there is insufficient memory in which to grow the array, the array must be copied to new memory. Thus if you plan on performing many insertions, it pays to reserve memory in advance. This also means you must keep track of how many elements are available in the reserve. When the reserve runs out, you must reallocate.

Assuming you have at least one unused element at the end of your array, you can then determine where the insertion point will be. Assuming the array is in sorted order, you can use the insertion sort algorithm, working backwards through the array, moving elements forward until the new element finds its place.

This answer is:
User Avatar

User Avatar

Learn bay

Lvl 8
2y ago

To begin, obtain the element to be added, such as x.

Then, say pos, get the position where this element will be put.

Then shift the array items one position ahead, then do the same for all the other elements next to pos.

Because the location pos is now empty, insert the element x there.

To learn more about data science please visit- Learnbay.co

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c programm of insertion of element in the array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.


Can you perform insertion and deletion in an array?

You can, but it's not as straightforward as inserting or deleting from a list. This is simply because arrays are not intended for insertions or deletions other than at the end of the array. This is because an insertion requires that the entire array be reallocated (which may require the array to be copied in its entirety) simply in order to make room for the new element, which can then simply be inserted in the unused element at the end of the new array. To insert elsewhere in the array, all the elements following the insertion point need to be copied (for a second time) into the next element, starting with the last used element. the entire process can prove quite costly, especially if the elements are complex objects which would require their copy constructors to be invoked at least once and possibly twice. This is why it is generally better to use arrays of pointers to objects rather than arrays of objects, as copying a pointer is more efficient than copying an object. However, if your array undergoes frequent resizing in order to accommodate insertions and deletions, then you really would be better off using a list.


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


What are the advantages of insertion sort?

It is less efficient on list containing more number of elements. As the number of elements increases the performance of the program would be slow. Insertion sort needs a large number of element shifts.

Related questions

How are arrays processed?

Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.Usually one element at a time. If you want to process all elements of an array, you write a loop.


How do you write a program that accepts 50 integer values and sort them in basic programming?

Create an array with 50 elements and input the integers one a time, filling the array. Use an insertion sort on the array for each input except the first. Alternatively, input the values first and then use insertion sort.


What will happen if you try to write to an array element larger than your array?

The results of that programming error is undefined. You must NEVER EVER write, or EVEN READ an array element beyond the allocated size of the array. Period.I would flunk a student that consistently did this, and I would fire a programmer that did the same.


Write an Algorithm to delete a last element from the array?

// Assuming you dynamically allocated this array using "new"... delete array[arraysize - 1]; arraysize--;


Can you compute one array in c say and then assign b to a?

Yes, you can. You have to do that in the following way. Each element of A should be assighed each element of B. You have to do it manually means you have to write a loop which will do it for you. In C array A = array B is not correct.


Can you perform insertion and deletion in an array?

You can, but it's not as straightforward as inserting or deleting from a list. This is simply because arrays are not intended for insertions or deletions other than at the end of the array. This is because an insertion requires that the entire array be reallocated (which may require the array to be copied in its entirety) simply in order to make room for the new element, which can then simply be inserted in the unused element at the end of the new array. To insert elsewhere in the array, all the elements following the insertion point need to be copied (for a second time) into the next element, starting with the last used element. the entire process can prove quite costly, especially if the elements are complex objects which would require their copy constructors to be invoked at least once and possibly twice. This is why it is generally better to use arrays of pointers to objects rather than arrays of objects, as copying a pointer is more efficient than copying an object. However, if your array undergoes frequent resizing in order to accommodate insertions and deletions, then you really would be better off using a list.


Write a c program to find the maximum value of 25 element in an array?

int findMax(int *array) { int max = array[0]; for(int i = 1; i &lt; array.length(); i++) { if(array[i] &gt; max) max = array[i] } return max; }


What are the advantages of insertion sort?

It is less efficient on list containing more number of elements. As the number of elements increases the performance of the program would be slow. Insertion sort needs a large number of element shifts.


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


Can linked list represent in array if yes show how insertion and deletions can perform in array representation of inked list?

yes


Write a program in c to find the largest no out of a matrix of order mn?

/* using ellipses (...) to indicate tabs for clarity */ double largest (double *array, int M, int N) { ... int i, j; ... double *element; ... double answer = array[0][0]; ... for (i=0; i&lt;M; i++) { ... ... for (j=0; j&lt;N; j++) { ... ... ... element = array + i*M + j; ... ... ... if (*element &gt; answer) answer = *element; ... ... } ... } ... return answer; }


Write down the function to insert an element into a queue, in which the queue is implemented as an array?

bring the police they will be in que by themselves