Thread: Delete Samllest Array Element

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    Delete Samllest Array Element

    I am working on a program which will take in an array[5] and a number - array length. I need to search and find the smallest number in the array, delete it, and then decrease array length by one.

    what i was thinking was just sorting array largest -> smallest, and then decreasing array length by one essentially making the program think that the array is 4 when it is 5.

    Example

    tScore [5]
    arrayLength 5

    sort tScore high -> low
    search tScore for low -> should be tScore[5]
    arrayLength --

    is that the best way to do it?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't "delete" elements from an array. So what you've got in is a decent work around. Consider:
    Code:
    int array[5] = { 5, 12,  7, 3, 33 };
    int size = 5;
    
    size = deletesmallest( array, size );
    Something like this perhaps? Here I'm returning the size of the array, and assigning it back to the 'size' parameter. I'm passing 'size' to the function, along with the array. In the function you'd use the passed size to control your loop, making sure you don't go beyond the passed size.

    Sure, that should work fine. Naturally it's up to you to write the deletesmallest function. If you get stuck, post your attempt, using code tags when you do so.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    That depends on whether you want to "corrupt" the original array by sorting it.

    I would pass through the array once to find the smallest number and make a note of its index.

    You'd need to have 2 scalar variables: smallest and smallest_index (or whatever). Whenever you see an array element that is lower than smallest, set smallest to that array element and save its index in smallest_index.

    Then have a second pass, starting from smallest_index, shuffling subsequent array entries up each overwriting its predecessor, up to the end of the array.

    If it's required for the array to be sorted, then simply dropping the bottom entry will be sufficient after sorting.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure that sorting the array is a legal thing to do?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Consider using memmove() to make your life easier.
    If you understand what you're doing, you're not learning anything.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Who said anything about sorting? Just roll yourself a loop. That's what memmove does.
    Code:
    int array[5] = { 5, 12,  7, 3, 33 };
    int size = 5;
    
    size = deletesmallest( array, size );
    In our above example, we want to remove the three. Therefore we:
    Code:
    walk through the maze, finding the index of the lowest value and store it
    from that stored index:
        copy storedindex + 1 to stored index
        increment stored index
    Repeat until you reach the size limit that was passed.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Absolute value of each element in a given array
    By DriftinSW20 in forum C Programming
    Replies: 9
    Last Post: 11-15-2007, 04:08 PM
  3. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM