# Just a beginner in C.......very trivial program causing problem.

#### f@ryal

Joined Jul 13, 2011
8
I have written this code to calculate the average score of three students but it is calculating it to be zero.which is wrong.plz chk the code

#include <stdio.h>
#include <stdlib.h>
main()
{
int score[3],sum;
float avg;

printf("Enter First score\n",score[0]);
scanf("%d",&score[0]);

printf("Enter Second score\n",score[1]);
scanf("%d",&score[1]);

printf("Enter Third score\n",score[2]);
scanf("%d",&score[2]);

sum=score[0]+score[1]+score[2];
avg=sum/3;

printf("Sum=%d\n Average=%d\n",sum,avg);
system("PAUSE");
return 0;
}

#### stahta01

Joined Jun 9, 2011
133
Try 3.0 instead of 3; side effect converts result to floating point.
Rich (BB code):
avg=sum/3.0;
Tim S.

Joined Jul 7, 2009
1,583
Please learn to use code tags (read the newbie instructions before posting).

There are a number of possible errors in your code. First, if you're going to calculate floating point results, you should probably use a floating point input array (unless you know the input will always be integers). Second, there's really no need to use an array, as you could just use a single input variable, then add it to the accumulated sum after getting the input from the user. Third, the sum should be a floating point variable. While stahta01's suggestion of using 3.0 will work, it's poor programming practice to depend on implicit conversions -- say what you mean and mean what you say. Your printout spec for the floating point number is a %d, which is for integers. You should be using e.g. %f, %g, or %e.

You should get in the habit of using all the warning features available for your compiler. For example, if you were using gcc, use 'gcc -Wall' to compile your code -- and you'd see four warnings. Then figure out what those warnings mean and what needs to be done to fix them. When I'm boss, I mandate that all warnings in code should be fixed -- or a pragma stuck in with an explanation by the programmer for why it's necessary -- and he/she will still get an argument from me. Even better, a switch in the compiler is used to turn warnings into errors in the makefile. Beginning programmers don't understand this being anal over warnings, so they just have to take it on faith from experienced folks that fixing warnings is almost always a good way to avoid subtle bugs down the road. Once your team spends a few days troubleshooting a subtle bug, you'll become a believer.

Finally, you're making one of the most basic mistakes of virtually every beginning C programmer: using uninitialized variables. This WILL bite you sooner or later, so you should inspect all your code to see that everything is initialized. Again, you don't have the experience yet to understand why this is important, so just take it on faith for now.

I also don't understand why you put that system call in there -- what's its purpose?

#### debjit625

Joined Apr 17, 2010
790
I also don't understand why you put that system call in there -- what's its purpose?
He did that,to stop program execution so that the program does no exit after the last line of code and he could watch the result.

Rich (BB code):
printf("Enter First score\n",score[0]);
What you are doing I have no idea,but if you need to print some variable you need to learn how to format your string...
When you are not using any variable just want to print const string use the "puts()" function.And also look how I declared the main function

Here is what you should do

Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int score[3] = {0};
double avg = 0.0;
int sum = 0;
puts("Enter First score");
scanf("%d",&score[0]);
puts("Enter Second score");
scanf("%d",&score[1]);
puts("Enter Third score");
scanf("%d",&score[2]);
sum=score[0]+score[1]+score[2];
avg=(double)sum/3.0;
printf("Sum=%d\n Average=%.2f\n",sum,avg);
system("PAUSE");
return 0;
}
Good Luck

Joined Jul 7, 2009
1,583
He did that,to stop program execution so that the program does no exit after the last line of code and he could watch the result.
No fooling? There are environments where you can't test this code without that? I've been using a console for so many years I thought that was all there was...

#### DumboFixer

Joined Feb 10, 2009
217
When I'm boss, I mandate that all warnings in code should be fixed -- or a pragma stuck in with an explanation by the programmer for why it's necessary -- and he/she will still get an argument from me.
Wish my previous boss was like you He'd reject code if it had any form of comment in it or variable names longer than 5 characters. His arguement was that good code didn't need documentation. Imagine a 1000 line C++ module without a single comment . Needless to say he and I didn't get along.

#### ErnieM

Joined Apr 24, 2011
8,212
I've played some with console programs under windows. If you just double click the filename in windows explorer then a dos box pops up and closes promptly as the program terminates.

There may be a setting to keep the dos box open, something tells me there is but it has been too long to remember.

If you first open a dos box and type in the program name, it runs and returns you to the prompt when it terminates. Box stays active.

I prefer the latter way myself.

#### debjit625

Joined Apr 17, 2010
790
No fooling? There are environments where you can't test this code without that? I've been using a console for so many years I thought that was all there was...
Yes there are,for example Microsoft Visual Studio.

#### debjit625

Joined Apr 17, 2010
790
I've played some with console programs under windows. If you just double click the filename in windows explorer then a dos box pops up and closes promptly as the program terminates.
Thats very normal.

There may be a setting to keep the dos box open, something tells me there is but it has been too long to remember.
No their is no setting in windows as far I know,unless the programmer does anything to stop the execution.

If you first open a dos box and type in the program name, it runs and returns you to the prompt when it terminates. Box stays active.
I prefer the latter way myself.
Thats what you should do when running console program under Windows.