patterncModerate
Scoring and grading answers against an answer key
Viewed 0 times
answeranswersgradingscoringagainstandkey
Problem
This is for a university assignment (don't worry the code is in working order), and while I could hand it in as-is and receive full marks I feel like there is probably a more efficient and better way to handle what I am doing.
What the code does
Takes user input of 4 names and for each name take True / False assignment scores in the format of 0 for False and 1 for True; then compare against the user input answer key and give: highest score, number correct for each name, and the letter grade which is based upon the highest score of the 4 names (more about that in the comments of the
Sample Input
Sample Output
Concerned parts
Ultimately, I dislike the amount of
What the code does
Takes user input of 4 names and for each name take True / False assignment scores in the format of 0 for False and 1 for True; then compare against the user input answer key and give: highest score, number correct for each name, and the letter grade which is based upon the highest score of the 4 names (more about that in the comments of the
computeGrade() function)Sample Input
Information for Student #1
Name: Smith
Answers: 0 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 1
Information for Student #2
Name: Tony
Answers: 0 1 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 1
Information for Student #3
Name: Patty
Answers: 1 1 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 1
Information for Student #4
Name: Meg
Answers: 1 0 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0
Please enter the Answer Key: 0 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0
Sample Output
Name Answers Number Correct Grade
--------------------------------------------------------------------------------
Smith 0 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 1 19 A
Tony 0 1 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 1 9 F
Patty 1 1 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 1 8 F
Meg 1 0 1 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 15 B
Answer Key Highest Score
--------------------------------------------------------------------
0 1 0 0 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 19
Concerned parts
Ultimately, I dislike the amount of
for loops I'm using, especially in the void computeGrade() and void printAll() functions, that and I'm not sure if I'm just being OCD or if there's a better way to handle it. Also, I know my `prinSolution
Lint
Some of the more obvious simple things that the compiler didn't complain about:
-
Commenting every line is very distracting, and more harmful than helpful. A lot of the code (e.g.
Your
More importantly, instead of the meaningless
Data structures
First of all, I'm glad to see that you broke down the work into four helper functions. It would be even better if you passed parameters instead of using global variables. Passing arrays in C is somewhat tricky, though, so I wouldn't fault a beginner for avoiding that hurdle.
Have you learned about
As noted in the compiler warnings,
You don't need
Input / output
is problematic. Things that could go wrong:
In that situation, to read a line of text up to a certain length limit, I suggest using
Printing could be better as well. For example, for
Please do not omit "optional" braces like that in your for-loops — you will someday contribute to a coding accident, perhaps a costly one. Just put the braces in, or, if you really feel the urge, write one-liner blocks.
Some of the more obvious simple things that the compiler didn't complain about:
- Why are you hard-coding
4and20all over the place?
- It's conventional to order the functions (i.e.
main()at the bottom) such that you don't need forward declarations.
-
Commenting every line is very distracting, and more harmful than helpful. A lot of the code (e.g.
printf() and scanf()) is obvious, and you're making me read everything twice. Note that // comments were introduced in C99, so your code is neither C89-compliant, nor is it using C99 features, which puts it in a grey zone.Your
/ / comments, on the other hand, are somewhat helpful. Keep those.More importantly, instead of the meaningless
/ Module 1000 / comments, tell us what global variables the function uses. Documenting those dependencies and side-effects is crucial.Data structures
First of all, I'm glad to see that you broke down the work into four helper functions. It would be even better if you passed parameters instead of using global variables. Passing arrays in C is somewhat tricky, though, so I wouldn't fault a beginner for avoiding that hurdle.
Have you learned about
structs yet? If so, I would prefer to see an array of four struct student_records than to have the information dispersed into various four-element arrays.As noted in the compiler warnings,
sGrades[n][1] involves out-of-bounds access. You don't need a char[][1] if all you need to store is one byte — just a one-dimensional char[] will do.You don't need
initialize(). You can just use an initializer. In fact, global variables are automatically initialized to zero. (Better to be safe than sorry, though!)Input / output
scanf() is a tricky function to use correctly. In particular, this linescanf("%s", sNames[i]); // Scan in the students nameis problematic. Things that could go wrong:
- Buffer overflow if the input exceeds 19 bytes.
- The whole program malfunctions if the user enters a first name and a last name.
- You didn't check for end-of-file (CtrlZ on Windows), which would lead to
sNames[i]not being written to at all.
In that situation, to read a line of text up to a certain length limit, I suggest using
fgets() instead.Printing could be better as well. For example, for
/ Print Labels /, I suggestprintf("%-10s%-45s%-20s%-10s\n", "Name", "Answers", "Number Correct", "Grade");printf("%c", '-'); is overkill; putchar('-') will do.Please do not omit "optional" braces like that in your for-loops — you will someday contribute to a coding accident, perhaps a costly one. Just put the braces in, or, if you really feel the urge, write one-liner blocks.
Code Snippets
scanf("%s", sNames[i]); // Scan in the students nameprintf("%-10s%-45s%-20s%-10s\n", "Name", "Answers", "Number Correct", "Grade");Context
StackExchange Code Review Q#110509, answer score: 10
Revisions (0)
No revisions yet.