| | |
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 |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop interest intmain() kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard strchr string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h





