![]() |
| ||
| Inserting a number to an array 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? #include <iostream> |
| ||
| Re: Inserting a number to an array 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. numbers[insertItem-1] = insertItem; 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. |
| ||
| Re: Inserting a number to an array Where did you use the variable indexto insert the variable insertIteminto the array? |
| ||
| Re: Inserting a number to an array This is definitely a job for std::vector! |
| ||
| Re: Inserting a number to an array 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? |
| ||
| Re: Inserting a number to an array Quote:
|
| ||
| Re: Inserting a number to an array Quote:
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? |
| ||
| Re: Inserting a number to an array Quote:
I used the index underneath my cout statements for user input. Is it suppose to be placed there? |
| ||
| Re: Inserting a number to an array Quote:
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? |
| ||
| Re: Inserting a number to an array 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 |
| All times are GMT -4. The time now is 8:02 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC