vendredi 19 août 2016

find smallest element of an array and set its index to 0

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.

Aucun commentaire:

Enregistrer un commentaire