Convert exam marks to overall gradeUsing bucket sort to rank students based on their marksConvert integers...

How can my powered armor quickly replace its ceramic plates?

What is the use of 'de' preposition in 'Yo voy *de* compras todos los sábados'

When can a QA tester start his job?

What is the standard way to address written statements presented as evidence in a civil case?

Convert exam marks to overall grade

Consequences of lack of rigour

How do I append a character to the end of every line in an Excel cell?

How much mayhem could I cause as a sentient fish?

How should I handle players who ignore the session zero agreement?

What incentives do banks have to gather up loans into pools (backed by Ginnie Mae)and selling them?

Numbers with a minus sign in a matrix not aligned with the numbers without minus sign

Citing paywalled articles accessed via illegal web sharing

Can we harness gravitational potential energy?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

Non-Cancer terminal illness that can affect young (age 10-13) girls?

How can I play a serial killer in a party of good PCs?

Why is it that Bernie Sanders is always called a "socialist"?

Is using an 'empty' metaphor considered bad style?

Why did Luke use his left hand to shoot?

What sets the resolution of an analog resistive sensor?

It took me a lot of time to make this, pls like. (YouTube Comments #1)

Is there a Linux system call to create a “view” of a range of a file?

How does Leonard in "Memento" remember reading and writing?

Can I make estimated tax payments instead of withholding from my paycheck?



Convert exam marks to overall grade


Using bucket sort to rank students based on their marksConvert integers into binaryConvert hours and minutes to total minutesRandom exam generatorCompute a students grade based on weight of assignmentsJavaScript live grade CalculatorSimple average grade calculatorGrade calculator for a project with four aspectsQuiz app with Practice and Exam modesConvert decimal to binary













1












$begingroup$


I have started working on the project below here. I want to practice my C coding skills. I would love any constructive criticism I can get!



It is a grading program: given a set of subject marks, it converts to an alphabetic grade letter.



#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[100];
int physics,chemistry,maths,biology,business,total_marks;
// physics,chemistry,maths,biology,business are just but subjects names.
float mean;
printf("Please enter your name n");
fgets(name,100,stdin);
{
printf("Please enter the marks for physics n");
scanf("%d",&physics);
printf("Please enter the marks for chemistry n");
scanf("%d",&chemistry);
printf("Please enter the marks for maths n");
scanf("%d",&maths);
printf("Please enter the marks for biology n");
scanf("%d",&biology);
printf("Please enter the marks for businessn");
scanf("%d",&business);
total_marks=physics+chemistry+maths+biology+business;
mean= (float)total_marks/5;}
printf("%st",name);
printf(" HAS TOTAL MARKS OF : %d,t",total_marks);
printf(" MEAN OF : %.2ft",mean, round(mean) );
if (mean>=70)
printf("AND THE GRADE IS A n");
if(mean>=60 && mean<69)
printf("AND THE GRADE IS B n");
if(mean>=50 && mean<59)
printf("AND THE GRADE IS C n");
if(mean>=40 && mean<49)
printf("AND THE GRADE IS D n");
if (mean<40)
printf("AND THE GRADE IS E n");
printf(" Thank you for your time n");
return 0;
}









share|improve this question









New contributor




