RSS Forums RSS

Need help removing number from an array

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Need help removing number from an array

  #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. }
AddThis Social Bookmark Button
Reply With Quote  
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Need help removing number from an array

  #2  
Oct 21st, 2008
You aren't assigning the variable index a value before calling the functions removeAt and insertAt. So you need to fix that first.

Well I just noticed that you are asking for the index in your function, in which case why are you passing "index" to it ? You really shouldn't be passing in arguments which have no valid values.

Now in your removeAt function you do this

for (int i = 0; i < length; i++)
   {
     numbers[i] = numbers[i+1];
     cout<<numbers[i]<<" ";
   }

You are shifting all the values starting from position 0, you only need to shift them starting at position index.
Last edited by stilllearning : Oct 21st, 2008 at 12:51 am.
Reply With Quote  
Posts: 5,133
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 634
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help removing number from an array

  #3  
Oct 21st, 2008
> numbers[i] = numbers[i+1];
When you're removing, this loop should start at the number you enter, not 0
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Re: Need help removing number from an array

  #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  
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Need help removing number from an array

  #5  
Oct 21st, 2008
are you sure it doesn't work ? it removes it correctly when I try using your code.
Reply With Quote  
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Re: Need help removing number from an array

  #6  
Oct 21st, 2008
Yeah, I tried it a few times, and it didn't work.

Like for example:


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

user input: 2

34,82,37,12,17,24,36,82,51 (It deletes the first 3 numbers, but I don't want that)

instead of

4,23,34,82,37,12,17,24,36,82,51 (65 gets deleted from 2)





Here is my removeAt function:


  1. int removeAt (int numbers[], int length, int index)
  2. {
  3.  
  4. index = 0;
  5.  
  6. cout<<endl;
  7. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  8. cout<<"Enter the position of the item to be removed."<<endl;
  9. cout<<"Enter 0 for the first item and so on: ";
  10. cin>>index;
  11.  
  12. do // keeps looping until the user puts in correct information
  13. {
  14. if (index > length)
  15. {
  16. cout<<endl;
  17. cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl;
  18. cout<<endl;
  19.  
  20. cout<<"The current array..."<<endl;
  21.  
  22. for (int i = 0; i<length; i++)
  23. {
  24. cout<<numbers[i]<<" ";
  25. }
  26. cout<<endl;
  27. cout<<endl;
  28. cout<<"!!!! Index out of Range !!!!"<<endl;
  29. cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl;
  30. cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl;
  31. cout<<"Enter the position of the item to be removed."<<endl;
  32. cout<<"Enter 0 for the first item and so on: ";
  33. cin>>index;
  34. cout<<endl;
  35. }
  36. }
  37. while(index > length);
  38.  
  39. cout<<"After removing the item at position "<<index<<", array is..."<<endl;
  40. cout<<endl;
  41. cout<<"The current array..."<<endl;
  42.  
  43. for (int i = index; i < length; i++)
  44. {
  45. numbers[i] = numbers[i+1];
  46. cout<<numbers[i]<<" ";
  47. }
  48.  
  49. cout<<endl;
  50. cout<<endl;
  51. cout<<"************************************************************";
  52. cout<<endl;
  53. cout<<endl;
  54.  
  55.  
  56. }
Reply With Quote  
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Need help removing number from an array

  #7  
Oct 21st, 2008
Its not deleting 3 numbers. Your for loop prints the numbers starting at index i instead of 0, so you "think" they are deleted.

You might want to delete the number and then call your print function.
Last edited by stilllearning : Oct 21st, 2008 at 1:31 am.
Reply With Quote  
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Re: Need help removing number from an array

  #8  
Oct 21st, 2008
I don't understand what you mean.

The thing that disappoints me is that it was actually working a few days ago. I open the code to print it, and it just didn't work anymore. That is why I'm trying to figure out what is wrong. =/
Reply With Quote  
Posts: 219
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz in Training

Re: Need help removing number from an array

  #9  
Oct 21st, 2008
Nevermind...I figured out what was wrong....

  1. for (int i = index; i < length; i++)
  2. {
  3. numbers[i] = numbers[i+1];
  4. }
  5.  
  6. printIt(numbers,11);

Out of an hour of trying to figure it out, that is all i had to do...wow!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 466 | Replies: 8 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:43 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC