...
/Solution Review: Calculate Overall Percentage of Student's Mark
Solution Review: Calculate Overall Percentage of Student's Mark
Let's go over the solution review of the challenge given in the previous lesson.
We'll cover the following...
Solution #
Press the RUN button and see the output!
Press + to interact
#include <iostream>using namespace std;// Student structurestruct Student {// Stores the name of Studentstring name;// Stores the marks of student in 4 subjectsdouble marks[4];};// calculate_percentage functiondouble calculate_percentage(struct Student s) {// Initialize variablesdouble total = 0, percentage = 0;// for loop to traverse marks of Studentfor (int i = 0; i < 4; i++) {// Add marks of Student in totaltotal = total + s.marks[i];}// Calculate percentagepercentage = (total / 400) * 100;// Return percentage of Studentreturn percentage;}// print_Student functionvoid print_Student(struct Student s) {cout << "Name of student = " << s.name << endl;cout << "Student marks:" << endl;for (int i = 0; i < 4; i++) {cout << "Student marks in subject" << i + 1 << "=" << s.marks[i] << endl;}}// main functionint main() {// Declare structure variable s of type Studentstruct Student s;// Declare variable of type doubledouble result;// Initialize members of ss = {"John",{30.5, 49.7, 22.3, 32.9}};// Call function calculate_percentage and store output in resultresult = calculate_percentage(s);// Call print_Student function to print members of sprint_Student(s);// Print percentage of Studentcout << "percentage = " << result << "%";return 0;}
Explanation
struct Student
We define the structure Student
...