Advertisement
Guest User

lab5.cpp

a guest
Oct 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.05 KB | None | 0 0
  1. /*
  2.     This program gets you to input an amount of students that want there marks to be calculated
  3.  
  4.     Then it gets you to input your marks for each section in the course and calculates
  5.     your final percentage of the course.
  6.  
  7.     The inputs are checked to see if they are vilid, if not then the program will output a
  8.     error message and end.
  9.  
  10.     The final mark is a weighted sum of all the marks from each section.
  11.  
  12.     Then repeate for the amount of students that you put in.
  13.  
  14.     then the program will display the final marks for each student and the  
  15.     average of all the students final marks combined.
  16. */
  17. #include <cstdio>
  18.  
  19. /*
  20.     DECLARATIONS
  21. */
  22.  
  23. // Maximum mark that you can get for each mark.
  24. const double LAB_MAX = 100.0;
  25. const double PROJECT_MAX = 15.0;
  26. const double MIDTERM_MAX = 75.0;
  27. const double FINAL_MAX = 80.0;
  28.  
  29. // How much the different marks contribute to the final percentage.
  30. const double LAB_WEIGHT = 25.0;
  31. const double PROJECT_WEIGHT = 5.0;
  32. const double MIDTERM_WEIGHT = 15.0;
  33. const double FINAL_WEIGHT = 40.0;
  34.  
  35. // Names for each grade.
  36. const char* LAB_NAME = "Lab";
  37. const char* PROJECT_NAME = "Project";
  38. const char* MIDTERM1_NAME = "Midterm1";
  39. const char* MIDTERM2_Name = "Midterm2";
  40. const char* FINAL_NAME = "Final";
  41.  
  42. // Max amount of students
  43. const int MAX_STUDENTS = 16.0;
  44.  
  45. /*
  46.     PROTOTYPES
  47. */
  48.  
  49. // Displays welcome message.
  50. void welcome();
  51.  
  52. // Displays welcome message.
  53. void welcome();
  54.  
  55. //Gets the user to input a number of student for marks to be calculated.
  56. bool getStudents(const int maxStudents, int &students);
  57.  
  58. // Gets the user to input a mark for a specific section of the course.
  59. bool getItemMark(const char* name, const double maxMark, double &mark);
  60.  
  61. // Adds the specific item to the final mark using the weighting constant.
  62. double calcItemContruibution(double mark, const double maxMark, const double weight);
  63.  
  64. // Displays the output of the program.
  65. double getFinalMark(double finalMark);
  66.  
  67. // Displays the marks frome each student and
  68. // take all the final marks from all the students inputed and averages them.
  69. void displayAverage(int students, double studentFinalMarks[]);
  70.  
  71. /*
  72.     MAIN FUNCTION
  73. */
  74.  
  75. int main()
  76. {
  77.     /*
  78.         LOCAL DECLARATIONS
  79.     */
  80.  
  81.     // Values for the input from the user.
  82.     double labMark, projectMark, midterm1Mark, midterm2Mark, finalExamMark;
  83.  
  84.     // The contribution that item makes to the final grade,
  85.     double labPercent, projectPercent, midterm1Percent, midterm2Percent, finalExamPercent;
  86.    
  87.     // The final overall mark
  88.     double finalMark;
  89.  
  90.     // The amount of student marks that are being calculated.
  91.     int numStudents;
  92.  
  93.     double studentFinalMarks[numStudents];
  94.  
  95.     /*
  96.         MAIN FUNCION BODY
  97.     */
  98.  
  99.     // Adds an extra line before the start of the program.
  100.     printf("\n");
  101.  
  102.     // Display the welcome/intro message
  103.     welcome();
  104.  
  105.     // Calls getStudents and checks to see if it returned true if not: error and stop program.
  106.     if(!getStudents(MAX_STUDENTS, numStudents))
  107.     {
  108.         printf("Terminating the program due to error\n\n");
  109.         return 0;
  110.     }
  111.  
  112.     // Loops through all the students and calculates there final mark
  113.     for (int i = 0; i < numStudents; i++)
  114.     {
  115.         // Gets the Marks for each section from the user.
  116.         if(!getItemMark(LAB_NAME, LAB_MAX, labMark))
  117.         {    
  118.             printf("Terminating the program due to error\n\n");
  119.             return 0;
  120.         }
  121.  
  122.         if(!getItemMark(PROJECT_NAME, PROJECT_MAX, projectMark))
  123.         {    
  124.             printf("Terminating the program due to error\n\n");
  125.             return 0;
  126.         }
  127.  
  128.         if(!getItemMark(MIDTERM1_NAME, MIDTERM_MAX, midterm1Mark))
  129.         {    
  130.             printf("Terminating the program due to error\n\n");
  131.             return 0;
  132.         }  
  133.  
  134.         if(!getItemMark(MIDTERM2_Name, MIDTERM_MAX, midterm2Mark))
  135.         {    
  136.             printf("Terminating the program due to error\n\n");
  137.             return 0;
  138.         }
  139.  
  140.         if(!getItemMark(FINAL_NAME, FINAL_MAX, finalExamMark))
  141.         {    
  142.             printf("Terminating the program due to error\n\n");
  143.             return 0;
  144.         }
  145.  
  146.         // Computes the weighted percentage for each mark.
  147.         labPercent = calcItemContruibution(labMark, LAB_MAX, LAB_WEIGHT);
  148.         projectPercent = calcItemContruibution(projectMark, PROJECT_MAX, PROJECT_WEIGHT);
  149.         midterm1Percent = calcItemContruibution(midterm1Mark, MIDTERM_MAX, MIDTERM_WEIGHT);
  150.         midterm2Percent = calcItemContruibution(midterm2Mark, MIDTERM_MAX, MIDTERM_WEIGHT);
  151.         finalExamPercent = calcItemContruibution(finalExamMark, FINAL_MAX, FINAL_WEIGHT);
  152.  
  153.         // Computes the overall mark (the sum of the five Percentages)
  154.         finalMark = (labPercent +
  155.                      projectPercent +
  156.                      midterm1Percent +
  157.                      midterm2Percent +
  158.                      finalExamPercent);
  159.  
  160.         // Adds an extra line after the last call for input from the user.
  161.         printf("\n");
  162.  
  163.         // Displays a summary of the result(Result is the final mark).
  164.         // and adds the final mark to the array of final marks.
  165.         studentFinalMarks[i] = getFinalMark(finalMark);
  166.  
  167.         // Adds an extra line after the end of the program.
  168.         printf("\n");
  169.     }
  170.    
  171.     // Displays the final marks from each stuent and displays the average of all the marks.
  172.     displayAverage(numStudents, studentFinalMarks);
  173. }
  174.  
  175. /*
  176.     FUNCTIONS
  177. */
  178.  
  179. void welcome()
  180. {
  181.     printf("Welcome to our final mark calculator.\n\n");
  182.  
  183.     printf("you will be asked to enter an amount of students that the program will"
  184.            " compute\n\n You will be asked to enter your marks for the different\n"
  185.            "items of assessment for the course, and the program will\n"
  186.            "then compute and display your final mark out of 100.\n\n");
  187. }
  188.  
  189. bool getStudents(const int maxStudents, int &students)
  190. {
  191.     // How many student the user inputs.
  192.     int numStudents = 0;
  193.  
  194.     // IsValid = true when the user enters valid data.
  195.     bool isValid = false;
  196.  
  197.     // Replacement of recurtion(loop).
  198.     // Loops untill a valid value is entered.
  199.     while(!isValid)
  200.     {
  201.         // Prompt for user to ener value.
  202.         printf("Please enter a amount of students that is in the"
  203.                "range from 1 to %d\n", maxStudents);
  204.  
  205.         int valsRead = scanf("%d", &numStudents);
  206.  
  207.         // If they did not enter a number
  208.         if(valsRead < 1)
  209.         {
  210.             printf("Sorry that was not an intager, please try again\n");
  211.  
  212.             // Clearing the input buffer.
  213.             scanf("&*s");
  214.         }
  215.  
  216.         // If they did not enter a number in the range specified.
  217.         else if( (numStudents < 1) || (numStudents > maxStudents) )
  218.         {
  219.             printf("Sorry the intager you entered was not in the"
  220.                    " range 1 to %d, please try again\n", maxStudents);
  221.  
  222.             // Clearing the input buffer.
  223.             scanf("&*s");
  224.         }
  225.  
  226.         // If the input is valid
  227.         if((valsRead > 0) && (numStudents > 0) && (numStudents < maxStudents) )
  228.         {
  229.             // Sets the input the passed mark variable.
  230.             students = numStudents;
  231.  
  232.             // Setting isvalid to true so that the loop stops.
  233.             isValid = true;
  234.         }
  235.     }
  236.  
  237.     return isValid ? true : false;
  238. }
  239.  
  240. bool getItemMark(const char* name, const double maxMark, double &mark)
  241. {
  242.     // The number of students the user inputs.
  243.     double studentMark = 0;
  244.  
  245.     // Equals true when the user enter a valid value.
  246.     bool isValid = false;
  247.  
  248.     // Replacement of recurtion(loop).
  249.     // Loops untill a valid value is entered.
  250.     while(!isValid)
  251.     {
  252.         // Prompt for user to ener value.
  253.         printf("Please enter a mark for the %s that is in the range from 0 to %lg\n",
  254.                name, maxMark);
  255.  
  256.         int valsRead = scanf("%lg", &studentMark);
  257.  
  258.         // If they did not enter a number.
  259.         if(valsRead < 1)
  260.         {
  261.             printf("Sorry that was not a number, please try again\n");
  262.  
  263.             // Clearing the input buffer.
  264.             scanf("&*s");
  265.         }
  266.  
  267.         // If they did not enter a number in the range specified.
  268.         else if( (studentMark < 0) || (studentMark > maxMark) )
  269.         {
  270.             printf("Sorry the intager you entered was not in the "
  271.                    "range 0 to %lg, please try again\n", maxMark);
  272.  
  273.             // Clearing the input buffer.
  274.             scanf("&*s");
  275.         }
  276.  
  277.         // If the input is valid
  278.         if((valsRead > 0) && (studentMark >= 0) && (studentMark <= maxMark) )
  279.         {
  280.             // Sets the input the passed mark variable.
  281.             mark = studentMark;
  282.  
  283.             // Setting isvalid to true so that the loop stops.
  284.             isValid = true;
  285.         }
  286.     }
  287.    
  288.     // Returning true to say that the function has succesfuly gotten valid input.
  289.     return isValid ? true : false;
  290. }
  291.  
  292. double calcItemContruibution(double mark, const double maxMark, const double weight)
  293. {
  294.     // Calculating the contribution for the mark that was passed.
  295.     return ((mark * weight) / maxMark);
  296. }
  297.  
  298. double getFinalMark(double finalMark)
  299. {
  300.     // Displays the final mark.
  301.     printf("the final mark for the course is %lg%%\n", finalMark);
  302.  
  303.     // Returns the final mark.
  304.     return finalMark;
  305. }
  306.  
  307. void displayAverage(int students, double studentFinalMarks[])
  308. {
  309.     // Average of all the students final marks.
  310.     double average;
  311.  
  312.     // Loops through all the students and adds them all to average.
  313.     for (int i = 0; i < students; i++)
  314.     {
  315.         average += studentFinalMarks[i];
  316.  
  317.         // Displays the current students final mark.
  318.         printf("Student %d has a final mark of %lg\n", i, studentFinalMarks[i]);
  319.     }
  320.  
  321.     // Dividing all the students final marks combined
  322.     // by the amount of students to get the average.
  323.     average /= students;
  324.    
  325.     // Displays the final average.
  326.     printf("\nthe average mark for all the students is %lg%%\n\n", average);
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement