| | |
Inserting a number to an array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
Here is your program basically, with more accurate cout statements to reflect the array. What you were displaying before and what was actually in the array are two different things. The display in this revised program is a more accurate display of the array contents. Try inserting a big number like a million into the array and see whether it crashes or runs to completion.
In some of your displays, you are hardcoding the number of elements in the array into your cout statements (i.e. line 49 and other lines). These should be based on the length variable. Line 93 in particular doesn't make sense. You say you have the array has ten elements, then say those elements are stored in positions 0 through 11.
C++ 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[] = {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); }
In some of your displays, you are hardcoding the number of elements in the array into your cout statements (i.e. line 49 and other lines). These should be based on the length variable. Line 93 in particular doesn't make sense. You say you have the array has ten elements, then say those elements are stored in positions 0 through 11.
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Alright, I made some corrections with the variables and stuff. It should look atleast alittle better than before, but I'm still have trouble. 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. I know something is wrong, but what is the problem? Also, when I insert a number in my insertAt function, the number doesn't get added to the array at all, but it shows that one of my elements is removed. Help!
C++ 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[] = {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]<<" "; } } }
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
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; }
It prints out the array as if you never removed an element because you in fact have never removed any element in the removeAt function. Do you see anywhere in the above code where you assign any value to any array element anywhere, a line that looks like this:
C++ Syntax (Toggle Plain Text)
numbers[?] = ?;
No? Then the array is unchanged. As I mentioned in my last post, your cout statements are obfuscating your program. That's why I replaced the cout statements in the functions with calls to the printIt function. Your array display code in the functions is not displaying the actual array due to lines like this:
C++ Syntax (Toggle Plain Text)
if (i != index) { cout<<numbers[i]<<" "; }
If you need lines like this to get the array to display correctly, that means that the array itself has not changed correctly. Display the array with the printIt function in order to get a real idea of what the program is doing. To delete an element from an array, you need to move all of the array elements after that element to the left by one:
C++ Syntax (Toggle Plain Text)
int indexToDelete = ?; for (int i = ?; i < ?; i++) numbers[i] = numbers[i+1];
You also, when you delete an element, need to adjust the array length since it is now one less than it used to be.
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
That's a separate issue. You don't have a variable called item. You have a variable called insertItem. As someone earlier mentioned, you are passing it by value uninitialized to a function, which makes it of no use, plus you are then overwriting it with your cin statement before you use it, which also makes it of no use.
But you have problems before then in your removeAt function, so I'd take care of those first.
But you have problems before then in your removeAt function, so I'd take care of those first.
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Alright, I made the changes for the values to be passed through the functions correctly. I need help with the inserting the number now. I can't seem to get it to work. The code I'm trying to use is at the bottom insertAt function. What is the correct way of doing this?
Here is my updated code:
Here is my updated code:
C++ 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[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; }
Alright, I will comment base on your latest codes, cos I don't really have time to go through all the posts. Also, I will skip removeAt() function at this moment and focus on the rest.
1. At the main function, you are still passing uninitialized variables "insertItem" and "index" by values to the functions (line 24 and 25). If you have no use for them, take them out from the parameters. Use local variables instead.
2. Line 140 should be this instead:
3. At line 144, according to your instruction at line 132, it should be the following instead:
4. Still, if you perform point 3, you are only OVERWRITING the array item, not inserting it. If you are going to continue using integer array for insertion, there are 2 ways you can do about it:
a) Declare a large array size (say 100) initially, fill the first 12 with your numbers and leave the rest. When doing insertion at say position #2, first use a loop to "push back" all items from position #2 onwards, then write the new value at position #2. Something like this:
You need use the variable length to keep track of the up-to-date length of the array that is filled.
b) The second method would be using dynamic memory allocation for the number array. Every time when you want to do insertion, you will have to copy all the content of the number array into a temporary array, then reallocate memory for the number array to expand its size, then copy content of temporary array back into the number array. I don't recommend this method though its more storage efficient, it will introduce you even more errors in the process.
5. As I said in earlier post, checking of invalid inputs can use loop instead. Something like this:
6. A lot of hardcoding here and there which is yet another bad practice, but nevermind get the logic right first.
1. At the main function, you are still passing uninitialized variables "insertItem" and "index" by values to the functions (line 24 and 25). If you have no use for them, take them out from the parameters. Use local variables instead.
2. Line 140 should be this instead:
C++ Syntax (Toggle Plain Text)
cout<<"After inserting the item at position "<<index<<", array
3. At line 144, according to your instruction at line 132, it should be the following instead:
C++ Syntax (Toggle Plain Text)
numbers[index] = insertItem;
4. Still, if you perform point 3, you are only OVERWRITING the array item, not inserting it. If you are going to continue using integer array for insertion, there are 2 ways you can do about it:
a) Declare a large array size (say 100) initially, fill the first 12 with your numbers and leave the rest. When doing insertion at say position #2, first use a loop to "push back" all items from position #2 onwards, then write the new value at position #2. Something like this:
C++ Syntax (Toggle Plain Text)
for(int i=length; i > index;i--) { number[i] = number[i-1]; } number[index] = insertItem;
b) The second method would be using dynamic memory allocation for the number array. Every time when you want to do insertion, you will have to copy all the content of the number array into a temporary array, then reallocate memory for the number array to expand its size, then copy content of temporary array back into the number array. I don't recommend this method though its more storage efficient, it will introduce you even more errors in the process.
5. As I said in earlier post, checking of invalid inputs can use loop instead. Something like this:
C++ Syntax (Toggle Plain Text)
do { /// Prompt user for item and index /// Get user's inputs /// If user's inputs is invalid, display error message } while (index > length);
6. A lot of hardcoding here and there which is yet another bad practice, but nevermind get the logic right first.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
![]() |
Similar Threads
- Need help checking for repetition in array (C++)
- how to print null subtree of a null subtree? (C++)
- Inserting objects into an array (C++)
- Insert a number in an array (C++)
- help with inserting a number into an array (C++)
- inserting an element into an array in c language (C)
- Array without twice the same number? (C)
Other Threads in the C++ Forum
- Previous Thread: How to optimize this code further for prime numbers
- Next Thread: Having problem with array in for loop...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






