Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

C Question: What's wrong with my program?

OpenStudy (anonymous):

I can't get it to find the element with the least value in the array. #include <stdio.h> int main (void) { int i; float array[5], Lowest = array[0], Total = 0, Average; for (i = 0; i < 5; i++) { printf("Input Quiz %d score: ", i+1); scanf("%f", &array[i]); if (Lowest > array[i]) { Lowest = array[i]; } Total += array[i]; } Average = (Total - Lowest) / (i-1); printf("%f", array[0]); printf("%f", array[1]); printf("%f", array[2]); printf("%f", array[3]); printf("%f", array[4]); printf("Average: %f", Average); printf("Lowest: %f", Lowest); printf("Total: %f", Total); }

OpenStudy (asnaseer):

My guess would be that you are initialising Lowest to array[0] before array[0] has been assignd a value. Instead of this, I would assign it FLT_MAX from float.h (see: http://publications.gbdirect.co.uk/c_book/chapter9/limits.html).

OpenStudy (anonymous):

I agree with asnaseer, you should set Lowest to the FLT_MAX. And since you do not initialize the array variable setting Lowest to array[0] is not giving you the correct data either.

OpenStudy (anonymous):

Why dont you declare a new int variable and when the user gives you input you check against it, so if the new grade has less value you make that the variable.

OpenStudy (llib_xoc):

cosio55 has a good idea. Your program is only computing or remembering a single value, right? So you don't need an array, as I understand the requirements.

OpenStudy (shadowfiend):

There is no reason to use FLT_MAX. The correct way to do a minimum comparison like this is to assign it to the first value in the array. HOWEVER, your first value isn't initialized when you set it up. If you want to do this, you need to read in all of the values the user gives you, *then* set Lowest to the first value and do everything else.

OpenStudy (shadowfiend):

What you're doing is basically declaring the array, which gets filled with uninitialized values. Then, you put one of those uninitialized values in the Lowest variable. Because the garbage value can and often is negative, it may be lower than everything else. Sometimes, you'll get lucky and get an uninitialized value that will let the program run correctly.

OpenStudy (asnaseer):

I totally agree with @shadowfiend's suggestions.

OpenStudy (anonymous):

Hmm, I tried doing this: for (i = 0; i < 5; i++) { printf("Input Quiz %d score: ", i+1); scanf("%f", &Score[i]); if (i == 0) Lowest = Score[0]; if (Lowest > Score[i]) Lowest = Score[i]; Total += Score[i]; } and it works. Basically what I did was setting the lowest score to the value entered during the first loop, so the subsequent loops have something to compare to. But what can you say about this one's efficiency?

OpenStudy (anonymous):

@shadowfiend: If I want to read in all the values THEN set the lowest to the first value, how can I do everything using no more than 1 loop?

OpenStudy (anonymous):

i think logic z crct......

OpenStudy (shadowfiend):

Jyqft—your approach makes it happen in one loop correctly. What I was suggesting would have taken two loops.

OpenStudy (asnaseer):

Having clear maintainable code is far more important than striving for the most efficient algorithm (in most cases).

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!