Need help removing number from an array
Please support our C++ advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 219
Reputation:
Solved Threads: 0
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
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
Cplusplus Syntax (Toggle Plain Text)
for (int i = 0; i < length; i++) { numbers[i] = numbers[i+1]; cout<<numbers[i]<<" "; }
Cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; void printIt (int numbers[],int length); int removeAt (int numbers[], int length, int index); void insertAt (int numbers[], int length, int insertItem, int index); int main() { int numbers[20] = {4,23,65,34,82,37,12,17,24,36,82,51}; // numbers stored in array int length; // length of array int index; // position in the array int insertItem; // number inserted into the array cout<<"Removing an item from the list..."<<endl; cout<<endl; printIt(numbers,12); removeAt(numbers,12,index); insertAt(numbers,12,insertItem,index); system ("PAUSE"); return 0; } void printIt (int numbers[],int length) { cout<<"The current array..."<<endl; for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; } int removeAt (int numbers[], int length, int index) { index = 0; cout<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; do // keeps looping until the user puts in correct information { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; cout<<endl; } } while(index > length); cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i < length; i++) { numbers[i] = numbers[i+1]; cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"************************************************************"; cout<<endl; cout<<endl; } void insertAt (int numbers[], int length, int insertItem, int index) { cout<<"Inserting an item in the list..."<<endl; cout<<endl; printIt(numbers,11); cout<<endl; cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl; cout<<"Enter item to be inserted and its position"<<endl; cout<<"Position of the first element is 0,"<<endl; cout<<"so if you want the #5 at the front type in: "<<endl; cout<<"5 (space) 0 "<<endl; cin>>insertItem; cin>>index; do { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; printIt(numbers,11); cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl; cout<<endl; cout<<"Enter item to be inserted and its position"<<endl; cout<<"Position of the first element is 0,"<<endl; cout<<"so if you want the #5 at the front type in: "<<endl; cout<<"5 (space) 0 "<<endl; cin>>insertItem; cin>>index; } } while (index > length); cout<<endl; cout<<"After inserting the item at position "<<index<<", array is..."<<endl; cout<<endl; for (int i = length; i > index; i--) { numbers[i] = numbers[i-1]; } numbers[index] = insertItem; printIt(numbers,11); }
•
•
Posts: 305
Reputation:
Solved Threads: 43
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
You are shifting all the values starting from position 0, you only need to shift them starting at position index.
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.
> numbers[i] = numbers[i+1];
When you're removing, this loop should start at the number you enter, not 0
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!
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!
•
•
Posts: 219
Reputation:
Solved Threads: 0
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)
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)
Cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; void printIt (int numbers[],int length); int removeAt (int numbers[], int length, int index); void insertAt (int numbers[], int length, int insertItem, int index); int main() { int numbers[15] = {4,23,65,34,82,37,12,17,24,36,82,51}; // numbers stored in array int length; // length of array int index = 0; // position in the array int insertItem; // number inserted into the array cout<<"Removing an item from the list..."<<endl; cout<<endl; printIt(numbers,12); removeAt(numbers,11,index); insertAt(numbers,11,insertItem,index); system ("PAUSE"); return 0; } void printIt (int numbers[],int length) { cout<<"The current array..."<<endl; for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; } int removeAt (int numbers[], int length, int index) { index = 0; cout<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; do // keeps looping until the user puts in correct information { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; cout<<endl; } } while(index > length); cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = index; i < length; i++) { numbers[i] = numbers[i+1]; cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"************************************************************"; cout<<endl; cout<<endl; } void insertAt (int numbers[], int length, int insertItem, int index) { index = 0; cout<<"Inserting an item in the list..."<<endl; cout<<endl; printIt(numbers,11); cout<<endl; cout<<"There are 10 items(s) in the list (position 0 through 11)"<<endl; cout<<"Enter item to be inserted and its position"<<endl; cout<<"Position of the first element is 0,"<<endl; cout<<"so if you want the #5 at the front type in: "<<endl; cout<<"5 (space) 0 "<<endl; cin>>insertItem; cin>>index; do { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; printIt(numbers,11); cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"You entered position "<<index<<", which is OUT OF RANGE. Please try again."<<endl; cout<<endl; cout<<"Enter item to be inserted and its position"<<endl; cout<<"Position of the first element is 0,"<<endl; cout<<"so if you want the #5 at the front type in: "<<endl; cout<<"5 (space) 0 "<<endl; cin>>insertItem; cin>>index; } } while (index > length); cout<<endl; cout<<"After inserting the item at position "<<index<<", array is..."<<endl; cout<<endl; for (int i = length; i > index; i--) { numbers[i] = numbers[i-1]; } numbers[index] = insertItem; printIt(numbers,11); }
•
•
Posts: 219
Reputation:
Solved Threads: 0
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:
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:
Cplusplus Syntax (Toggle Plain Text)
int removeAt (int numbers[], int length, int index) { index = 0; cout<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; do // keeps looping until the user puts in correct information { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 11)"<<endl; cout<<"You entered position "<<index<<", which is OUT OF RANGE."<<endl; cout<<"Enter the position of the item to be removed."<<endl; cout<<"Enter 0 for the first item and so on: "; cin>>index; cout<<endl; } } while(index > length); cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = index; i < length; i++) { numbers[i] = numbers[i+1]; cout<<numbers[i]<<" "; } cout<<endl; cout<<endl; cout<<"************************************************************"; cout<<endl; cout<<endl; }
•
•
Posts: 219
Reputation:
Solved Threads: 0
Nevermind...I figured out what was wrong....
Out of an hour of trying to figure it out, that is all i had to do...wow!
Cplusplus Syntax (Toggle Plain Text)
for (int i = index; i < length; i++) { numbers[i] = numbers[i+1]; } printIt(numbers,11);
Out of an hour of trying to figure it out, that is all i had to do...wow!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Removing an item from head of linked list (C)
- My prime Number generator (Python)
- two array comparison (C++)
- Array and File Help (C++)
- Removing characters from a string (C)
- appending and subtracting a number in an array (C++)
- Storing data into an Array (C)
- C++ complete binary tree using an array. Unexpected end file (C++)
- never taught how to show the VAT of a total. I know that u divide by 1.175 (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: did anyone ever debug a c/c++ compiler?
- Next Thread: working with stringstream
•
•
•
•
Views: 466 | Replies: 8 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode