Convert exam marks to overall grade
$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;
}
beginner c
New contributor
$endgroup$
add a comment |
$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;
}
beginner c
New contributor
$endgroup$
add a comment |
$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;
}
beginner c
New contributor
$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
beginner c
New contributor
New contributor
edited 5 hours ago
Toby Speight
25k741115
25k741115
New contributor
asked 5 hours ago
O.MukhtarO.Mukhtar
133
133
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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.
$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
3 hours ago
add a comment |
$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?
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
$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
3 hours ago
add a comment |
$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.
$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
3 hours ago
add a comment |
$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.
$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.
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
3 hours ago
add a comment |
$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
3 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
3 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
3 hours ago
add a comment |
$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?
$endgroup$
add a comment |
$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?
$endgroup$
add a comment |
$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?
$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?
edited 4 hours ago
answered 5 hours ago
Toby SpeightToby Speight
25k741115
25k741115
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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