I can't seem to get the correct minimum value to print out no matter what I do. I have tried a while loop, for loop, if statement, if-else statement, but no matter what I do I still get 0.0 as the min value.
Input file:
1355
158.2 157.5
157.0 156.5
156.6 151.2
151.0 148.9
148.7 146.4
0
Code:
/*Reads file and prints total, minimum, maximum and average weight lost to the screen*/
int i, j, k;
float total, value, value2;
float average, min, max;
float startWeek, endWeek, loseWeight;
for (i = 0; i < n; i++) {
fscanf(ifp, "%f", &startWeek);
fscanf(ifp, "%f", &endWeek);
total = total + (startWeek - endWeek);
/*computes the average weight lost*/
average = total/n;
for(j = 0; j < n; j++) {
value = startWeek - endWeek;
if (value > max) {
max = value;
}
for (k = 0; k < n; k++) {
value2 = startWeek - endWeek;
if (min > value2) {
min = value2;
}
}
}
}
printf("Total Weight Lost: %.1f lbs\n", total);
printf("Min lost in a week: %.1f lbs\n", min);
printf("Max lost in a week: %.1f lbs\n", max);
printf("Avg weight lost per week: %.1f lbs\n", average);