How do i insert an element into array?
pleae explain .
How do i insert an element into array?
pleae explain .
AbHHinaay
how to insert an element into an array
this is the code to insert an element in to array
-------------------------------------------------------------
main()
{
int a[10],i,key,pos;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("in which position u want to insert element: ");
scanf("%d",&pos);
scanf("%d",&key);/*element to insert*/
for(i=n-1;i>=pos;i--)
a[i+1] = a[i];
a[pos] = key;
n++;
/* now u have the new element in the array*/
}
You do not need to alply the ampersand sign (&) to read into an array. Since an array is a form of a pointer the & sign is not needed.
Hmm..I was wrong. Funny how I have never come across that before but then again I never do use scanf() .
The code doesn't even compile
Code:Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland junk1.c: Error E2451 junk1.c 5: Undefined symbol 'n' in function main Warning W8065 junk1.c 5: Call to function 'scanf' with no prototype in function main Warning W8065 junk1.c 8: Call to function 'printf' with no prototype in function main Warning W8070 junk1.c 16: Function should return a value in function main *** 1 errors in Compile ***
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]