Grades and Average

Good day programmers!

I've look as much as possible in youtube videos analyzing and on many websites, but i think (in here will be the best) so for my problem here is my code isnt working, i hope you can encourage me with this

#include <stdio.h>
void main ()
{
float n1,n2,n3,n4,n5,sum,avg;

printf("Enter grade for English: ");
scanf("%f" ,&n1);

printf("Enter grade forFilipino: ");
scanf("%f" ,&n2);

printf("Enter grade for Science: ");
scanf("%f" ,&n3);

printf("Enter grade for Math: ");
scanf("%f" ,&n4);

printf("Enter grade for Mapeh: ");
scanf("%f" ,&n5);

sum = n1+n2+n3+n4+n5;
avg = sum/5;

printf("the sum is %d and the average is %d",sum,avg);
}

why is the sum and the average wont appear? as if its missing or the software's just malfunction,
this is the output:

the sum is 0 and the average is 1081757696
printf("the sum is %d and the average is %d",sum,avg);

%d means int but you're passing a float
Hello philipmorries3,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



To go along with what Repeater has said this may help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
	const float GRADES = 5.0;

	float n1=80, n2=81, n3=95, n4=100, n5=95, sum, avg;

	printf("Enter grade for English: %0.0f\n", n1);
	//printf("Enter grade for English: ");
	//scanf("%f", &n1);

        // <--- Rest of input.

	sum = n1 + n2 + n3 + n4 + n5;
	avg = sum / GRADES;

	printf("\n The sum is %0.2f and the average is %0.2f", sum, avg);

	return 0;
}

Unfortunately I was not able to duplicate your problems. and the only problem I can see is the format specifiers in the last "printf" statement.

If you can use void main() you must have a very old compiler. These days int main() is the correct way to write this line. Also I compiled the program as a C++ program with VS2017 and that might make some difference.

The use of "GRADES", (defined as a constant), is helpful and avoids the use of a magic number as in your original calculation for "avg". Also easier to change if you should add or subtract a grade or two. You do not have to hunt through the whole program to find where you would need to make a change. This becomes more apparent when your programs grow to several hundred lines of code.

With setting the "n" variables equal to a number and changing lines 8 - 10 you can skip the input each time the program runs and it gives the advantage of using the same numbers when testing your program.

In line 16 notice the difference in what I did to what you have. Although this did not seem to be a problem for me with what I did, it looks like it is giving you a problem with your compiler and the way you are running the program producing a known output.

I believe if you make the changes it should work better.

Hope that helps,

Andy
Topic archived. No new replies allowed.