Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This program gets you to input an amount of students that want there marks to be calculated
- Then it gets you to input your marks for each section in the course and calculates
- your final percentage of the course.
- The inputs are checked to see if they are vilid, if not then the program will output a
- error message and end.
- The final mark is a weighted sum of all the marks from each section.
- Then repeate for the amount of students that you put in.
- then the program will display the final marks for each student and the
- average of all the students final marks combined.
- */
- #include <cstdio>
- /*
- DECLARATIONS
- */
- // Maximum mark that you can get for each mark.
- const double LAB_MAX = 100.0;
- const double PROJECT_MAX = 15.0;
- const double MIDTERM_MAX = 75.0;
- const double FINAL_MAX = 80.0;
- // How much the different marks contribute to the final percentage.
- const double LAB_WEIGHT = 25.0;
- const double PROJECT_WEIGHT = 5.0;
- const double MIDTERM_WEIGHT = 15.0;
- const double FINAL_WEIGHT = 40.0;
- // Names for each grade.
- const char* LAB_NAME = "Lab";
- const char* PROJECT_NAME = "Project";
- const char* MIDTERM1_NAME = "Midterm1";
- const char* MIDTERM2_Name = "Midterm2";
- const char* FINAL_NAME = "Final";
- // Max amount of students
- const int MAX_STUDENTS = 16.0;
- /*
- PROTOTYPES
- */
- // Displays welcome message.
- void welcome();
- // Displays welcome message.
- void welcome();
- //Gets the user to input a number of student for marks to be calculated.
- bool getStudents(const int maxStudents, int &students);
- // Gets the user to input a mark for a specific section of the course.
- bool getItemMark(const char* name, const double maxMark, double &mark);
- // Adds the specific item to the final mark using the weighting constant.
- double calcItemContruibution(double mark, const double maxMark, const double weight);
- // Displays the output of the program.
- double getFinalMark(double finalMark);
- // Displays the marks frome each student and
- // take all the final marks from all the students inputed and averages them.
- void displayAverage(int students, double studentFinalMarks[]);
- /*
- MAIN FUNCTION
- */
- int main()
- {
- /*
- LOCAL DECLARATIONS
- */
- // Values for the input from the user.
- double labMark, projectMark, midterm1Mark, midterm2Mark, finalExamMark;
- // The contribution that item makes to the final grade,
- double labPercent, projectPercent, midterm1Percent, midterm2Percent, finalExamPercent;
- // The final overall mark
- double finalMark;
- // The amount of student marks that are being calculated.
- int numStudents;
- double studentFinalMarks[numStudents];
- /*
- MAIN FUNCION BODY
- */
- // Adds an extra line before the start of the program.
- printf("\n");
- // Display the welcome/intro message
- welcome();
- // Calls getStudents and checks to see if it returned true if not: error and stop program.
- if(!getStudents(MAX_STUDENTS, numStudents))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- // Loops through all the students and calculates there final mark
- for (int i = 0; i < numStudents; i++)
- {
- // Gets the Marks for each section from the user.
- if(!getItemMark(LAB_NAME, LAB_MAX, labMark))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- if(!getItemMark(PROJECT_NAME, PROJECT_MAX, projectMark))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- if(!getItemMark(MIDTERM1_NAME, MIDTERM_MAX, midterm1Mark))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- if(!getItemMark(MIDTERM2_Name, MIDTERM_MAX, midterm2Mark))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- if(!getItemMark(FINAL_NAME, FINAL_MAX, finalExamMark))
- {
- printf("Terminating the program due to error\n\n");
- return 0;
- }
- // Computes the weighted percentage for each mark.
- labPercent = calcItemContruibution(labMark, LAB_MAX, LAB_WEIGHT);
- projectPercent = calcItemContruibution(projectMark, PROJECT_MAX, PROJECT_WEIGHT);
- midterm1Percent = calcItemContruibution(midterm1Mark, MIDTERM_MAX, MIDTERM_WEIGHT);
- midterm2Percent = calcItemContruibution(midterm2Mark, MIDTERM_MAX, MIDTERM_WEIGHT);
- finalExamPercent = calcItemContruibution(finalExamMark, FINAL_MAX, FINAL_WEIGHT);
- // Computes the overall mark (the sum of the five Percentages)
- finalMark = (labPercent +
- projectPercent +
- midterm1Percent +
- midterm2Percent +
- finalExamPercent);
- // Adds an extra line after the last call for input from the user.
- printf("\n");
- // Displays a summary of the result(Result is the final mark).
- // and adds the final mark to the array of final marks.
- studentFinalMarks[i] = getFinalMark(finalMark);
- // Adds an extra line after the end of the program.
- printf("\n");
- }
- // Displays the final marks from each stuent and displays the average of all the marks.
- displayAverage(numStudents, studentFinalMarks);
- }
- /*
- FUNCTIONS
- */
- void welcome()
- {
- printf("Welcome to our final mark calculator.\n\n");
- printf("you will be asked to enter an amount of students that the program will"
- " compute\n\n You will be asked to enter your marks for the different\n"
- "items of assessment for the course, and the program will\n"
- "then compute and display your final mark out of 100.\n\n");
- }
- bool getStudents(const int maxStudents, int &students)
- {
- // How many student the user inputs.
- int numStudents = 0;
- // IsValid = true when the user enters valid data.
- bool isValid = false;
- // Replacement of recurtion(loop).
- // Loops untill a valid value is entered.
- while(!isValid)
- {
- // Prompt for user to ener value.
- printf("Please enter a amount of students that is in the"
- "range from 1 to %d\n", maxStudents);
- int valsRead = scanf("%d", &numStudents);
- // If they did not enter a number
- if(valsRead < 1)
- {
- printf("Sorry that was not an intager, please try again\n");
- // Clearing the input buffer.
- scanf("&*s");
- }
- // If they did not enter a number in the range specified.
- else if( (numStudents < 1) || (numStudents > maxStudents) )
- {
- printf("Sorry the intager you entered was not in the"
- " range 1 to %d, please try again\n", maxStudents);
- // Clearing the input buffer.
- scanf("&*s");
- }
- // If the input is valid
- if((valsRead > 0) && (numStudents > 0) && (numStudents < maxStudents) )
- {
- // Sets the input the passed mark variable.
- students = numStudents;
- // Setting isvalid to true so that the loop stops.
- isValid = true;
- }
- }
- return isValid ? true : false;
- }
- bool getItemMark(const char* name, const double maxMark, double &mark)
- {
- // The number of students the user inputs.
- double studentMark = 0;
- // Equals true when the user enter a valid value.
- bool isValid = false;
- // Replacement of recurtion(loop).
- // Loops untill a valid value is entered.
- while(!isValid)
- {
- // Prompt for user to ener value.
- printf("Please enter a mark for the %s that is in the range from 0 to %lg\n",
- name, maxMark);
- int valsRead = scanf("%lg", &studentMark);
- // If they did not enter a number.
- if(valsRead < 1)
- {
- printf("Sorry that was not a number, please try again\n");
- // Clearing the input buffer.
- scanf("&*s");
- }
- // If they did not enter a number in the range specified.
- else if( (studentMark < 0) || (studentMark > maxMark) )
- {
- printf("Sorry the intager you entered was not in the "
- "range 0 to %lg, please try again\n", maxMark);
- // Clearing the input buffer.
- scanf("&*s");
- }
- // If the input is valid
- if((valsRead > 0) && (studentMark >= 0) && (studentMark <= maxMark) )
- {
- // Sets the input the passed mark variable.
- mark = studentMark;
- // Setting isvalid to true so that the loop stops.
- isValid = true;
- }
- }
- // Returning true to say that the function has succesfuly gotten valid input.
- return isValid ? true : false;
- }
- double calcItemContruibution(double mark, const double maxMark, const double weight)
- {
- // Calculating the contribution for the mark that was passed.
- return ((mark * weight) / maxMark);
- }
- double getFinalMark(double finalMark)
- {
- // Displays the final mark.
- printf("the final mark for the course is %lg%%\n", finalMark);
- // Returns the final mark.
- return finalMark;
- }
- void displayAverage(int students, double studentFinalMarks[])
- {
- // Average of all the students final marks.
- double average;
- // Loops through all the students and adds them all to average.
- for (int i = 0; i < students; i++)
- {
- average += studentFinalMarks[i];
- // Displays the current students final mark.
- printf("Student %d has a final mark of %lg\n", i, studentFinalMarks[i]);
- }
- // Dividing all the students final marks combined
- // by the amount of students to get the average.
- average /= students;
- // Displays the final average.
- printf("\nthe average mark for all the students is %lg%%\n\n", average);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement