![]() |
| ||
| Re: Inserting a number to an array 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. #include <iostream> 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. |
| ||
| Re: Inserting a number to an array 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! #include <iostream> |
| ||
| Re: Inserting a number to an array any help? i can't figure it out. |
| ||
| Re: Inserting a number to an array Quote:
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: 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: if (i != index) 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: int indexToDelete = ?; You also, when you delete an element, need to adjust the array length since it is now one less than it used to be. |
| ||
| Re: Inserting a number to an array So, I shouldn't use the cin statement for "item"? |
| ||
| Re: Inserting a number to an array Quote:
But you have problems before then in your removeAt function, so I'd take care of those first. |
| ||
| Re: Inserting a number to an array 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: #include <iostream> |
| ||
| Re: Inserting a number to an array 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: 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: 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: for(int i=length; i > index;i--)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: do 6. A lot of hardcoding here and there which is yet another bad practice, but nevermind get the logic right first. |
| ||
| Re: Inserting a number to an array Thanks for helping me. I'm making this as solved because it works now. I appreciate it. The reason why I have the stuff in the parameters is because the directions told me to put them in the parameters, otherwise, i would use them as local variables. |
| All times are GMT -4. The time now is 9:55 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC