| | |
inserting an element into an array in c language
![]() |
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
hi
i am right now new to c language..and i have been facing problem in 1 array problem where i have to insert an element in to an array...i would like ur help plzz..i have tried everything but i m not able to make out from where i should start although i know the logic but i don't know what will be the code as i have not reached to any conclusion....so plz help me out .....
i am right now new to c language..and i have been facing problem in 1 array problem where i have to insert an element in to an array...i would like ur help plzz..i have tried everything but i m not able to make out from where i should start although i know the logic but i don't know what will be the code as i have not reached to any conclusion....so plz help me out .....
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() { int a[11],i,j,k,ch,sh,e; clrscr(); printf("enter array elements:"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end"); scanf("%d",&ch); switch(ch) {case1: printf("enter the element:"); scanf("%d",&e); printf("enter the element after which the number has to be inserted:"); scanf("%d",&sh); for(i=0;i<10;i++) { if(a[i]==sh)//finding the element break; } for(k=9;k>i;k--) a[k+1]=a[k];//shifting the element a[i+1]=e; break; case2: printf("enter the element:"); scanf("%d",&e); for(k=9;k>=0;k--) a[k+1]=a[k]; a[0]=e; break; case3: printf("enter the element:"); scanf("%d",&e); a[10]=e; } printf("\n"); for(i=0;i<10;i++)//display the result printf("%d",a[i]); getch(); }
when i run this code...the output is like:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
enter the choice1.insertion in between 2.insertion in the beginning 3.insertion at the end
so when i enter choice 1.insertion in the beginning...it immediately prints:
enter the element:20
it does not print the line"enter the element after which the number has to be inserted"
and shows the unusual output of 1 2 3 4 5 6 7 8 9 10 20 52 4.....
now i don't seem to understand why is this program showing such kind of error while running?
plz help me out here........
Last edited by kc0arf; Jan 7th, 2005 at 4:34 pm. Reason: Code tags
Copy and paste directly to avoid typos. And please use [code][/code] tags.
My test: You're not running an un-fixed MSVC6 by chance?
Also, reading a number from the stdin has caveats. Enlighten yourself.
http://faq.cprogramming.com/cgi-bin/...043284385#opt3
C Syntax (Toggle Plain Text)
case1: case2: case3:
My test:
•
•
•
•
enter array elements:1 2 3 4 5 6 7 8 9 10
enter choice
1.in between insertion
2.insertion in the beginning
3.insertion at the end1
enter the element:2
enter the element after which the number has to be inserted:5
1234526789
Also, reading a number from the stdin has caveats. Enlighten yourself.
http://faq.cprogramming.com/cgi-bin/...043284385#opt3
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Solved Threads: 0
what is the error in this program?im still not able to find out?it is still giving me the same output of 12 3 4 5 6 7 8 9 10 52 30 ....what is the error here?
[code]
#include<stdio.h>
#include<conio.h>
void main()
{
int a[11],i,j,k,ch,sh,e;
clrscr();
printf("enter array elements:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
scanf("%d",&ch);
switch(ch)
{case1:
printf("enter the element:");
scanf("%d",&e);
printf("enter the element after which the number has to be inserted:");
scanf("%d",&sh);
for(i=0;i<10;i++)
{
if(a[i]==sh)//finding the element
break;
}
for(k=9;k>i;k--)
a[k+1]=a[k];//shifting the element
a[i+1]=e;
break;
case2:
printf("enter the element:");
scanf("%d",&e);
for(k=9;k>=0;k--)
a[k+1]=a[k];
a[0]=e;
break;
case3:
printf("enter the element:");
scanf("%d",&e);
a[10]=e;
}
printf("\n");
for(i=0;i<11;i++)//display the result
printf("%d",a[i]);
getch();
}
when i run this code...the output is like:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
enter the choice1.insertion in between 2.insertion in the beginning 3.insertion at the end
so when i enter choice 1.insertion in the between and other choices...it immediately prints:
enter the element:30
it does not print the line"enter the element after which the number has to be inserted"
and shows the unusual output of 1 2 3 4 5 6 7 8 9 10 52 33 30.....
now i don't seem to understand why is this program showing such kind of error while running?
plz help me out here....
[code]
#include<stdio.h>
#include<conio.h>
void main()
{
int a[11],i,j,k,ch,sh,e;
clrscr();
printf("enter array elements:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
scanf("%d",&ch);
switch(ch)
{case1:
printf("enter the element:");
scanf("%d",&e);
printf("enter the element after which the number has to be inserted:");
scanf("%d",&sh);
for(i=0;i<10;i++)
{
if(a[i]==sh)//finding the element
break;
}
for(k=9;k>i;k--)
a[k+1]=a[k];//shifting the element
a[i+1]=e;
break;
case2:
printf("enter the element:");
scanf("%d",&e);
for(k=9;k>=0;k--)
a[k+1]=a[k];
a[0]=e;
break;
case3:
printf("enter the element:");
scanf("%d",&e);
a[10]=e;
}
printf("\n");
for(i=0;i<11;i++)//display the result
printf("%d",a[i]);
getch();
}
when i run this code...the output is like:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
enter the choice1.insertion in between 2.insertion in the beginning 3.insertion at the end
so when i enter choice 1.insertion in the between and other choices...it immediately prints:
enter the element:30
it does not print the line"enter the element after which the number has to be inserted"
and shows the unusual output of 1 2 3 4 5 6 7 8 9 10 52 33 30.....
now i don't seem to understand why is this program showing such kind of error while running?
plz help me out here....
you forgot the [/code] tag to make it display properly - so the code has many typos. Nevertheless i will help by creating a c++ program to do the same, try and spot the similarities to yours:
this program inserts at any location, allowing you to replace all three options with just option 2 (anywhere)
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main(void) { int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // allow for extra insert (11 and not 10 items!) int num = 0; int data; do { cout << "Where do you want to insert an element (0-10)"; // replace it with 1 option cin >> num; // no ascii check here, watch out! } while (num < 0 || num > 10); // ask until num is in range // the intended position is in num cout << "enter data to insert "; cin >> data; // the element is in data for(int i = 9; i > num - 1; i--) array[i + 1] = array[i]; // shift array[num] = data; // insert cout << "Final Array: "; for(int i = 0; i < 11; i++) cout << array[i] << ","; return 0; }
this program inserts at any location, allowing you to replace all three options with just option 2 (anywhere)
http://sales.carina-e.com
no www
no nonsense
coming soon to a pc near you! :cool:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: inserting an element into an array in c language
- Next Thread: Need some help with putting a looping into a converter program.
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi





