inserting an element into an array in c language

Closed Thread

Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

inserting an element into an array in c language

 
0
  #1
Jan 7th, 2005
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 .....
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: inserting an element into an array in c language

 
0
  #2
Jan 7th, 2005
Originally Posted by galmca
i have tried everything
But you forgot to copy and paste these attempts that we may correct them and instead assume that someone will drop in your lap exactly the code you are thinking of.
Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: inserting an element into an array in c language

 
0
  #3
Jan 7th, 2005
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a[11],i,j,k,ch,sh,e;
  6. clrscr();
  7. printf("enter array elements:");
  8. for(i=0;i<10;i++)
  9. scanf("%d",&a[i]);
  10. printf("enter choice\n1.in between insertion\n2.insertion in the beginning\n3.insertion at the end");
  11. scanf("%d",&ch);
  12. switch(ch)
  13. {case1:
  14. printf("enter the element:");
  15. scanf("%d",&e);
  16. printf("enter the element after which the number has to be inserted:");
  17. scanf("%d",&sh);
  18. for(i=0;i<10;i++)
  19. {
  20. if(a[i]==sh)//finding the element
  21. break;
  22. }
  23. for(k=9;k>i;k--)
  24. a[k+1]=a[k];//shifting the element
  25. a[i+1]=e;
  26. break;
  27. case2:
  28. printf("enter the element:");
  29. scanf("%d",&e);
  30. for(k=9;k>=0;k--)
  31. a[k+1]=a[k];
  32. a[0]=e;
  33. break;
  34. case3:
  35. printf("enter the element:");
  36. scanf("%d",&e);
  37. a[10]=e;
  38. }
  39. printf("\n");
  40. for(i=0;i<10;i++)//display the result
  41. printf("%d",a[i]);
  42. getch();
  43. }



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
Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: inserting an element into an array in c language

 
0
  #4
Jan 7th, 2005
Copy and paste directly to avoid typos.
  1. case1:
  2. case2:
  3. case3:
And please use [code][/code] tags.

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
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
Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: inserting an element into an array in c language

 
0
  #5
Jan 8th, 2005
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....
Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: inserting an element into an array in c language

 
0
  #6
Jan 8th, 2005
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?
Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: inserting an element into an array in c language

 
0
  #7
Jan 8th, 2005
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:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(void)
  5. {
  6. int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // allow for extra insert (11 and not 10 items!)
  7. int num = 0;
  8. int data;
  9.  
  10. do
  11. {
  12. cout << "Where do you want to insert an element (0-10)"; // replace it with 1 option
  13. cin >> num; // no ascii check here, watch out!
  14. } while (num < 0 || num > 10); // ask until num is in range
  15.  
  16. // the intended position is in num
  17. cout << "enter data to insert ";
  18. cin >> data;
  19. // the element is in data
  20.  
  21. for(int i = 9; i > num - 1; i--)
  22. array[i + 1] = array[i]; // shift
  23. array[num] = data; // insert
  24.  
  25. cout << "Final Array: ";
  26. for(int i = 0; i < 11; i++)
  27. cout << array[i] << ",";
  28.  
  29. return 0;
  30. }

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:
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: inserting an element into an array in c language

 
0
  #8
Jan 8th, 2005
Deja vu, deja vu, the OP has some learning to do!

Why start two threads with the exact same question?
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC