![]() |
|
#1
|
|||
|
|||
deleting from an arrayHi, im pretty new at c++ and am taking a class right now, but ive run into a problem and i was hoping to see if someone could help me out.
Problem: "write a function that removes all odd integers from an array using pointers." This is what i have so far: the array before: 3,4,5,6,7,8,9,10,11,12 what it has to look like after: 4,6,8,10,12 what below actually does: 4,6,6,8,8,10,10,12,12 my function: CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
|
Hey ionyka, welcome to the forums.
C/C++ isn't flexible with arrays at all. Once you've defined the size of an array, you cannot change it. So you cannot "delete" elements from an array. But you can mark elements as "removed", in your case as settings them as something wierd, maybe 0? Secondly, I'm looking through your code. I've always found it handy for working with arrays to do out a horrible memory map, a simply table with a box for each element of the array. Then step through a a cycles of the loop, and see what happens. If you do this, you'll see what's up. This chunk: CPP / C++ / C Code:
Let i=0; and 3 is odd, the if statement will be true & so you do this: array2[0] = array2[1]. So, you've copied the value 4 over 3, so you're array will look like this: Code:
Let i=1, the number 4 (the second element of the array) is even, so it's okay. Let i=2, we're on the number 5, it's odd, so the array will look like this now: Code:
You see now where it's going wrong? You're not moving the values of the array to the left, you're simply copying even numbers over the odd, making more correct values. What you want to do, it to shift all the values of the array to the left in one go, to prevent any values being copied. It's not veryhard, so I'll let you figure out that, but if you've problems, please ask! Hope this helps Garth Farley |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Sorting a 2D array c++ | mike3340 | C++ Forum | 4 | 15-Dec-2003 14:35 |
| Extra null element in an array | samtediou | MySQL / PHP Forum | 2 | 11-Dec-2003 12:52 |
| radio button problem | zuzupus | MySQL / PHP Forum | 1 | 08-Aug-2003 04:25 |
| Problem with array | norok | MySQL / PHP Forum | 3 | 16-Jul-2003 03:08 |
| Array_search on a multidimensional array | JdS | MySQL / PHP Forum | 3 | 11-May-2003 07:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The