1. ## Check for efficiency

I am new to programming and have to write a program for class that is designed to calculate student averages, class averages, and report letter grades. A sentinel has to be used for assignment 1 score in order to produce the summary report, and if the user inputs an invalid score it has to output an error message and ignore the scores when producing a summary.

The program works just fine, but I was hoping to get some feedback before I turn it in. Obviously it is very basic stuff, but I just want to know if there is anything I could possibly improve on or make more efficient.

Code:
```#include <stdlib.h>
#include <stdio.h>

int main()
{

float assignment_total, test_total, final_exam, final_grade, sum, class_average;
bool A, B, C, D, F;

sum= 0; //sum variable used to determine class average in summary report
count= 1; //count variable used to store number of students' grades entered
//these variables store the amount of different letter grades displayed in the summary report

//intructs the user how to properly use the program
printf ("                          *INSTRUCTIONS FOR USE*\n");
printf ("\n");
printf ("This program is designed to record student grades and report class averages. \n");
printf ("Enter a score between 0-100 for each grade. To calculate the class average entera negative ");
printf ("number for assignment number one.");
printf ("\n");
printf ("\n");
printf ("Please input assignment 1 score then press ENTER: ");
scanf ("%d", &a1);

//if the sentinal is not used for variable a1 then the program will continue to compute the scores.
while (a1 >=0)
{
printf ("Please input assignment 2 score then press ENTER: ");
scanf ("%d", &a2);
printf ("Please input assignment 3 score then press ENTER: ");
scanf ("%d", &a3);
printf ("Please input assignment 4 score then press ENTER: ");
scanf ("%d", &a4);
printf ("Please input assignment 5 score then press ENTER: ");
scanf ("%d", &a5);

printf ("Please input test 1 score then press ENTER: ");
scanf ("%d", &t1);
printf ("Please input test 2 score then press ENTER: ");
scanf ("%d", &t2);

printf ("Please input final exam score then press ENTER: ");
scanf ("%d", &f);
printf ("\n");

//determines if an invalid score was entered, notifies the user, and allows for a correction.
while (a1>100 || a2<0 || a2>100 || a3<0 || a3>100 || a4<0 || a4>100 || a5<0 || a5>100 || t1<0 || t1>100 || t2<0 || t2>100 || f<0 || f>100)
{
printf ("YOU HAVE ENTERED AN INVALID SCORE. CHECK YOUR SCORES AND TRY AGAIN.\n");
printf ("\n");
printf ("\n");

printf ("**IF FINISHED ENTER A NEGATIVE NUMBER FOR ASSIGNMENT 1 AND PRESS ENTER.");

printf ("\n");
printf ("Please input assignment 1 score then press ENTER: ");
scanf ("%d", &a1);

while (a1 <= -1)
{
printf ("\n");
printf ("\n");
printf ("Class Average score is %f\n", class_average);
printf ("\n");
printf ("The program will now close. ");
system("pause");
return 0;
}
printf ("Please input assignment 2 score then press ENTER: ");
scanf ("%d", &a2);
printf ("Please input assignment 3 score then press ENTER: ");
scanf ("%d", &a3);
printf ("Please input assignment 4 score then press ENTER: ");
scanf ("%d", &a4);
printf ("Please input assignment 5 score then press ENTER: ");
scanf ("%d", &a5);

printf ("Please input test 1 score then press ENTER: ");
scanf ("%d", &t1);
printf ("Please input test 2 score then press ENTER: ");
scanf ("%d", &t2);

printf ("Please input final exam score then press ENTER: ");
scanf ("%d", &f);
printf ("\n");
}
//echo printing the scores for verification
printf ("Assignment 1 score is: %d\n", a1);
printf ("Assignment 2 score is: %d\n", a2);
printf ("Assignment 3 score is: %d\n", a3);
printf ("Assignment 4 score is: %d\n", a4);
printf ("Assignment 5 score is: %d\n", a5);
printf ("Test 1 score is: %d\n", t1);
printf ("Test 2 score is %d\n", t2);
printf ("Final Exam score is %d\n", f);
printf ("IF THESE SCORES ARE INCORRECT YOU MUST RESTART THE PROGRAM AND RE-ENTER GRADES!");

//algebra required to compute the scores.
assignment_total= ((a1+a2+a3+a4+a5)/5)*.30;
test_total= ((t1+t2)/2)*.30;
final_exam= (f)*.40;
printf ("\n");
printf ("\n");
class_average= sum/count;

//letter grade ranges are set here

//prints out the letter grade and keeps a running count of how many different letter grades there are.
if (A)
{
printf ("LETTER GRADE for student %d is: A\n", count);
}
else if (B)
{
printf ("LETTER GRADE for student %d is: B\n", count);
}
else if (C)
{
}
else if (D)
{
}
else
{
}
printf ("\n");

system ("pause");
printf ("\n");
printf ("\n");

//changes the student number to the next higher student
count= count+1;

//starts the loop over and allows the user to enter a sentinel number to produce a summary and terminate the program.
printf ("**IF FINISHED ENTER A NEGATIVE NUMBER FOR ASSIGNMENT 1 AND PRESS ENTER.");
printf ("\n");
printf ("Please input assignment 1 score and press ENTER: ");
scanf ("%d", &a1);
}

//summary report produced by entering a negative number for assignment 1.
while (a1 <= -1)
{
printf ("\n");
printf ("Class Average score is %f\n", class_average);
printf ("\n");
printf ("The program will now close. ");
system("pause");
return 0;
}

}```
Thanks in advance for any input.

