Inserting a number to an array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 368
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 Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Inserting a number to an array

 
1
  #2
Oct 16th, 2008
Are you sure your program runs fine? At the first glance, there's so many errors in it:

1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.

2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.

3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.

4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.

5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.

  1. numbers[insertItem-1] = insertItem;
  2. cout<<numbers[insertItem-1]<<" ";

6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Inserting a number to an array

 
0
  #3
Oct 16th, 2008
Where did you use the variable index to insert the variable insertItem into the array?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #4
Oct 16th, 2008
This is definitely a job for std::vector!
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 368
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #5
Oct 16th, 2008
I understand that I need to delete some things and make some changes, but I'm not done with the program yet. My program runs and works. I'm just trying to make this adjustment right now. I will make the further changes after I'm done. Can anyone answer my previous question please?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Inserting a number to an array

 
0
  #6
Oct 16th, 2008
Originally Posted by NinjaLink View Post
I understand that I need to delete some things and make some changes, but I'm not done with the program yet. My program runs and works. I'm just trying to make this adjustment right now. I will make the further changes after I'm done. Can anyone answer my previous question please?
Your program most assuredly does NOT run and does NOT work. It runs to completion if you insert a number in such a way so that it doesn't crash and it works under no circumstances as far as I can tell. Reread WaltP's and Denniz's comments. The program does not even do what you state that it does (putting the element in the FRONT of the array). Call your printIt function after you insert/delete and see what I mean, rather than using your cout statements inside of the functions. Also, try inserting a large number (i.e. 100) and see what happens. The cout statements in your insertion function are hindering, not helping, you to debug the program in my view, as they do not reflect what your array contents are after you insert.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 368
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #7
Oct 16th, 2008
Originally Posted by Denniz View Post
Are you sure your program runs fine? At the first glance, there's so many errors in it:

1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.

2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.

3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.

4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.

5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.

  1. numbers[insertItem-1] = insertItem;
  2. cout<<numbers[insertItem-1]<<" ";

6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.

1. What values should I set for index and insertItem? I'm currently using it for user input

2. In the book, the directions stated to set those exact names in the parameters for insertAt, but it didn't tell me to set it to values. I just used it as user input

3. item is used for user input, so i didn't set a value for it

4. How do I make it loop around each time a user inputs a number?

5. For this one, do I need to set Index as a certain value for the piece of code to work? If so, what value am I suppose to set it to?

6. Can you give me an example of how to do this?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 368
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #8
Oct 16th, 2008
Originally Posted by WaltP View Post
Where did you use the variable index to insert the variable insertItem into the array?

I used the index underneath my cout statements for user input. Is it suppose to be placed there?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 368
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #9
Oct 16th, 2008
Originally Posted by VernonDozier View Post
Your program most assuredly does NOT run and does NOT work. It runs to completion if you insert a number in such a way so that it doesn't crash and it works under no circumstances as far as I can tell. Reread WaltP's and Denniz's comments. The program does not even do what you state that it does (putting the element in the FRONT of the array). Call your printIt function after you insert/delete and see what I mean, rather than using your cout statements inside of the functions. Also, try inserting a large number (i.e. 100) and see what happens. The cout statements in your insertion function are hindering, not helping, you to debug the program in my view, as they do not reflect what your array contents are after you insert.

Well, I was working on this project overnight, so I know I had a lot of mistakes in it. I think the main problem is that I have these variables, but I don't exactly know where they are suppose to go. I'm just following what the book told me to do as far as setting the program up in functions and the parameters etc.

Currently, the program runs, and when I do the removeAt function, it removes the numbers that I want to and the place I want it to be removed. It is just the InsertAt function is giving me trouble when I'm adding numbers. Whatever number I want to add, it just hits the front of the array instead of the position. I know my code for insertAt function is wrong, that is why I need help making the adjustment to this. One other problem I noticed was that I don't know how to keep looping when someone writes a number that is out of range. Can you specifically tell me what "index" is? Is that the same as position? Also, where is insertItem specifically suppose to go?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Inserting a number to an array

 
0
  #10
Oct 16th, 2008
numbers[insertItem-1] = insertItem;

Does this mean if you insert, oh i dont know, "6432", it goes into numbers[6431]???

If it does I'd look at it again, because I don't think that's what you WANT it to do
Last edited by chococrack; Oct 16th, 2008 at 6:14 pm.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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