GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Sep-2003, 14:07
ionyka ionyka is offline
New Member
 
Join Date: Sep 2003
Posts: 1
ionyka is an unknown quantity at this point

deleting from an array


Hi, 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:
void rmvOdd (int *array2)
{

for(int i = 1; i < 10; i++)
{
 if( (*(array2 + i) % 2) != 0)
  array2[i] = array2[i+1];
}

for(int j = 1; j < 10; j++)
{
 cout<<*(array2 + j)<<endl;
}

}
Thanks for any help!
  #2  
Old 02-Sep-2003, 04:09
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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:
if( (*(array2 + i) % 2) != 0){
array2[i] = array2[i+1];
}
is in charge of what happens when an odd number is found.

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:
4,4,5,6,7,8,9,10,11,12

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:
4,4,6,6,7,8,9,10,11,12

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 GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 17:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.