2. Have you learned about arrays yet? Arrays could definitely help to clean up the code quite a bit.

3. Since this program's for your class, i suggest you don't optimise it. Optimisation sometimes makes the code harder to understand, even for your teacher. ( Good documentation can counter this )

4. About the only thing I see is the lack of meaningful variable names. See this link for an example: Variable Names.

Jim

5. I have read about arrays a little bit in my spare time, but we haven't learned them yet. Just seemed that a while statement within a while statement within another while statement was a bit overkill for what i needed to do. But I am brand freakin new to this so what do I know right? I tried to use good documentation in each section so that people knew what is going on.

6. It's nicely formatted and clear what it's doing - that's the good part. One formatting change I would suggest is to NOT indent the curly braces themselves. Only indent the subordinate lines of code that go between the curly braces:

Code:
```while(something)
{ //<== all the way to the left, under the 'w'
one level of indentation
} //<===brace is all the way to the left

//as opposed to:
while(something)
#####{ # = wasted real estate
#####      one level of indentation
#####}```
You see how both code and comments run out of real estate, on the right hand side of the line, in your code. (It's already breaking the set page width for the forum, for easy viewing). Add on two or three more levels of indentation, and easy viewing will be hard to come by in your code.

The bad part is it's repeating a lot of code, explicitly, "please input assignment #1, please input assignment #2, etc.", that should be done in a more representative, way:
Code:
```for(i=0;i<5;i++) {
}```
That adds a lot of length to the program, and would, in a larger program, be a PITA. You have enough variables in there to float a battleship, I do believe.

Improvement will come with experience, and after you learn more basics in the C language.

Welcome to the forum, RgnWatson!

7. Originally Posted by rgrwatson85
I have read about arrays a little bit in my spare time, but we haven't learned them yet. Just seemed that a while statement within a while statement within another while statement was a bit overkill for what i needed to do. But I am brand freakin new to this so what do I know right? I tried to use good documentation in each section so that people knew what is going on.
It could just be an assignment that's purposely designed to introduce arrays in the next segment. You can almost hear the prof saying, "See how much better this is now that you know about arrays?"

8. I made the changes to the indentations that you suggested Adak. Thanks for that. It looks ten times better. Well better anyway. I talked with my prof today about there being so many while statements in my code and he said that there should only be one while statement. So the two extra while statements got changed to an if statement. I understand that I had to change that because it created a way to exit the entire program while in the loop. Apparently that is a big problem. I cant get a good answer as to why this shouldn't be allowed.

Another thing he pointed out is my use of bool to assign the letter grades. I changed the declaration to "char letter_grade;" I thought my way was just fine but he kept just saying that it is a bad program. Again no real answer as to why it is bad to have it the first way.

The final thing that I had to change was when I checked for a score validity (between 0-100). I couldn't say "if (a1>=0 && a1<=100...)". The directions specifically state that using int/float to check scores is not acceptable. So I made a bool variable called good_scores and set it equal to the valid scores. Why on earth would I have to do that?!

I'm not one to accept the answer "Because I Said So!" from anyone. Anyone care to try and explain the logic behind these changes. The revised code is below.

Code:
```#include <stdlib.h>
#include <stdio.h>

int main()
{

float assignment_total, test_total, final_exam, final_grade, sum, class_average;
bool good_scores;

sum= 0; //sum variable used to determine class average in summary report
count= 1; //count variable used to store number of students' grades entered
//these variables store the amount of different letter grades displayed in the summary report

//intructs the user how to properly use the program
printf ("                          *INSTRUCTIONS FOR USE*\n");
printf ("\n");
printf ("This program is designed to record student grades and report class averages. \n");
printf ("Enter a score between 0-100 for each grade. To calculate the class average entera negative ");
printf ("number for assignment number one.");
printf ("\n");
printf ("\n");
printf ("Please input assignment 1 score then press ENTER: ");
scanf ("%d", &a1);

//if the sentinal is not used for variable a1 then the program will continue to compute the scores.
while (a1 >=0)
{
printf ("Please input assignment 2 score then press ENTER: ");
scanf ("%d", &a2);
printf ("Please input assignment 3 score then press ENTER: ");
scanf ("%d", &a3);
printf ("Please input assignment 4 score then press ENTER: ");
scanf ("%d", &a4);
printf ("Please input assignment 5 score then press ENTER: ");
scanf ("%d", &a5);
printf ("Please input test 1 score then press ENTER: ");
scanf ("%d", &t1);
printf ("Please input test 2 score then press ENTER: ");
scanf ("%d", &t2);
printf ("Please input final exam score then press ENTER: ");
scanf ("%d", &f);
printf ("\n");

//determines if an invalid score was entered, notifies the user, and allows for a correction.
good_scores= (a1>=0 && a1<=100 && a2>=0 && a2<=100 && a3>=0 && a3<=100 && a4>=0 && a4<=100 && a5>=0 && a5<=100 && t1>=0 && t1<=100 && t2>=0 && t2<=100 && f>=0 && f<=100);

if (!good_scores)
{
printf ("YOU HAVE ENTERED AN INVALID SCORE. CHECK YOUR SCORES AND TRY AGAIN.\n");
printf ("\n");
printf ("\n");
printf ("**IF FINISHED ENTER A NEGATIVE NUMBER FOR ASSIGNMENT 1 AND PRESS ENTER.");
printf ("\n");
printf ("Please input assignment 1 score then press ENTER: ");
scanf ("%d", &a1);
}

else
{
//echo printing the scores for verification
printf ("Assignment 1 score is: %d\n", a1);
printf ("Assignment 2 score is: %d\n", a2);
printf ("Assignment 3 score is: %d\n", a3);
printf ("Assignment 4 score is: %d\n", a4);
printf ("Assignment 5 score is: %d\n", a5);
printf ("Test 1 score is: %d\n", t1);
printf ("Test 2 score is %d\n", t2);
printf ("Final Exam score is %d\n", f);
printf ("IF THESE SCORES ARE INCORRECT YOU MUST RESTART THE PROGRAM AND RE-ENTER GRADES!");

//algebra required to compute the scores.
assignment_total= ((a1+a2+a3+a4+a5)/5)*.30;
test_total= ((t1+t2)/2)*.30;
final_exam= (f)*.40;
printf ("\n");
printf ("\n");
class_average= sum/count;

//prints out the letter grade and keeps a running count of how many different letter grades there are.
{
}
{
}
{
}
{
}
else
{
}
printf ("\n");
system ("pause");
printf ("\n");
printf ("\n");

//changes the student number to the next higher student
count= count+1;

//starts the loop over and allows the user to enter a sentinel number to produce a summary and terminate the program.
printf ("**IF FINISHED ENTER A NEGATIVE NUMBER FOR ASSIGNMENT 1 AND PRESS ENTER.");
printf ("\n");
printf ("Please input assignment 1 score and press ENTER: ");
scanf ("%d", &a1);
}
}

//summary report produced by entering a negative number for assignment 1.
printf ("\n");
printf ("Class Average score is %f\n", class_average);
printf ("\n");
printf ("The program will now close. ");
system("pause");
return 0;
}```

9. Letter grades should be ABCD or F, and obviously, char's. Each letter grade having it's own boolean? No, that introduces more variables you don't need, into the program.

Try to be as direct as possible. C works best when it's straightforward and concise - but clear. Never obtuse.

It's not obvious to you now, but later on, you'll see how this program could and should be done in half that number of lines of code - maybe 1/3rd.

For now, good work, and try to be open-minded. A closed mind, like a closed parachute, doesn't always work well.