C Question: What's wrong with my program?

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

C Question: What's wrong with my program?

Computer Science
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

I can't get it to find the element with the least value in the array. #include 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); }
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).
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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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.
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.
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.
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.
I totally agree with @shadowfiend's suggestions.
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?
@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?
i think logic z crct......
Jyqft—your approach makes it happen in one loop correctly. What I was suggesting would have taken two loops.
Having clear maintainable code is far more important than striving for the most efficient algorithm (in most cases).

Not the answer you are looking for?

Search for more explanations.

Ask your own question