View Single Post
Join Date: Mar 2008
Posts: 370
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Need help removing number from an array

 
0
  #1
Oct 21st, 2008
I'm having problems in my "RemoveAt" function. I am trying to get a user to input a a position that represents a number they would like to remove from the array. Example below.

For example:


Current array....
4,23,65,34,82,37,12,17,24,36,82,51

User input: 1 (1 represents the position of what number will be removed)

Updated array....
4,65,34,82,37,12,17,24,36,82,51

The number 23 was removed from the array.

It might have something to do with this code, but please take a look at it and let me know what is wrong...My full code is below....Thanks

  1. for (int i = 0; i < length; i++)
  2. {
  3. numbers[i] = numbers[i+1];
  4. cout<<numbers[i]<<" ";
  5. }



  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void printIt (int numbers[],int length);
  7. int removeAt (int numbers[], int length, int index);
  8. void insertAt (int numbers[], int length, int insertItem, int index);
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14. int numbers[20] = {4,23,65,34,82,37,12,17,24,36,82,51}; // numbers stored in array
  15. int length; // length of array
  16. int index; // position in the array
  17. int insertItem; // number inserted into the array
  18.  
  19.  
  20. cout<<"Removing an item from the list..."<<endl;
  21. cout<<endl;
  22.  
  23. printIt(numbers,12);
  24. removeAt(numbers,12,index);
  25. insertAt(numbers,12,insertItem,index);
  26.  
  27.  
  28.  
  29. system ("PAUSE");
  30. return 0;
  31. }
  32.  
  33. void printIt (int numbers[],int length)
  34. {
  35.  
  36. cout<<"The current array..."<<endl;
  37.  
  38. for (int i = 0; i<length; i++)
  39. {
  40. cout<<numbers[i]<<" ";
  41. }
  42. cout<<endl;
  43.  
  44. }
  45.  
  46. int removeAt (int numbers[], int length, int index)
  47. {
  48.  
  49. index = 0;
  50.  
  51. cout<<endl;
  52. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  53. cout<<"Enter the position of the item to be removed."<<endl;
  54. cout<<"Enter 0 for the first item and so on: ";
  55. cin>>index;
  56.  
  57. do // keeps looping until the user puts in correct information
  58. {
  59. if (index > length)
  60. {
  61. cout<<endl;
  62. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  63. cout<<endl;
  64.  
  65. cout<<"The current array..."<<endl;
  66.  
  67. for (int i = 0; i<length; i++)
  68. {
  69. cout<<numbers[i]<<" ";
  70. }
  71. cout<<endl;
  72. cout<<endl;
  73. cout<<"!!!! Index out of Range !!!!"<<endl;
  74. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  75. cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
  76. cout<<"Enter the position of the item to be removed."<<endl;
  77. cout<<"Enter 0 for the first item and so on: ";
  78. cin>>index;
  79. cout<<endl;
  80. }
  81. }
  82. while(index > length);
  83.  
  84. cout<<"After removing the item at position "<<index<<", array is..."<<endl;
  85. cout<<endl;
  86. cout<<"The current array..."<<endl;
  87.  
  88. for (int i = 0; i < length; i++)
  89. {
  90. numbers[i] = numbers[i+1];
  91. cout<<numbers[i]<<" ";
  92. }
  93.  
  94. cout<<endl;
  95. cout<<endl;
  96. cout<<"************************************************************";
  97. cout<<endl;
  98. cout<<endl;
  99.  
  100.  
  101. }
  102.  
  103.  
  104. void insertAt (int numbers[], int length, int insertItem, int index)
  105. {
  106.  
  107.  
  108. cout<<"Inserting an item in the list..."<<endl;
  109. cout<<endl;
  110.  
  111. printIt(numbers,11);
  112.  
  113. cout<<endl;
  114. cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
  115. cout<<"Enter item to be inserted and its position"<<endl;
  116. cout<<"Position of the first element is 0,"<<endl;
  117. cout<<"so if you want the #5 at the front type in: "<<endl;
  118. cout<<"5 (space) 0 "<<endl;
  119. cin>>insertItem;
  120. cin>>index;
  121.  
  122.  
  123. do
  124. {
  125. if (index > length)
  126. {
  127. cout<<endl;
  128. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  129. cout<<endl;
  130.  
  131. printIt(numbers,11);
  132. cout<<endl;
  133. cout<<endl;
  134. cout<<"!!!! Index out of Range !!!!"<<endl;
  135. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  136. cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
  137. cout<<endl;
  138. cout<<"Enter item to be inserted and its position"<<endl;
  139. cout<<"Position of the first element is 0,"<<endl;
  140. cout<<"so if you want the #5 at the front type in: "<<endl;
  141. cout<<"5 (space) 0 "<<endl;
  142. cin>>insertItem;
  143. cin>>index;
  144. }
  145. } while (index > length);
  146.  
  147. cout<<endl;
  148. cout<<"After inserting the item at position "<<index<<", array is..."<<endl;
  149. cout<<endl;
  150.  
  151.  
  152. for (int i = length; i > index; i--)
  153. {
  154. numbers[i] = numbers[i-1];
  155. }
  156. numbers[index] = insertItem;
  157.  
  158. printIt(numbers,11);
  159.  
  160. }
Reply With Quote