#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[] = {4,23,65,34,82,37,12,17,24,36,82,51}; int length; int index; int insertItem; cout<<"Removing an item from the list..."<<endl; cout<<endl; printIt(numbers,12); removeAt(numbers,12,index); printIt (numbers, 12); insertAt(numbers,12,insertItem,index); printIt(numbers,12); 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) { int item; 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>>item; if (item > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; printIt (numbers, length); 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 "<<item<<", 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>>item; cout<<endl; cout<<"After removing the item at position "<<item<<", array is..."<<endl; cout<<endl; } printIt (numbers, length); } void insertAt (int numbers[], int length, int insertItem, int index) { int item; cout<<"Inserting an item in the list..."<<endl; cout<<endl; printIt(numbers, length); 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; if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; printIt (numbers, length); 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; } cout<<endl; cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl; cout<<endl; numbers[insertItem-1] = insertItem; // cout<<numbers[insertItem-1]<<" "; // get rid of this line! printIt (numbers, length); }
#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[] = {4,23,65,34,82,37,12,17,24,36,82,51}; int length = 12; int index; int insertItem; cout<<"Removing an item from the list..."<<endl; cout<<endl; printIt(numbers,12); removeAt(numbers,12,index); insertAt(numbers,12,insertItem,index); // printIt(numbers,12); system ("PAUSE"); return 0; } void printIt (int numbers[],int length) { for (int i = 0; i<length; i++) { cout<<numbers[i]<<" "; } cout<<endl; } int removeAt (int numbers[], int length, int index) { 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; while (index > length) { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; cout<<"The current array..."<<endl; printIt(numbers,12); cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 12)"<<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; cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array is..."<<endl; printIt(numbers,12); } } for (int i = 0; i < length; i++) { if (i != index) { cout<<numbers[i]<<" "; } } cout<<endl; } void insertAt (int numbers[], int length, int insertItem, int index) { cout<<endl; cout<<"****************************************************"<<endl; cout<<endl; cout<<"Inserting an item in the list..."<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i < length; i++) { if (i != index) { cout<<numbers[i]<<" "; } } cout<<endl; cout<<endl; cout<<"There are 10 items(s) in the list (position 0 through 10)"<<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; if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; printIt (numbers, length); cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 10)"<<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; } cout<<endl; cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl; cout<<endl; numbers[index-1] = insertItem; for (int i = 0; i < length; i++) { if (i != index) { cout<<numbers[i]<<" "; } } }
When I come to my insertAt function and print my current array at the top of the function, it prints out everything except the 8th element in the same order as if I never removed any element.
int removeAt (int numbers[], int length, int index) { if (index > length) { cout<<endl; cout<<"!!!!!!!!!!!!!!!!!! ERROR !!!!!!!!!!!!!!!!!!"<<endl; cout<<endl; cout<<"The current array..."<<endl; printIt(numbers,12); cout<<endl; cout<<endl; cout<<"!!!! Index out of Range !!!!"<<endl; cout<<"There are "<<length<<" item(s) in the list (position 0 through 12)"<<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; cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array is..."<<endl; printIt(numbers,12); } } for (int i = 0; i < length; i++) { if (i != index) { cout<<numbers[i]<<" "; } } cout<<endl; }
numbers[?] = ?;
if (i != index) { cout<<numbers[i]<<" "; }
int indexToDelete = ?; for (int i = ?; i < ?; i++) numbers[i] = numbers[i+1];
So, I shouldn't use the cin statement for "item"?
#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[12] = {4,23,65,34,82,37,12,17,24,36,82,51}; int length; int index; int insertItem; 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) { 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; 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; cout<<"After removing the item at position "<<index<<", array is..."<<endl; cout<<endl; cout<<"The current array..."<<endl; } for (int i = 0; i < 11; 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; 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; } cout<<endl; cout<<"After inserting the item at position "<<insertItem<<", array is..."<<endl; cout<<endl; cout<<"The current array..."<<endl; numbers[index-1] = insertItem; for (int i = 0; i < 11; i++) { cout<<numbers[i]<<" "; } cout<<endl; }
cout<<"After inserting the item at position "<<index<<", array
numbers[index] = insertItem;
for(int i=length; i > index;i--) { number[i] = number[i-1]; } number[index] = insertItem;
do { /// Prompt user for item and index /// Get user's inputs /// If user's inputs is invalid, display error message } while (index > length);
| DaniWeb Message | |
| Cancel Changes | |