Check for efficiency

This is a discussion on Check for efficiency within the C Programming forums, part of the General Programming Boards category; I am new to programming and have to write a program for class that is designed to calculate student averages, ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    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()
    {
    
    int a1, a2, a3, a4, a5, t1, t2, f, count, a_grades, b_grades, c_grades, d_grades, f_grades;
    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
    a_grades= 0;
    b_grades= 0;
    c_grades= 0;
    d_grades= 0;
    f_grades= 0;
    
    //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 ("Number of A grades: %d\n", a_grades);
                                  printf ("Number of B grades: %d\n", b_grades);
                                  printf ("Number of C grades: %d\n", c_grades);
                                  printf ("Number of D grades: %d\n", d_grades);
                                  printf ("Number of F grades: %d\n", f_grades);
                                  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;
              final_grade= (assignment_total+test_total+final_exam);
              printf ("\n");
              printf ("\n");
              printf ("FINAL GRADE for student %d is %f\n", count, final_grade);
              sum= sum+final_grade;
              class_average= sum/count;
              
              //letter grade ranges are set here
              A= (final_grade >=85 && final_grade <=100);
              B= (final_grade >=74 && final_grade <85);
              C= (final_grade >=63 && final_grade <74);
              D= (final_grade >=52 && final_grade <63);
              F= (final_grade >=0  && final_grade <52);
              
              //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);
                    a_grades= a_grades+1;
                    }
              else if (B)
                    {
                    printf ("LETTER GRADE for student %d is: B\n", count);
                    b_grades= b_grades+1;
                    }
              else if (C)
                    {
                    printf ("LETTER GRADE is: C\n");
                    c_grades= c_grades+1;
                    }
              else if (D)
                    {
                    printf ("LETTER GRADE is: D\n");
                    d_grades= d_grades+1;
                    }
              else 
                    {
                    printf ("LETTER GRADE is: F\n");
                    f_grades= f_grades+1;
                    }
              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 ("Number of A grades: %d\n", a_grades);
              printf ("Number of B grades: %d\n", b_grades);
              printf ("Number of C grades: %d\n", c_grades);
              printf ("Number of D grades: %d\n", d_grades);
              printf ("Number of F grades: %d\n", f_grades);
              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. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Have you learned about arrays yet? Arrays could definitely help to clean up the code quite a bit.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    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 )
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    About the only thing I see is the lack of meaningful variable names. See this link for an example: Variable Names.


    Jim

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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++) {
      printf("please input assignment number %d"i+1)
      //get your five scores here 
    }
    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!
    Last edited by Adak; 02-23-2011 at 01:54 PM.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by rgrwatson85 View Post
    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?"
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    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()
    {
    
    int a1, a2, a3, a4, a5, t1, t2, f, count, a_grades, b_grades, c_grades, d_grades, f_grades;
    float assignment_total, test_total, final_exam, final_grade, sum, class_average;
    bool good_scores; 
    char letter_grade;
    
    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
    a_grades= 0;
    b_grades= 0;
    c_grades= 0;
    d_grades= 0;
    f_grades= 0;
    
    //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;
          final_grade= (assignment_total+test_total+final_exam);
          printf ("\n");
          printf ("\n");
          printf ("FINAL GRADE for student %d is %f\n", count, final_grade);
          sum= sum+final_grade;
          class_average= sum/count;
              
          //prints out the letter grade and keeps a running count of how many different letter grades there are.
          if (final_grade >=85 && final_grade <=100)
          {   
    		 letter_grade= 'A';
             printf ("LETTER GRADE for student %d is: %c\n", count, letter_grade);
             a_grades= a_grades+1;
          }
          else if (final_grade >=74 && final_grade <85)
          {
    		 letter_grade= 'B';
             printf ("LETTER GRADE for student %d is: %c\n", count, letter_grade);
             b_grades= b_grades+1;
          }
          else if (final_grade >=63 && final_grade <74)
          {
    		 letter_grade= 'C';
             printf ("LETTER GRADE for student %d is: %c\n", count, letter_grade);
             c_grades= c_grades+1;
          }
          else if (final_grade >=52 && final_grade <63)
          {
    		 letter_grade= 'D';
             printf ("LETTER GRADE for student %d is: %c\n", count, letter_grade);
             d_grades= d_grades+1;
          }
          else 
          {
    		 letter_grade= 'F';
             printf ("LETTER GRADE for student %d is: %c\n", count, letter_grade);
             f_grades= f_grades+1;
          }
          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 ("Number of A grades: %d\n", a_grades);
    printf ("Number of B grades: %d\n", b_grades);
    printf ("Number of C grades: %d\n", c_grades);
    printf ("Number of D grades: %d\n", d_grades);
    printf ("Number of F grades: %d\n", f_grades);
    printf ("\n");
    printf ("Class Average score is %f\n", class_average);
    printf ("\n");
    printf ("The program will now close. ");
    system("pause");
    return 0;     
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21