A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

C Question: What's wrong with my program?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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); }

  2. asnaseer
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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).

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. llib_xoc
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  6. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

    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.

  7. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

    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.

  8. asnaseer
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I totally agree with @shadowfiend's suggestions.

  9. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  10. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @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?

  11. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i think logic z crct......

  12. shadowfiend
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

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

  13. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  14. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.