943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2590
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 16th, 2008
0

Inserting a number to an array

Expand Post »
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?


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 16th, 2008
1

Re: Inserting a number to an array

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

Where did you use the variable index to insert the variable insertItem into the array?
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,716 posts
since May 2006
Oct 16th, 2008
0

Re: Inserting a number to an array

This is definitely a job for std::vector!
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Oct 16th, 2008
0

Re: Inserting a number to an array

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?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

Click to Expand / Collapse  Quote originally posted by NinjaLink ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

Click to Expand / Collapse  Quote originally posted by Denniz ...
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.

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

Click to Expand / Collapse  Quote originally posted by WaltP ...
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?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

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?
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 16th, 2008
0

Re: Inserting a number to an array

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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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.
Message:
Previous Thread in C++ Forum Timeline: How to optimize this code further for prime numbers
Next Thread in C++ Forum Timeline: Having problem with array in for loop...





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


Follow us on Twitter


© 2011 DaniWeb® LLC