![]() |
| ||
| inserting an element into an array in c language 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 ..... |
| ||
| Re: inserting an element into an array in c language Quote:
|
| ||
| Re: inserting an element into an array in c language #include<stdio.h> 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........ |
| ||
| Re: inserting an element into an array in c language Copy and paste directly to avoid typos. case1:And please use [code][/code] tags. My test: Quote:
Also, reading a number from the stdin has caveats. Enlighten yourself. http://faq.cprogramming.com/cgi-bin/...043284385#opt3 |
| ||
| Re: inserting an element into an array in c language 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.... |
| ||
| Re: inserting an element into an array in c language plz tell me the errors in my program.....im not able to find out....the errors in my program...plz tell me what r the changes that i need to make in order to make the program run correctly for all the 3 cases? |
| ||
| Re: inserting an element into an array in c language 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: #include <iostream> this program inserts at any location, allowing you to replace all three options with just option 2 (anywhere) |
| ||
| Re: inserting an element into an array in c language Deja vu, deja vu, the OP has some learning to do! Why start two threads with the exact same question? |
| All times are GMT -4. The time now is 6:43 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC