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

Inserting a number to an array

 
0
  #1
Oct 16th, 2008
My program runs fine. The only problem is at the bottom of my "insertAt" function. I am trying to insert a number to the array. Whatever number I insert, it keeps appearing at the front of the array instead of the position I want it to.

For example:


Original Array:

23,65,34,82,37,12,17,24,36,82,51

user input: 5 2 (with 5 as the number and 2 as in the position in the array)


comes out as:

5,23,65,34,82,37,12,17,24,36,82,51

instead of

23,65,5,34,82,37,12,17,24,36,82,51[/B]


What is the correct adjustment to this problem?


  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[] = {4,23,65,34,82,37,12,17,24,36,82,51};
  15. int length;
  16. int index;
  17. int insertItem;
  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. int item;
  49.  
  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>>item;
  56.  
  57. if (item > length)
  58. {
  59. cout<<endl;
  60. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  61. cout<<endl;
  62.  
  63. cout<<"The current array..."<<endl;
  64.  
  65. for (int i = 0; i<length; i++)
  66. {
  67. cout<<numbers[i]<<" ";
  68. }
  69. cout<<endl;
  70. cout<<endl;
  71. cout<<"!!!! Index out of Range !!!!"<<endl;
  72. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  73. cout<<"You entered position "<<item<<", which is OUT OF RANGE."<<endl;
  74. cout<<"Enter the position of the item to be removed."<<endl;
  75. cout<<"Enter 0 for the first item and so on: ";
  76. cin>>item;
  77. cout<<endl;
  78. cout<<"After removing the item at position "<<item<<", array is..."<<endl;
  79. cout<<endl;
  80. cout<<"The current array..."<<endl;
  81. }
  82.  
  83. for (int i = 0; i < length; i++)
  84. {
  85. if (i != item)
  86. {
  87. cout<<numbers[i]<<" ";
  88. }
  89. }
  90.  
  91. cout<<endl;
  92. cout<<endl;
  93. cout<<"************************************************************";
  94. cout<<endl;
  95. cout<<endl;
  96.  
  97.  
  98. }
  99.  
  100.  
  101. void insertAt (int numbers[], int length, int insertItem, int index)
  102. {
  103.  
  104. int item;
  105.  
  106.  
  107. cout<<"Inserting an item in the list..."<<endl;
  108. cout<<endl;
  109. cout<<"The current array..."<<endl;
  110.  
  111.  
  112. for (int i = 0; i < length; i++)
  113. {
  114. if (i != item)
  115. {
  116. cout<<numbers[i]<<" ";
  117. }
  118. }
  119. cout<<endl;
  120. cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl;
  121. cout<<"Enter item to be inserted and its position"<<endl;
  122. cout<<"Position of the first element is 0,"<<endl;
  123. cout<<"so if you want the #5 at the front type in: "<<endl;
  124. cout<<"5 (space) 0 "<<endl;
  125. cin>>insertItem;
  126. cin>>index;
  127.  
  128.  
  129. if (index > length)
  130. {
  131. cout<<endl;
  132. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  133. cout<<endl;
  134.  
  135. cout<<"The current array..."<<endl;
  136.  
  137. for (int i = 0; i < length; i++)
  138. {
  139. if (i != item)
  140. {
  141. cout<<numbers[i]<<" ";
  142. }
  143. }
  144. cout<<endl;
  145. cout<<endl;
  146. cout<<"!!!! Index out of Range !!!!"<<endl;
  147. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  148. cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl;
  149. cout<<endl;
  150. cout<<"Enter item to be inserted and its position"<<endl;
  151. cout<<"Position of the first element is 0,"<<endl;
  152. cout<<"so if you want the #5 at the front type in: "<<endl;
  153. cout<<"5 (space) 0 "<<endl;
  154. cin>>insertItem;
  155. cin>>index;
  156. }
  157.  
  158. cout<<endl;
  159. cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl;
  160. cout<<endl;
  161. cout<<"The current array..."<<endl;
  162.  
  163. numbers[insertItem-1] = insertItem;
  164. cout<<numbers[insertItem-1]<<" ";
  165.  
  166. for (int i = 0; i < length; i++)
  167. {
  168. if (i != item)
  169. {
  170. cout<<numbers[i]<<" ";
  171. }
  172. }
  173. cout<<endl;
  174. }
Reply With Quote