1

I am trying to create a C function that takes an array, finds the smallest element, and sets the value of that element to zero.

This is my function so far.

void find_minimum(double a[], int n) {

    int i, smallest = 0;

    smallest = a[0];

    for (i = 0; i < n; i++) {
        if (a[i] < smallest) {
            smallest = a[i];
        }
        a[i] = 0;
    }
}

When I run it, every index except the last one is zero, but I want only the smallest element to be zero.

4
  • move a[i] = 0 ; inside the if statement, and start comparison from the index 1 i.e. i=1 in for loop Commented Jun 14, 2016 at 21:28
  • And what if there is more than 1 smallest element? Commented Jun 14, 2016 at 21:43
  • @ImranAli no, then it will set the smallest-so-far to 0 each time it finds a new smallest Commented Jun 14, 2016 at 21:52
  • 2
    The title says "index", I guess from the description you actually meant "value" Commented Jun 14, 2016 at 21:53

3 Answers 3

2

There's a few issues with your code, I'm surprised your compiler didn't explode on you.

void find_minimum(double a[], size_t n) {

  size_t i, index;
  double smallest;

  // sanity check to make sure we're not accessing outside allocated memory
  if (n > 0) {
    smallest = a[0];
    index = 0;
  } else {
    // nothing left to do here
    return;
  }

  // start at 1 because 0 is initial default
  for (i = 1; i < n; i++) {
    if (a[i] < smallest) {
      smallest = a[i];
      index = i;
    }
  }

  // assign only the smallest index
  a[index] = 0.0;
}

Feel free to post comments if you have any questions.

Sign up to request clarification or add additional context in comments.

5 Comments

you can remove second sanity check by doing an else return on the first one
@pm100 good point, and a more DRY answer. I will update that, thank you. It's fixed now.
Hey Patrick. I implemented your code but it didn't change anything in the array. I appreciate your help tho. I know to start i at 1 instead of 0 now and also know about size_t
@JayJava please provide code demonstrating how you call the function.
@PatrickRoberts follow up question, how could I alter this to set the 2, 3, or n lowest elements to zero? Im thinking either surrounding this code around a while loop or creating a new function and calling this one
1

If we're assuming only one smallest like the OP and some answers, we can also keep track of just the index of the smallest, instead of the value of the smallest, to keep things simple:

void find_minimum(double a[], size_t n) {
    if (n <= 0) {
        return;
    }

    size_t smallest = 0;

    for (size_t i = 1; i < n; i++) {
        if (a[i] < a[smallest]) {
            smallest = i;
        }
    }

    a[smallest] = 0.0;
}

1 Comment

Thanks but this function didn't change any values of the array. I think I may have a complier issue because other solution didn't work
0

I'd like to address the case where there are more then one unique smallest value.

In such a case, two loops are needed. One to find out the smallest value over the entire array and the other to set to zero all the elements with that value.

I'll keep track of the minimum and maximum index where this value appears in the array to minimize the range of the second loop:

void set_smallest_to_zero(double a[], size_t n) {

    if ( !a || !n ) {
        return;
    }

    double smallest = a[0];
    size_t min_index = 0,               
           max_index = 0,
           i;

    // find the smallest values
    for ( i = 1; i < n; ++i ) {
        if ( a[i] > smallest ) {
            continue;
        } else if ( a[i] < smallest ) {
            smallest = a[i];
            min_index = i;
            max_index = i;
        } else {                       //  a[i] == smallest
            max_index = i;
        }
    }

    // set to zero all the the minimum values
    for ( i = min_index; i <= max_index; ++i ) {
        if ( a[i] == smallest ) {
            a[i] = 0.0;
        }
    }
}

HERE there is a live example.

1 Comment

Thanks. This works and appreciate you addresses other cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.