943,974 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 674
  • C++ RSS
Oct 21st, 2008
0

Need help removing number from an array

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

C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i < length; i++)
  2. {
  3. numbers[i] = numbers[i+1];
  4. cout<<numbers[i]<<" ";
  5. }



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[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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 21st, 2008
0

Re: Need help removing number from an array

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 1:51 am.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: Need help removing number from an array

> numbers[i] = numbers[i+1];
When you're removing, this loop should start at the number you enter, not 0
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 21st, 2008
0

Re: Need help removing number from an array

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)


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[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. }
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 21st, 2008
0

Re: Need help removing number from an array

are you sure it doesn't work ? it removes it correctly when I try using your code.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: Need help removing number from an array

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:


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

Re: Need help removing number from an array

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 2:31 am.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 21st, 2008
0

Re: Need help removing number from an array

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. =/
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 21st, 2008
0

Re: Need help removing number from an array

Nevermind...I figured out what was wrong....

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 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: did anyone ever debug a c/c++ compiler?
Next Thread in C++ Forum Timeline: working with stringstream





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


Follow us on Twitter


© 2011 DaniWeb® LLC