View Single Post
Join Date: Mar 2008
Posts: 345
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Inserting a number to an array

 
0
  #7
Oct 16th, 2008
Originally Posted by Denniz View Post
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.

  1. numbers[insertItem-1] = insertItem;
  2. 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?
Reply With Quote