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

Re: Need help removing number from an array

 
0
  #4
Oct 21st, 2008
Thanks for your replys.


I updated my code. Now this is what it is doing....

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

user input: 1

updated array...
65,34,82,37,12,17,24,36,82,51 (It now deducts "4" and "23", but that is not what I'm trying to get)


I'm trying to get it to be...

4,65,34,82,37,12,17,24,36,82,51 (In which when I enter "1", 23 will be removed)


  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[15] = {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 = 0; // 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,11,index);
  25. insertAt(numbers,11,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 = index; 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. index = 0;
  108.  
  109. cout<<"Inserting an item in the list..."<<endl;
  110. cout<<endl;
  111.  
  112. printIt(numbers,11);
  113.  
  114. cout<<endl;
  115. cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
  116. cout<<"Enter item to be inserted and its position"<<endl;
  117. cout<<"Position of the first element is 0,"<<endl;
  118. cout<<"so if you want the #5 at the front type in: "<<endl;
  119. cout<<"5 (space) 0 "<<endl;
  120. cin>>insertItem;
  121. cin>>index;
  122.  
  123.  
  124. do
  125. {
  126. if (index > length)
  127. {
  128. cout<<endl;
  129. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  130. cout<<endl;
  131.  
  132. printIt(numbers,11);
  133. cout<<endl;
  134. cout<<endl;
  135. cout<<"!!!! Index out of Range !!!!"<<endl;
  136. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  137. cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
  138. cout<<endl;
  139. cout<<"Enter item to be inserted and its position"<<endl;
  140. cout<<"Position of the first element is 0,"<<endl;
  141. cout<<"so if you want the #5 at the front type in: "<<endl;
  142. cout<<"5 (space) 0 "<<endl;
  143. cin>>insertItem;
  144. cin>>index;
  145. }
  146. } while (index > length);
  147.  
  148. cout<<endl;
  149. cout<<"After inserting the item at position "<<index<<", array is..."<<endl;
  150. cout<<endl;
  151.  
  152.  
  153. for (int i = length; i > index; i--)
  154. {
  155. numbers[i] = numbers[i-1];
  156. }
  157. numbers[index] = insertItem;
  158.  
  159. printIt(numbers,11);
  160.  
  161. }
Reply With Quote