| | |
Inserting a number to an array
Thread Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 356
Reputation:
Solved Threads: 0
My program runs fine. The only problem is at the bottom of my "insertAt" function. I am trying to insert a number to the array. Whatever number I insert, it keeps appearing at the front of the array instead of the position I want it to.
For example:
Original Array:
23,65,34,82,37,12,17,24,36,82,51
user input: 5 2 (with 5 as the number and 2 as in the position in the array)
comes out as:
5,23,65,34,82,37,12,17,24,36,82,51
instead of
23,65,5,34,82,37,12,17,24,36,82,51[/B]
What is the correct adjustment to this problem?
For example:
Original Array:
23,65,34,82,37,12,17,24,36,82,51
user input: 5 2 (with 5 as the number and 2 as in the position in the array)
comes out as:
5,23,65,34,82,37,12,17,24,36,82,51
instead of
23,65,5,34,82,37,12,17,24,36,82,51[/B]
What is the correct adjustment to this problem?
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); 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) { 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; 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 "<<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; cout<<"The current array..."<<endl; } for (int i = 0; i < length; i++) { if (i != item) { cout<<numbers[i]<<" "; } } cout<<endl; cout<<endl; cout<<"************************************************************"; cout<<endl; cout<<endl; } void insertAt (int numbers[], int length, int insertItem, int index) { int item; cout<<"Inserting an item in the list..."<<endl; cout<<endl; cout<<"The current array..."<<endl; for (int i = 0; i < length; i++) { if (i != item) { cout<<numbers[i]<<" "; } } 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; cout<<"The current array..."<<endl; for (int i = 0; i < length; i++) { if (i != item) { 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. 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[insertItem-1] = insertItem; cout<<numbers[insertItem-1]<<" "; for (int i = 0; i < length; i++) { if (i != item) { cout<<numbers[i]<<" "; } } cout<<endl; }
Are you sure your program runs fine? At the first glance, there's so many errors in it:
1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.
2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.
3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.
4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.
5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.
6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.
1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.
2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.
3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.
4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.
5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.
C++ Syntax (Toggle Plain Text)
numbers[insertItem-1] = insertItem; cout<<numbers[insertItem-1]<<" ";
6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.
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
•
•
Join Date: Jan 2008
Posts: 3,765
Reputation:
Solved Threads: 493
•
•
•
•
I understand that I need to delete some things and make some changes, but I'm not done with the program yet. My program runs and works. I'm just trying to make this adjustment right now. I will make the further changes after I'm done. Can anyone answer my previous question please?
•
•
Join Date: Mar 2008
Posts: 356
Reputation:
Solved Threads: 0
•
•
•
•
Are you sure your program runs fine? At the first glance, there's so many errors in it:
1. Your main function is passing uninitialized variables' values like index and insertItem into removeAt() and insertAT() functions.
2. Inside insertAt() function, index and insertItem are treated as local variables. That's very bad programming. If you don't intend to pass values, you should simply declare those as local variables instead of parameters.
3. In insertAt() function, the variable item is neither initialized nor assigned any values, yet it is used at the end of the function inside the loop. This leads to some undefined behaviour.
4. You only check once for invalid user input. You should use a loop such that the program will always prompt the user for inputs after each invalid inputs.
5. You use insertItem for the item to be inserted, and index for the position to be inserted. Yet in the below code you never used the variable index! You should used it as the index of the array.
C++ Syntax (Toggle Plain Text)
numbers[insertItem-1] = insertItem; cout<<numbers[insertItem-1]<<" ";
6. Even if you rectify for point number 5, it still doesn't achieve what you want. It merely overwrite the existing array item with the new item, instead of inserting the item into the position stated. To do what you want, all the subsequent array items must be pushed backwards. If you want to use integer array, then you need to manually "grows" the array to cater for the possible overflow of array.
1. What values should I set for index and insertItem? I'm currently using it for user input
2. In the book, the directions stated to set those exact names in the parameters for insertAt, but it didn't tell me to set it to values. I just used it as user input
3. item is used for user input, so i didn't set a value for it
4. How do I make it loop around each time a user inputs a number?
5. For this one, do I need to set Index as a certain value for the piece of code to work? If so, what value am I suppose to set it to?
6. Can you give me an example of how to do this?
•
•
Join Date: Mar 2008
Posts: 356
Reputation:
Solved Threads: 0
•
•
•
•
Your program most assuredly does NOT run and does NOT work. It runs to completion if you insert a number in such a way so that it doesn't crash and it works under no circumstances as far as I can tell. Reread WaltP's and Denniz's comments. The program does not even do what you state that it does (putting the element in the FRONT of the array). Call your printIt function after you insert/delete and see what I mean, rather than using your cout statements inside of the functions. Also, try inserting a large number (i.e. 100) and see what happens. The cout statements in your insertion function are hindering, not helping, you to debug the program in my view, as they do not reflect what your array contents are after you insert.
Well, I was working on this project overnight, so I know I had a lot of mistakes in it. I think the main problem is that I have these variables, but I don't exactly know where they are suppose to go. I'm just following what the book told me to do as far as setting the program up in functions and the parameters etc.
Currently, the program runs, and when I do the removeAt function, it removes the numbers that I want to and the place I want it to be removed. It is just the InsertAt function is giving me trouble when I'm adding numbers. Whatever number I want to add, it just hits the front of the array instead of the position. I know my code for insertAt function is wrong, that is why I need help making the adjustment to this. One other problem I noticed was that I don't know how to keep looping when someone writes a number that is out of range. Can you specifically tell me what "index" is? Is that the same as position? Also, where is insertItem specifically suppose to go?
numbers[insertItem-1] = insertItem;
Does this mean if you insert, oh i dont know, "6432", it goes into numbers[6431]???
If it does I'd look at it again, because I don't think that's what you WANT it to do
Does this mean if you insert, oh i dont know, "6432", it goes into numbers[6431]???
If it does I'd look at it again, because I don't think that's what you WANT it to do
Last edited by chococrack; Oct 16th, 2008 at 6:14 pm.
I would love to change the world, but they won't give me the source code
![]() |
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 auto based binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count delay-loading delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






