inserting an element into an array in c language

Reply

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 .....
Reply With Quote 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
  #2
Jan 8th, 2005
To insert a value into an array, you need to shift every value after the location you want to insert. This way you make a hole for the new value. Of course, this can be a problem if the array is already full. You would most likely end up throwing away the last value in the list. If that's acceptable then the logic is very simple: start from the end and copy the previous item to the current element until you hit the location you want to insert. A simple implementation might look like this:
  1. #include <stdio.h>
  2.  
  3. #define length(x) (sizeof x / sizeof *x)
  4.  
  5. int insert(int *array, int val, size_t size, size_t at)
  6. {
  7. size_t i;
  8.  
  9. /* In range? */
  10. if (at >= size)
  11. return -1;
  12.  
  13. /* Shift elements to make a hole */
  14. for (i = size - 1; i > at; i--)
  15. array[i] = array[i - 1];
  16. /* Insertion! */
  17. array[at] = val;
  18.  
  19. return 0;
  20. }
  21.  
  22. int main(void)
  23. {
  24. int a[11] = {0,1,2,3,4,5,6,7,8,9};
  25. int i;
  26.  
  27. if (insert(a, 6, length(a), 3) != -1) {
  28. for (i = 0; i < length(a); i++)
  29. printf("%-3d", a[i]);
  30. printf("\n");
  31. }
  32. else
  33. fprintf(stderr, "Insertion failed\n");
  34.  
  35. return 0;
  36. }
I'm here to prove you wrong.
Reply With Quote 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
  #3
Jan 8th, 2005
from your previous post (looking at the post times you must of missed this one otherwise you wouldnt post again!)

  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 (untested)
  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--) // i goes from num to 10
  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. cin >> num;
  29. return 0;
  30. }

solved your specific problem - narue's is the same but put into a function which is more useful to you - and her's is in c

sorry for butting in narue!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

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