0

i'm just starting with getting to know C, and i'm now following the cs50 course. i have a question on the following code.

I want to calculate the avarage score of the user input.

#include <cs50.h>
#include <stdio.h>

int main(void)
{

    int s = get_int("how many scores? ");
    int sum = 0;
    int score[s];
    
    for(int i = 0; i < s; i++)
        {
            score[i] = get_int("score: ");
            sum =  sum + score[i];
        }
    float avg = sum / s;
    printf("avarage: %f\n", avg);
}

So, it prints the avarage, but gets round down to .0000. Is it because i am using a int to divide by? i have tried several things, like changing int to float, but without result. How do I solve this?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • All of the variables involved in the summing and the division are *integers*, and the result will therefore be an integer. If you want a floating-point result, some of the values in the operations must be floating-point as well. – Some programmer dude Aug 25 '22 at 10:21
  • 1
    On another couple of notes: These days there's almost no reason at all to use `float` instead of `double`. And for this simple task there's no need for an array. – Some programmer dude Aug 25 '22 at 10:25
  • Does this answer your question? [How can I force division to be floating point? Division keeps rounding down to 0?](https://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-division-keeps-rounding-down-to-0) – Persixty Aug 25 '22 at 10:35
  • 1
    While there is nothing actually wrong with what you are doing with `score[ s ]`, there really is no need for an array in this example. A single `int score` to receive input, then its value added to the `sum` accumulator.... Less complexity :-) – Fe2O3 Aug 25 '22 at 10:36

2 Answers2

1

This is an integer division:

float avg = sum / s;

Which means that 3 / 2 will be 1 (the decimal part is discarded). This will then be stored in avg as 1.f.

You need to make it into a floating point division. You can cast one of the operands to the desired type:

float avg = (float)sum / s;

Now both operands (sum and s) will be converted to float before the actual division takes place and the correct result will be shown, which is 1.5 + some zeroes in the example above.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

@Ted's answer (integer division) is the reason your results were not as expected.

For your consideration, I've rewritten your code to be more concise. (The less code there is, the easier it can be to read and understand (as you learn more about C.))

#include <cs50.h>
#include <stdio.h>

int main() // 'void' is unnecessary
{
    double sum = 0.0; // make the accumulator floating point

    int s = get_int( "how many scores? " );

    for(int i = 0; i < s; i++) // no need for braces when...
        sum += get_int( "score: " ); // everything happens on one line

    /* Above: you may not have seen "+=" before. Find out. Aids clarity */

    printf("avarage: %lf\n", sum / s ); // float division result is printed.
}

And, the same thing again to compare without comments

#include <cs50.h>
#include <stdio.h>

int main()
{
    double sum = 0.0;

    int s = get_int( "how many scores? " );

    for(int i = 0; i < s; i++)
        sum += get_int( "score: " );

    printf("avarage: %lf\n", sum / s );
}
Fe2O3
  • 6,077
  • 2
  • 4
  • 20