The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UF - User Diary Index

NTSGM thread by myke2002-03-29 22:27:24
  Ideas/Suggestions/Comments Thread by myke2002-03-29 22:30:32
    Yo! Here's what I came up with... by krupa 2002-04-22 00:04:06
I'm sure there are still better ways to do this, but my way doesn't use recursion, and it doesn't make a copy of the array.

int index = ARRAY_SIZE - 1;
int running_index = index;

while( index > 0 ) {

--running_index; // decrement running_ index so it's ahead of index

if( array[index] == 0 ) {

if( running_index < 0 ) { // keep from running past end of array
break;
}

array[ index ] = array[ running_index ];
array[ running_index ] = 0; // we've just moved the value in this position so set it equal to 0

} else {

--index; // array[index] != 0 so go to next element
running_index = index; // we still want to keep running_index ahead of index

} // end of first if statement

} // end of while loop


Hopefully, you can make out what I was doing ( I can't get the formatting decent so you can read it easily). I tested this on a 5 element array with 0's in different positions as well as arrays that have no 0s and all 0s. They all worked without seg faulting.
[ Reply ]
      Implemented - Thank you - but one bug... by myke2002-04-28 22:06:00
        Huh... that's odd. by krupa2002-04-29 13:25:45
          The extra IF by myke2002-04-29 14:27:17
            Is this the same behavior you're seeing? by krupa2002-04-29 14:43:53
              any clue why? by myke2002-04-30 12:55:39
                I haven't had much of a chance to look at it yet.. by krupa2002-04-30 15:08:42

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)