O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    I have started working on the project below here. I want to practice my C coding skills. I would love any constructive criticism I can get!



    It is a grading program: given a set of subject marks, it converts to an alphabetic grade letter.



    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    char name[100];
    int physics,chemistry,maths,biology,business,total_marks;
    // physics,chemistry,maths,biology,business are just but subjects names.
    float mean;
    printf("Please enter your name n");
    fgets(name,100,stdin);
    {
    printf("Please enter the marks for physics n");
    scanf("%d",&physics);
    printf("Please enter the marks for chemistry n");
    scanf("%d",&chemistry);
    printf("Please enter the marks for maths n");
    scanf("%d",&maths);
    printf("Please enter the marks for biology n");
    scanf("%d",&biology);
    printf("Please enter the marks for businessn");
    scanf("%d",&business);
    total_marks=physics+chemistry+maths+biology+business;
    mean= (float)total_marks/5;}
    printf("%st",name);
    printf(" HAS TOTAL MARKS OF : %d,t",total_marks);
    printf(" MEAN OF : %.2ft",mean, round(mean) );
    if (mean>=70)
    printf("AND THE GRADE IS A n");
    if(mean>=60 && mean<69)
    printf("AND THE GRADE IS B n");
    if(mean>=50 && mean<59)
    printf("AND THE GRADE IS C n");
    if(mean>=40 && mean<49)
    printf("AND THE GRADE IS D n");
    if (mean<40)
    printf("AND THE GRADE IS E n");
    printf(" Thank you for your time n");
    return 0;
    }









    share|improve this question









    New contributor




    O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1


      1



      $begingroup$


      I have started working on the project below here. I want to practice my C coding skills. I would love any constructive criticism I can get!



      It is a grading program: given a set of subject marks, it converts to an alphabetic grade letter.



      #include <stdio.h>
      #include <stdlib.h>
      int main()
      {
      char name[100];
      int physics,chemistry,maths,biology,business,total_marks;
      // physics,chemistry,maths,biology,business are just but subjects names.
      float mean;
      printf("Please enter your name n");
      fgets(name,100,stdin);
      {
      printf("Please enter the marks for physics n");
      scanf("%d",&physics);
      printf("Please enter the marks for chemistry n");
      scanf("%d",&chemistry);
      printf("Please enter the marks for maths n");
      scanf("%d",&maths);
      printf("Please enter the marks for biology n");
      scanf("%d",&biology);
      printf("Please enter the marks for businessn");
      scanf("%d",&business);
      total_marks=physics+chemistry+maths+biology+business;
      mean= (float)total_marks/5;}
      printf("%st",name);
      printf(" HAS TOTAL MARKS OF : %d,t",total_marks);
      printf(" MEAN OF : %.2ft",mean, round(mean) );
      if (mean>=70)
      printf("AND THE GRADE IS A n");
      if(mean>=60 && mean<69)
      printf("AND THE GRADE IS B n");
      if(mean>=50 && mean<59)
      printf("AND THE GRADE IS C n");
      if(mean>=40 && mean<49)
      printf("AND THE GRADE IS D n");
      if (mean<40)
      printf("AND THE GRADE IS E n");
      printf(" Thank you for your time n");
      return 0;
      }









      share|improve this question









      New contributor




      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have started working on the project below here. I want to practice my C coding skills. I would love any constructive criticism I can get!



      It is a grading program: given a set of subject marks, it converts to an alphabetic grade letter.



      #include <stdio.h>
      #include <stdlib.h>
      int main()
      {
      char name[100];
      int physics,chemistry,maths,biology,business,total_marks;
      // physics,chemistry,maths,biology,business are just but subjects names.
      float mean;
      printf("Please enter your name n");
      fgets(name,100,stdin);
      {
      printf("Please enter the marks for physics n");
      scanf("%d",&physics);
      printf("Please enter the marks for chemistry n");
      scanf("%d",&chemistry);
      printf("Please enter the marks for maths n");
      scanf("%d",&maths);
      printf("Please enter the marks for biology n");
      scanf("%d",&biology);
      printf("Please enter the marks for businessn");
      scanf("%d",&business);
      total_marks=physics+chemistry+maths+biology+business;
      mean= (float)total_marks/5;}
      printf("%st",name);
      printf(" HAS TOTAL MARKS OF : %d,t",total_marks);
      printf(" MEAN OF : %.2ft",mean, round(mean) );
      if (mean>=70)
      printf("AND THE GRADE IS A n");
      if(mean>=60 && mean<69)
      printf("AND THE GRADE IS B n");
      if(mean>=50 && mean<59)
      printf("AND THE GRADE IS C n");
      if(mean>=40 && mean<49)
      printf("AND THE GRADE IS D n");
      if (mean<40)
      printf("AND THE GRADE IS E n");
      printf(" Thank you for your time n");
      return 0;
      }






      beginner c






      share|improve this question









      New contributor




      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 5 hours ago









      Toby Speight

      25k741115




      25k741115






      New contributor




      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      O.MukhtarO.Mukhtar

      133




      133




      New contributor




      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      O.Mukhtar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          3












          $begingroup$

          The first thing you should do is to let your editor or your IDE format the source code. Right now it looks confusing because the lines are not properly indented. I ran GNU Indent with the -kr option on your code:



          indent -kr marks.c


          This is the result:



          #include <stdio.h>
          #include <stdlib.h>
          int main()
          {
          char name[100];
          int physics, chemistry, maths, biology, business, total_marks;
          // physics,chemistry,maths,biology,business are just but subjects names.
          float mean;
          printf("Please enter your name n");
          fgets(name, 100, stdin);
          {
          printf("Please enter the marks for physics n");
          scanf("%d", &physics);
          printf("Please enter the marks for chemistry n");
          scanf("%d", &chemistry);
          printf("Please enter the marks for maths n");
          scanf("%d", &maths);
          printf("Please enter the marks for biology n");
          scanf("%d", &biology);
          printf("Please enter the marks for businessn");
          scanf("%d", &business);
          total_marks = physics + chemistry + maths + biology + business;
          mean = (float) total_marks / 5;
          }
          printf("%st", name);
          printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
          printf(" MEAN OF : %.2ft", mean, round(mean));
          if (mean >= 70)
          printf("AND THE GRADE IS A n");
          if (mean >= 60 && mean < 69)
          printf("AND THE GRADE IS B n");
          if (mean >= 50 && mean < 59)
          printf("AND THE GRADE IS C n");
          if (mean >= 40 && mean < 49)
          printf("AND THE GRADE IS D n");
          if (mean < 40)
          printf("AND THE GRADE IS E n");
          printf(" Thank you for your time n");
          return 0;
          }


          Now the code structure is a little clearer than before.



          The next thing I noticed is that your code does not have a single empty line. This always feels to me as if youwerewritingyourcodewithoutanyspaces, and this also makes it unnecessarily hard to see the building blocks of the code. I added the empty lines manually since I don't know any program that could automatically do this. Now your code is:



          #include <stdio.h>
          #include <stdlib.h>

          int main()
          {
          char name[100];
          int physics, chemistry, maths, biology, business, total_marks;
          // physics,chemistry,maths,biology,business are just but subjects names.
          float mean;

          printf("Please enter your name n");
          fgets(name, 100, stdin);

          printf("Please enter the marks for physics n");
          scanf("%d", &physics);
          printf("Please enter the marks for chemistry n");
          scanf("%d", &chemistry);
          printf("Please enter the marks for maths n");
          scanf("%d", &maths);
          printf("Please enter the marks for biology n");
          scanf("%d", &biology);
          printf("Please enter the marks for businessn");
          scanf("%d", &business);

          total_marks = physics + chemistry + maths + biology + business;
          mean = (float) total_marks / 5;

          printf("%st", name);
          printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
          printf(" MEAN OF : %.2ft", mean, round(mean));

          if (mean >= 70)
          printf("AND THE GRADE IS A n");
          if (mean >= 60 && mean < 69)
          printf("AND THE GRADE IS B n");
          if (mean >= 50 && mean < 59)
          printf("AND THE GRADE IS C n");
          if (mean >= 40 && mean < 49)
          printf("AND THE GRADE IS D n");
          if (mean < 40)
          printf("AND THE GRADE IS E n");

          printf(" Thank you for your time n");
          return 0;
          }


          At this stage the code is ready to be read by a human. Before it just looked like a mess.



          One thing I already did in the previous step is to remove the { and } around the subjects input block. They didn't serve any purpose.



          In the first block of the main function, you only declare variables but don't use them. This is dangerous since in C it is a silent error to declare a variable, not initialize it and then read its value. This leads to undefined behavior. Therefore it's better to combine the variable declaration with its first assignment. Instead of this:



          float mean;
          // ... some 20 lines of unrelated code ...
          mean = (float) sum / n;


          Better combine these lines:



          float mean = (float) sum / n;


          In the 1990s, only the first variant of the code was allowed. But that time has long since passed, and there's no reason anymore to declare variables at the top of the function. Instead, declare them right when they are needed.



          The strings you print typically end in " n". There's no point in writing a trailing space character before a line break. It won't be visible anyway, so you can simply write "n" instead. Even worse, after business you didn't add a space. Whatever you do in programming, be consistent. Either write the spaces nowhere or everywhere. In this case nowhere.



          WHY DO YOU MAKE YOUR PROGRAM SHOUT SO LOUD? There's no reason to use all-uppercase text in your program. It hurts both the ears and the eyes of the readers of the code.



          The round function is only guaranteed to be available in your program if you #include <math.h> at the top.



          There's a bug in the program. When the mean grade is 69, it is neither >= 70 nor < 69, therefore none of the result sentences is printed. Instead of the if-then you should use if-then-else-if-then-else-if-else instead:



          if (mean >= 70)
          printf("AND THE GRADE IS A n");
          else if (mean >= 60)
          printf("AND THE GRADE IS B n");
          else if (mean >= 50)
          printf("AND THE GRADE IS C n");
          else if (mean >= 40)
          printf("AND THE GRADE IS D n");
          else
          printf("AND THE GRADE IS E n");


          This is much clearer and is guaranteed to cover all cases. Again, you have trailing spaces here. Just remove them. What does the double space between THE GRADE mean? If there's no meaning to it, just use a single space.



          One last point is the error handling. Whenever you call a function like fgets or scanf, you must check its return value, like this:



          if (fgets(name, 100, stdin) == NULL)
          return 0;


          This can happen if the user presses Ctrl+D (on Linux, macOS or any other UNIX-like operating system) or Ctrl+Z (on Windows) and thereby terminates the input. For scanf this error checking is even more important. What if the user enters dgfhtzhrdghfnjfthfd instead of a number? Therefore:



          if (scanf("%d", &physics) != 1) {
          fprintf(stderr, "error: physics mark must be a numbern");
          return EXIT_FAILURE;
          }


          That's all for now.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
            $endgroup$
            – O.Mukhtar
            4 hours ago





















          2












          $begingroup$

          Formatting - if the code looks the same in your editor as it does here, you could really do with an automatic indentation tool (many editors have that built-in, or you could use GNU Indent, for instance).



          I'll assume that the inconsistent indentation is due to the way you've copied your code into the site; a good way to avoid problems is to consistently use either spaces or tabs for indentation, but never to mix the two.





          It's a good idea to specify that main() takes no arguments:



          int main(void)




          When we read input, we must check whether it was successful or not:



          int physics;
          if (scanf("%d", &physics) == 1) {
          // physics has been set, and we can safely use it
          } else {
          // physics is still uninitialised
          }


          If input fails, then we might want to re-ask (but be careful about doing this if we reach end of the input stream, or we'll try and try again, indefinitely).



          Output can also fail, but for this application, that's less of a concern.





          There's an unused argument to printf() here:




          printf(" MEAN OF :  %.2ft",mean, round(mean) );



          That suggests that you could enable more compiler warnings (e.g. gcc -Wall -Wextra - other compilers should have similar options).





          If mean is between (say) 59.0 and 60.0, then it will fall in between the grade boundaries, and no grade will be printed. The easy way to avoid this is to use else if:



          if (mean >= 70) {
          puts("AND THE GRADE IS A");
          } else if (mean >= 60) {
          puts("AND THE GRADE IS B");
          } else if (mean >= 50) {
          puts("AND THE GRADE IS C");
          } else if (mean >= 40) {
          puts("AND THE GRADE IS D");
          } else {
          puts("AND THE GRADE IS E");
          }


          Now, exactly one of those blocks will be executed.



          I also changed the printf() to puts() there - prefer the simpler function when you just need to print a single string and a newline.





          More advanced ideas



          Should the grade thresholds be fixed in the code? If they change, you need to re-compile the program. Think about how you might read them from a configuration file.



          Should the list of subjects be fixed? How would you make the program more flexible for students of different courses?






          share|improve this answer











          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            O.Mukhtar is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214421%2fconvert-exam-marks-to-overall-grade%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3












            $begingroup$

            The first thing you should do is to let your editor or your IDE format the source code. Right now it looks confusing because the lines are not properly indented. I ran GNU Indent with the -kr option on your code:



            indent -kr marks.c


            This is the result:



            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;
            printf("Please enter your name n");
            fgets(name, 100, stdin);
            {
            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);
            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;
            }
            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));
            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");
            printf(" Thank you for your time n");
            return 0;
            }


            Now the code structure is a little clearer than before.



            The next thing I noticed is that your code does not have a single empty line. This always feels to me as if youwerewritingyourcodewithoutanyspaces, and this also makes it unnecessarily hard to see the building blocks of the code. I added the empty lines manually since I don't know any program that could automatically do this. Now your code is:



            #include <stdio.h>
            #include <stdlib.h>

            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;

            printf("Please enter your name n");
            fgets(name, 100, stdin);

            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);

            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;

            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));

            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");

            printf(" Thank you for your time n");
            return 0;
            }


            At this stage the code is ready to be read by a human. Before it just looked like a mess.



            One thing I already did in the previous step is to remove the { and } around the subjects input block. They didn't serve any purpose.



            In the first block of the main function, you only declare variables but don't use them. This is dangerous since in C it is a silent error to declare a variable, not initialize it and then read its value. This leads to undefined behavior. Therefore it's better to combine the variable declaration with its first assignment. Instead of this:



            float mean;
            // ... some 20 lines of unrelated code ...
            mean = (float) sum / n;


            Better combine these lines:



            float mean = (float) sum / n;


            In the 1990s, only the first variant of the code was allowed. But that time has long since passed, and there's no reason anymore to declare variables at the top of the function. Instead, declare them right when they are needed.



            The strings you print typically end in " n". There's no point in writing a trailing space character before a line break. It won't be visible anyway, so you can simply write "n" instead. Even worse, after business you didn't add a space. Whatever you do in programming, be consistent. Either write the spaces nowhere or everywhere. In this case nowhere.



            WHY DO YOU MAKE YOUR PROGRAM SHOUT SO LOUD? There's no reason to use all-uppercase text in your program. It hurts both the ears and the eyes of the readers of the code.



            The round function is only guaranteed to be available in your program if you #include <math.h> at the top.



            There's a bug in the program. When the mean grade is 69, it is neither >= 70 nor < 69, therefore none of the result sentences is printed. Instead of the if-then you should use if-then-else-if-then-else-if-else instead:



            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            else if (mean >= 60)
            printf("AND THE GRADE IS B n");
            else if (mean >= 50)
            printf("AND THE GRADE IS C n");
            else if (mean >= 40)
            printf("AND THE GRADE IS D n");
            else
            printf("AND THE GRADE IS E n");


            This is much clearer and is guaranteed to cover all cases. Again, you have trailing spaces here. Just remove them. What does the double space between THE GRADE mean? If there's no meaning to it, just use a single space.



            One last point is the error handling. Whenever you call a function like fgets or scanf, you must check its return value, like this:



            if (fgets(name, 100, stdin) == NULL)
            return 0;


            This can happen if the user presses Ctrl+D (on Linux, macOS or any other UNIX-like operating system) or Ctrl+Z (on Windows) and thereby terminates the input. For scanf this error checking is even more important. What if the user enters dgfhtzhrdghfnjfthfd instead of a number? Therefore:



            if (scanf("%d", &physics) != 1) {
            fprintf(stderr, "error: physics mark must be a numbern");
            return EXIT_FAILURE;
            }


            That's all for now.






            share|improve this answer









            $endgroup$













            • $begingroup$
              Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
              $endgroup$
              – O.Mukhtar
              4 hours ago


















            3












            $begingroup$

            The first thing you should do is to let your editor or your IDE format the source code. Right now it looks confusing because the lines are not properly indented. I ran GNU Indent with the -kr option on your code:



            indent -kr marks.c


            This is the result:



            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;
            printf("Please enter your name n");
            fgets(name, 100, stdin);
            {
            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);
            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;
            }
            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));
            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");
            printf(" Thank you for your time n");
            return 0;
            }


            Now the code structure is a little clearer than before.



            The next thing I noticed is that your code does not have a single empty line. This always feels to me as if youwerewritingyourcodewithoutanyspaces, and this also makes it unnecessarily hard to see the building blocks of the code. I added the empty lines manually since I don't know any program that could automatically do this. Now your code is:



            #include <stdio.h>
            #include <stdlib.h>

            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;

            printf("Please enter your name n");
            fgets(name, 100, stdin);

            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);

            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;

            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));

            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");

            printf(" Thank you for your time n");
            return 0;
            }


            At this stage the code is ready to be read by a human. Before it just looked like a mess.



            One thing I already did in the previous step is to remove the { and } around the subjects input block. They didn't serve any purpose.



            In the first block of the main function, you only declare variables but don't use them. This is dangerous since in C it is a silent error to declare a variable, not initialize it and then read its value. This leads to undefined behavior. Therefore it's better to combine the variable declaration with its first assignment. Instead of this:



            float mean;
            // ... some 20 lines of unrelated code ...
            mean = (float) sum / n;


            Better combine these lines:



            float mean = (float) sum / n;


            In the 1990s, only the first variant of the code was allowed. But that time has long since passed, and there's no reason anymore to declare variables at the top of the function. Instead, declare them right when they are needed.



            The strings you print typically end in " n". There's no point in writing a trailing space character before a line break. It won't be visible anyway, so you can simply write "n" instead. Even worse, after business you didn't add a space. Whatever you do in programming, be consistent. Either write the spaces nowhere or everywhere. In this case nowhere.



            WHY DO YOU MAKE YOUR PROGRAM SHOUT SO LOUD? There's no reason to use all-uppercase text in your program. It hurts both the ears and the eyes of the readers of the code.



            The round function is only guaranteed to be available in your program if you #include <math.h> at the top.



            There's a bug in the program. When the mean grade is 69, it is neither >= 70 nor < 69, therefore none of the result sentences is printed. Instead of the if-then you should use if-then-else-if-then-else-if-else instead:



            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            else if (mean >= 60)
            printf("AND THE GRADE IS B n");
            else if (mean >= 50)
            printf("AND THE GRADE IS C n");
            else if (mean >= 40)
            printf("AND THE GRADE IS D n");
            else
            printf("AND THE GRADE IS E n");


            This is much clearer and is guaranteed to cover all cases. Again, you have trailing spaces here. Just remove them. What does the double space between THE GRADE mean? If there's no meaning to it, just use a single space.



            One last point is the error handling. Whenever you call a function like fgets or scanf, you must check its return value, like this:



            if (fgets(name, 100, stdin) == NULL)
            return 0;


            This can happen if the user presses Ctrl+D (on Linux, macOS or any other UNIX-like operating system) or Ctrl+Z (on Windows) and thereby terminates the input. For scanf this error checking is even more important. What if the user enters dgfhtzhrdghfnjfthfd instead of a number? Therefore:



            if (scanf("%d", &physics) != 1) {
            fprintf(stderr, "error: physics mark must be a numbern");
            return EXIT_FAILURE;
            }


            That's all for now.






            share|improve this answer









            $endgroup$













            • $begingroup$
              Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
              $endgroup$
              – O.Mukhtar
              4 hours ago
















            3












            3








            3





            $begingroup$

            The first thing you should do is to let your editor or your IDE format the source code. Right now it looks confusing because the lines are not properly indented. I ran GNU Indent with the -kr option on your code:



            indent -kr marks.c


            This is the result:



            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;
            printf("Please enter your name n");
            fgets(name, 100, stdin);
            {
            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);
            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;
            }
            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));
            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");
            printf(" Thank you for your time n");
            return 0;
            }


            Now the code structure is a little clearer than before.



            The next thing I noticed is that your code does not have a single empty line. This always feels to me as if youwerewritingyourcodewithoutanyspaces, and this also makes it unnecessarily hard to see the building blocks of the code. I added the empty lines manually since I don't know any program that could automatically do this. Now your code is:



            #include <stdio.h>
            #include <stdlib.h>

            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;

            printf("Please enter your name n");
            fgets(name, 100, stdin);

            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);

            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;

            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));

            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");

            printf(" Thank you for your time n");
            return 0;
            }


            At this stage the code is ready to be read by a human. Before it just looked like a mess.



            One thing I already did in the previous step is to remove the { and } around the subjects input block. They didn't serve any purpose.



            In the first block of the main function, you only declare variables but don't use them. This is dangerous since in C it is a silent error to declare a variable, not initialize it and then read its value. This leads to undefined behavior. Therefore it's better to combine the variable declaration with its first assignment. Instead of this:



            float mean;
            // ... some 20 lines of unrelated code ...
            mean = (float) sum / n;


            Better combine these lines:



            float mean = (float) sum / n;


            In the 1990s, only the first variant of the code was allowed. But that time has long since passed, and there's no reason anymore to declare variables at the top of the function. Instead, declare them right when they are needed.



            The strings you print typically end in " n". There's no point in writing a trailing space character before a line break. It won't be visible anyway, so you can simply write "n" instead. Even worse, after business you didn't add a space. Whatever you do in programming, be consistent. Either write the spaces nowhere or everywhere. In this case nowhere.



            WHY DO YOU MAKE YOUR PROGRAM SHOUT SO LOUD? There's no reason to use all-uppercase text in your program. It hurts both the ears and the eyes of the readers of the code.



            The round function is only guaranteed to be available in your program if you #include <math.h> at the top.



            There's a bug in the program. When the mean grade is 69, it is neither >= 70 nor < 69, therefore none of the result sentences is printed. Instead of the if-then you should use if-then-else-if-then-else-if-else instead:



            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            else if (mean >= 60)
            printf("AND THE GRADE IS B n");
            else if (mean >= 50)
            printf("AND THE GRADE IS C n");
            else if (mean >= 40)
            printf("AND THE GRADE IS D n");
            else
            printf("AND THE GRADE IS E n");


            This is much clearer and is guaranteed to cover all cases. Again, you have trailing spaces here. Just remove them. What does the double space between THE GRADE mean? If there's no meaning to it, just use a single space.



            One last point is the error handling. Whenever you call a function like fgets or scanf, you must check its return value, like this:



            if (fgets(name, 100, stdin) == NULL)
            return 0;


            This can happen if the user presses Ctrl+D (on Linux, macOS or any other UNIX-like operating system) or Ctrl+Z (on Windows) and thereby terminates the input. For scanf this error checking is even more important. What if the user enters dgfhtzhrdghfnjfthfd instead of a number? Therefore:



            if (scanf("%d", &physics) != 1) {
            fprintf(stderr, "error: physics mark must be a numbern");
            return EXIT_FAILURE;
            }


            That's all for now.






            share|improve this answer









            $endgroup$



            The first thing you should do is to let your editor or your IDE format the source code. Right now it looks confusing because the lines are not properly indented. I ran GNU Indent with the -kr option on your code:



            indent -kr marks.c


            This is the result:



            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;
            printf("Please enter your name n");
            fgets(name, 100, stdin);
            {
            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);
            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;
            }
            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));
            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");
            printf(" Thank you for your time n");
            return 0;
            }


            Now the code structure is a little clearer than before.



            The next thing I noticed is that your code does not have a single empty line. This always feels to me as if youwerewritingyourcodewithoutanyspaces, and this also makes it unnecessarily hard to see the building blocks of the code. I added the empty lines manually since I don't know any program that could automatically do this. Now your code is:



            #include <stdio.h>
            #include <stdlib.h>

            int main()
            {
            char name[100];
            int physics, chemistry, maths, biology, business, total_marks;
            // physics,chemistry,maths,biology,business are just but subjects names.
            float mean;

            printf("Please enter your name n");
            fgets(name, 100, stdin);

            printf("Please enter the marks for physics n");
            scanf("%d", &physics);
            printf("Please enter the marks for chemistry n");
            scanf("%d", &chemistry);
            printf("Please enter the marks for maths n");
            scanf("%d", &maths);
            printf("Please enter the marks for biology n");
            scanf("%d", &biology);
            printf("Please enter the marks for businessn");
            scanf("%d", &business);

            total_marks = physics + chemistry + maths + biology + business;
            mean = (float) total_marks / 5;

            printf("%st", name);
            printf(" HAS TOTAL MARKS OF : %d,t", total_marks);
            printf(" MEAN OF : %.2ft", mean, round(mean));

            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            if (mean >= 60 && mean < 69)
            printf("AND THE GRADE IS B n");
            if (mean >= 50 && mean < 59)
            printf("AND THE GRADE IS C n");
            if (mean >= 40 && mean < 49)
            printf("AND THE GRADE IS D n");
            if (mean < 40)
            printf("AND THE GRADE IS E n");

            printf(" Thank you for your time n");
            return 0;
            }


            At this stage the code is ready to be read by a human. Before it just looked like a mess.



            One thing I already did in the previous step is to remove the { and } around the subjects input block. They didn't serve any purpose.



            In the first block of the main function, you only declare variables but don't use them. This is dangerous since in C it is a silent error to declare a variable, not initialize it and then read its value. This leads to undefined behavior. Therefore it's better to combine the variable declaration with its first assignment. Instead of this:



            float mean;
            // ... some 20 lines of unrelated code ...
            mean = (float) sum / n;


            Better combine these lines:



            float mean = (float) sum / n;


            In the 1990s, only the first variant of the code was allowed. But that time has long since passed, and there's no reason anymore to declare variables at the top of the function. Instead, declare them right when they are needed.



            The strings you print typically end in " n". There's no point in writing a trailing space character before a line break. It won't be visible anyway, so you can simply write "n" instead. Even worse, after business you didn't add a space. Whatever you do in programming, be consistent. Either write the spaces nowhere or everywhere. In this case nowhere.



            WHY DO YOU MAKE YOUR PROGRAM SHOUT SO LOUD? There's no reason to use all-uppercase text in your program. It hurts both the ears and the eyes of the readers of the code.



            The round function is only guaranteed to be available in your program if you #include <math.h> at the top.



            There's a bug in the program. When the mean grade is 69, it is neither >= 70 nor < 69, therefore none of the result sentences is printed. Instead of the if-then you should use if-then-else-if-then-else-if-else instead:



            if (mean >= 70)
            printf("AND THE GRADE IS A n");
            else if (mean >= 60)
            printf("AND THE GRADE IS B n");
            else if (mean >= 50)
            printf("AND THE GRADE IS C n");
            else if (mean >= 40)
            printf("AND THE GRADE IS D n");
            else
            printf("AND THE GRADE IS E n");


            This is much clearer and is guaranteed to cover all cases. Again, you have trailing spaces here. Just remove them. What does the double space between THE GRADE mean? If there's no meaning to it, just use a single space.



            One last point is the error handling. Whenever you call a function like fgets or scanf, you must check its return value, like this:



            if (fgets(name, 100, stdin) == NULL)
            return 0;


            This can happen if the user presses Ctrl+D (on Linux, macOS or any other UNIX-like operating system) or Ctrl+Z (on Windows) and thereby terminates the input. For scanf this error checking is even more important. What if the user enters dgfhtzhrdghfnjfthfd instead of a number? Therefore:



            if (scanf("%d", &physics) != 1) {
            fprintf(stderr, "error: physics mark must be a numbern");
            return EXIT_FAILURE;
            }


            That's all for now.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 5 hours ago









            Roland IlligRoland Illig

            11.2k11844




            11.2k11844












            • $begingroup$
              Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
              $endgroup$
              – O.Mukhtar
              4 hours ago




















            • $begingroup$
              Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
              $endgroup$
              – O.Mukhtar
              4 hours ago


















            $begingroup$
            Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
            $endgroup$
            – O.Mukhtar
            4 hours ago






            $begingroup$
            Sorry I did not get your last point clearly. It would really help me if you show me one and final code with the above mistakes corrected.
            $endgroup$
            – O.Mukhtar
            4 hours ago















            2












            $begingroup$

            Formatting - if the code looks the same in your editor as it does here, you could really do with an automatic indentation tool (many editors have that built-in, or you could use GNU Indent, for instance).



            I'll assume that the inconsistent indentation is due to the way you've copied your code into the site; a good way to avoid problems is to consistently use either spaces or tabs for indentation, but never to mix the two.





            It's a good idea to specify that main() takes no arguments:



            int main(void)




            When we read input, we must check whether it was successful or not:



            int physics;
            if (scanf("%d", &physics) == 1) {
            // physics has been set, and we can safely use it
            } else {
            // physics is still uninitialised
            }


            If input fails, then we might want to re-ask (but be careful about doing this if we reach end of the input stream, or we'll try and try again, indefinitely).



            Output can also fail, but for this application, that's less of a concern.





            There's an unused argument to printf() here:




            printf(" MEAN OF :  %.2ft",mean, round(mean) );



            That suggests that you could enable more compiler warnings (e.g. gcc -Wall -Wextra - other compilers should have similar options).





            If mean is between (say) 59.0 and 60.0, then it will fall in between the grade boundaries, and no grade will be printed. The easy way to avoid this is to use else if:



            if (mean >= 70) {
            puts("AND THE GRADE IS A");
            } else if (mean >= 60) {
            puts("AND THE GRADE IS B");
            } else if (mean >= 50) {
            puts("AND THE GRADE IS C");
            } else if (mean >= 40) {
            puts("AND THE GRADE IS D");
            } else {
            puts("AND THE GRADE IS E");
            }


            Now, exactly one of those blocks will be executed.



            I also changed the printf() to puts() there - prefer the simpler function when you just need to print a single string and a newline.





            More advanced ideas



            Should the grade thresholds be fixed in the code? If they change, you need to re-compile the program. Think about how you might read them from a configuration file.



            Should the list of subjects be fixed? How would you make the program more flexible for students of different courses?






            share|improve this answer











            $endgroup$


















              2












              $begingroup$

              Formatting - if the code looks the same in your editor as it does here, you could really do with an automatic indentation tool (many editors have that built-in, or you could use GNU Indent, for instance).



              I'll assume that the inconsistent indentation is due to the way you've copied your code into the site; a good way to avoid problems is to consistently use either spaces or tabs for indentation, but never to mix the two.





              It's a good idea to specify that main() takes no arguments:



              int main(void)




              When we read input, we must check whether it was successful or not:



              int physics;
              if (scanf("%d", &physics) == 1) {
              // physics has been set, and we can safely use it
              } else {
              // physics is still uninitialised
              }


              If input fails, then we might want to re-ask (but be careful about doing this if we reach end of the input stream, or we'll try and try again, indefinitely).



              Output can also fail, but for this application, that's less of a concern.





              There's an unused argument to printf() here:




              printf(" MEAN OF :  %.2ft",mean, round(mean) );



              That suggests that you could enable more compiler warnings (e.g. gcc -Wall -Wextra - other compilers should have similar options).





              If mean is between (say) 59.0 and 60.0, then it will fall in between the grade boundaries, and no grade will be printed. The easy way to avoid this is to use else if:



              if (mean >= 70) {
              puts("AND THE GRADE IS A");
              } else if (mean >= 60) {
              puts("AND THE GRADE IS B");
              } else if (mean >= 50) {
              puts("AND THE GRADE IS C");
              } else if (mean >= 40) {
              puts("AND THE GRADE IS D");
              } else {
              puts("AND THE GRADE IS E");
              }


              Now, exactly one of those blocks will be executed.



              I also changed the printf() to puts() there - prefer the simpler function when you just need to print a single string and a newline.





              More advanced ideas



              Should the grade thresholds be fixed in the code? If they change, you need to re-compile the program. Think about how you might read them from a configuration file.



              Should the list of subjects be fixed? How would you make the program more flexible for students of different courses?






              share|improve this answer











              $endgroup$
















                2












                2








                2





                $begingroup$

                Formatting - if the code looks the same in your editor as it does here, you could really do with an automatic indentation tool (many editors have that built-in, or you could use GNU Indent, for instance).



                I'll assume that the inconsistent indentation is due to the way you've copied your code into the site; a good way to avoid problems is to consistently use either spaces or tabs for indentation, but never to mix the two.





                It's a good idea to specify that main() takes no arguments:



                int main(void)




                When we read input, we must check whether it was successful or not:



                int physics;
                if (scanf("%d", &physics) == 1) {
                // physics has been set, and we can safely use it
                } else {
                // physics is still uninitialised
                }


                If input fails, then we might want to re-ask (but be careful about doing this if we reach end of the input stream, or we'll try and try again, indefinitely).



                Output can also fail, but for this application, that's less of a concern.





                There's an unused argument to printf() here:




                printf(" MEAN OF :  %.2ft",mean, round(mean) );



                That suggests that you could enable more compiler warnings (e.g. gcc -Wall -Wextra - other compilers should have similar options).





                If mean is between (say) 59.0 and 60.0, then it will fall in between the grade boundaries, and no grade will be printed. The easy way to avoid this is to use else if:



                if (mean >= 70) {
                puts("AND THE GRADE IS A");
                } else if (mean >= 60) {
                puts("AND THE GRADE IS B");
                } else if (mean >= 50) {
                puts("AND THE GRADE IS C");
                } else if (mean >= 40) {
                puts("AND THE GRADE IS D");
                } else {
                puts("AND THE GRADE IS E");
                }


                Now, exactly one of those blocks will be executed.



                I also changed the printf() to puts() there - prefer the simpler function when you just need to print a single string and a newline.





                More advanced ideas



                Should the grade thresholds be fixed in the code? If they change, you need to re-compile the program. Think about how you might read them from a configuration file.



                Should the list of subjects be fixed? How would you make the program more flexible for students of different courses?






                share|improve this answer











                $endgroup$



                Formatting - if the code looks the same in your editor as it does here, you could really do with an automatic indentation tool (many editors have that built-in, or you could use GNU Indent, for instance).



                I'll assume that the inconsistent indentation is due to the way you've copied your code into the site; a good way to avoid problems is to consistently use either spaces or tabs for indentation, but never to mix the two.





                It's a good idea to specify that main() takes no arguments:



                int main(void)




                When we read input, we must check whether it was successful or not:



                int physics;
                if (scanf("%d", &physics) == 1) {
                // physics has been set, and we can safely use it
                } else {
                // physics is still uninitialised
                }


                If input fails, then we might want to re-ask (but be careful about doing this if we reach end of the input stream, or we'll try and try again, indefinitely).



                Output can also fail, but for this application, that's less of a concern.





                There's an unused argument to printf() here:




                printf(" MEAN OF :  %.2ft",mean, round(mean) );



                That suggests that you could enable more compiler warnings (e.g. gcc -Wall -Wextra - other compilers should have similar options).





                If mean is between (say) 59.0 and 60.0, then it will fall in between the grade boundaries, and no grade will be printed. The easy way to avoid this is to use else if:



                if (mean >= 70) {
                puts("AND THE GRADE IS A");
                } else if (mean >= 60) {
                puts("AND THE GRADE IS B");
                } else if (mean >= 50) {
                puts("AND THE GRADE IS C");
                } else if (mean >= 40) {
                puts("AND THE GRADE IS D");
                } else {
                puts("AND THE GRADE IS E");
                }


                Now, exactly one of those blocks will be executed.



                I also changed the printf() to puts() there - prefer the simpler function when you just need to print a single string and a newline.





                More advanced ideas



                Should the grade thresholds be fixed in the code? If they change, you need to re-compile the program. Think about how you might read them from a configuration file.



                Should the list of subjects be fixed? How would you make the program more flexible for students of different courses?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 5 hours ago

























                answered 5 hours ago









                Toby SpeightToby Speight

                25k741115




                25k741115






















                    O.Mukhtar is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    O.Mukhtar is a new contributor. Be nice, and check out our Code of Conduct.













                    O.Mukhtar is a new contributor. Be nice, and check out our Code of Conduct.












                    O.Mukhtar is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214421%2fconvert-exam-marks-to-overall-grade%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Couldn't open a raw socket. Error: Permission denied (13) (nmap)Is it possible to run networking commands...

                    VNC viewer RFB protocol error: bad desktop size 0x0I Cannot Type the Key 'd' (lowercase) in VNC Viewer...

                    Why not use the yoke to control yaw, as well as pitch and roll? Announcing the arrival of...