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

Discussion in 'Programmer's Corner' started by f@ryal, Jul 14, 2011.

  1. f@ryal

    Thread Starter New Member

    Jul 13, 2011
    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>
    int score[3],sum;
    float avg;

    printf("Enter First score\n",score[0]);

    printf("Enter Second score\n",score[1]);

    printf("Enter Third score\n",score[2]);


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

    Thanks in advance:p
  2. stahta01


    Jun 9, 2011
    Try 3.0 instead of 3; side effect converts result to floating point.
    Code ( (Unknown Language)):
    2. avg=sum/[B]3.0[/B];
    Tim S.
  3. someonesdad

    Senior Member

    Jul 7, 2009
    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. :p 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?
    f@ryal likes this.
  4. debjit625

    Well-Known Member

    Apr 17, 2010
    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.

    Code ( (Unknown Language)):
    1. 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

    Code ( (Unknown Language)):
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. int main(void)
    5. {
    6. int score[3] = {0};
    7. double avg = 0.0;
    8. int sum = 0;
    9. puts("Enter First score");
    10. scanf("%d",&score[0]);
    11. puts("Enter Second score");
    12. scanf("%d",&score[1]);
    13. puts("Enter Third score");
    14. scanf("%d",&score[2]);
    15. sum=score[0]+score[1]+score[2];
    16. avg=(double)sum/3.0;
    17. printf("Sum=%d\n Average=%.2f\n",sum,avg);
    18. system("PAUSE");
    19. return 0;
    20. }
    Good Luck
  5. someonesdad

    Senior Member

    Jul 7, 2009
    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... :p
  6. DumboFixer

    Active Member

    Feb 10, 2009
    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 :eek:. Needless to say he and I didn't get along.
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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.
  8. debjit625

    Well-Known Member

    Apr 17, 2010
    Yes there are,for example Microsoft Visual Studio.:)
  9. debjit625

    Well-Known Member

    Apr 17, 2010
    Thats very normal.

    No their is no setting in windows as far I know,unless the programmer does anything to stop the execution.

    Thats what you should do when running console program under Windows.
  10. f@ryal

    Thread Starter New Member

    Jul 13, 2011
    the problem in my code has been solved...by using %f command and 3.0 for average value........thanks for your help.