944,178 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 41181
  • C RSS
Jan 7th, 2005
0

inserting an element into an array in c language

Expand Post »
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 .....
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Jan 8th, 2005
0

Re: inserting an element into an array in c language

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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 8th, 2005
0

Re: inserting an element into an array in c language

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!
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: how to increase the size of an array?
Next Thread in C Forum Timeline: inserting an element into an array in c language





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC