![]() |
| ||
| Need help removing number from an array I'm having problems in my "RemoveAt" function. I am trying to get a user to input a a position that represents a number they would like to remove from the array. Example below. For example: Current array.... 4,23,65,34,82,37,12,17,24,36,82,51 User input: 1 (1 represents the position of what number will be removed) Updated array.... 4,65,34,82,37,12,17,24,36,82,51 The number 23 was removed from the array. It might have something to do with this code, but please take a look at it and let me know what is wrong...My full code is below....Thanks for (int i = 0; i < length; i++) #include <iostream> |
| ||
| Re: Need help removing number from an array You aren't assigning the variable index a value before calling the functions removeAt and insertAt. So you need to fix that first. Well I just noticed that you are asking for the index in your function, in which case why are you passing "index" to it ? You really shouldn't be passing in arguments which have no valid values. Now in your removeAt function you do this for (int i = 0; i < length; i++) You are shifting all the values starting from position 0, you only need to shift them starting at position index. |
| ||
| Re: Need help removing number from an array > numbers[i] = numbers[i+1]; When you're removing, this loop should start at the number you enter, not 0 |
| ||
| Re: Need help removing number from an array Thanks for your replys. I updated my code. Now this is what it is doing.... current array... 4,23,65,34,82,37,12,17,24,36,82,51 user input: 1 updated array... 65,34,82,37,12,17,24,36,82,51 (It now deducts "4" and "23", but that is not what I'm trying to get) I'm trying to get it to be... 4,65,34,82,37,12,17,24,36,82,51 (In which when I enter "1", 23 will be removed) #include <iostream> |
| ||
| Re: Need help removing number from an array are you sure it doesn't work ? it removes it correctly when I try using your code. |
| ||
| Re: Need help removing number from an array Yeah, I tried it a few times, and it didn't work. Like for example: current array... 4,23,65,34,82,37,12,17,24,36,82,51 user input: 2 34,82,37,12,17,24,36,82,51 (It deletes the first 3 numbers, but I don't want that) instead of 4,23,34,82,37,12,17,24,36,82,51 (65 gets deleted from 2) Here is my removeAt function: int removeAt (int numbers[], int length, int index) |
| ||
| Re: Need help removing number from an array Its not deleting 3 numbers. Your for loop prints the numbers starting at index i instead of 0, so you "think" they are deleted. You might want to delete the number and then call your print function. |
| ||
| Re: Need help removing number from an array I don't understand what you mean. The thing that disappoints me is that it was actually working a few days ago. I open the code to print it, and it just didn't work anymore. That is why I'm trying to figure out what is wrong. =/ |
| ||
| Re: Need help removing number from an array Nevermind...I figured out what was wrong.... for (int i = index; i < length; i++) Out of an hour of trying to figure it out, that is all i had to do...wow! |
| All times are GMT -4. The time now is 10:51 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC