skip/ignore array index that contain value in c -
my goal create c function finds minimum value of array , set zero. far, function ignore/skip indexes zero.
void find_minimum(double a[], int n) { int i, index; double low; index = 0; low = a[0]; (i = 1; < n; i++) { if ( a[i] < low && a[i] != 0.0) { low = a[i]; index = i; } } a[index] = 0.0; }
i tried using continue
statement this:
void find_minimum(double a[], int n) { int i, index; double low; index = 0; low = a[0]; (i = 1; < n; i++) { if(a[i] == 0.0){ continue; } if ( a[i] < low) { low = a[i]; index = i; } } a[index] = 0.0; }
what missing or messing up?
your problem is, code ignores case when a[0]
zero. try assign low constant double_max , iterate through whole array instead (i = 0
in for
initialization statement instead of i = 1
).
void find_minimum(double a[], int n) { int i, index; double low; index = 0; low = double_max; (i = 0; < n; i++) { if ( a[i] < low && a[i] != 0.0) { low = a[i]; index = i; } } a[index] = 0.0; }
Comments
Post a Comment