C/C++ C++ Coding question (How do I do it?)

  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    C++ Coding
Click For Summary
SUMMARY

The discussion focuses on completing a C++ program that calculates a student's total course percentage based on weighted scores from homework, midterm, and final exams. The program initially computes percentages using hardcoded values but is improved by introducing constant variables and a function to handle score calculations. Participants suggest implementing a quiz score and modifying weights, as well as creating a separate function for each assessment to enhance code maintainability. The final structure allows for easy adjustments to the program as requirements change.

PREREQUISITES
  • Understanding of C++ programming syntax and structure
  • Familiarity with constant variables and their usage in C++
  • Knowledge of functions and how to define and call them in C++
  • Basic understanding of weighted averages and percentage calculations
NEXT STEPS
  • Implement C++ functions for calculating weighted scores for each assessment type
  • Learn about using preprocessor directives in C++ for defining constants
  • Explore best practices for code maintainability and modular programming in C++
  • Research how to handle user input and output effectively in C++
USEFUL FOR

C++ developers, computer science students, and educators looking to enhance their understanding of program structure and percentage calculations in C++.

carl123
Messages
55
Reaction score
0
The following incomplete program should compute a student's total course percentage based on scores on three items of different weights (%s):

20% Homeworks (out of 80 points)
30% Midterm exam (out of 40 points)
50% Final exam (out of 70 points)

Suggested (incremental) steps to finish the program:
First run it.

Next, complete the midterm exam calculation and run the program again. Use the constant variables where appropriate.

Then, complete the final exam calculation and run the program. Use the constant variables where appropriate.

Modify the program to include a quiz score out of 20 points. New weights: 10% homework, 15% quizzes, 30% midterm, 45% final. Run the program again.

To avoid having one large expression, introduce variables homeworkPart, quizPart, midtermPart, and finalPart. Compute each part first; each will be a number between 0 and 1. Then combine the parts using the weights into the course value.
Run the program again.

#include <iostream>
using namespace std;

int main() {
const double HOMEWORK_MAX = 80.0;
const double MIDTERM_MAX = 40.0;
const double FINAL_MAX = 70.0;
const double HOMEWORK_WEIGHT = 0.20; // 20%
const double MIDTERM_WEIGHT = 0.30;
const double FINAL_WEIGHT = 0.50;

double homeworkScore = 0.0;
double midtermScore = 0.0;
double finalScore = 0.0;
double coursePercentage = 0.0;

count << "Enter homework score:" << endl;
cin >> homeworkScore;

count << "Enter midterm exam score:" << endl;
cin >> midtermScore;

count << "Enter final exam score: " << endl;
cin >> finalScore;

coursePercentage = ((homeworkScore / HOMEWORK_MAX) * HOMEWORK_WEIGHT)
+ 0.0 // FIXME for midterm
+ 0.0; // FIXME for final
coursePercentage = coursePercentage * 100; // Convert fraction to %

count << endl << "Your course percentage (FIXME): ";
count << coursePercentage << endl;

return 0;
}
 
Technology news on Phys.org
First of all I recommend having preprocessor symbols for data provided
Code:
#define HOMEWORK_MAX  80
#define MIDTERM_MAX      40 ...

Since calculation for each test procedure (ie midterm, assignment etc.) is same we can create a separate function for that

Code:
double CalculateResults(double dReceivedMarks, double dMaxMarks, double dPercentage)
{
         double dMarks = (dReceivedMarks/dMaxMarks)*dPercentage*100;

         return dMarks;
}

For main() method you can get the result for each test procedure as,

Code:
int main()
{
    ...

    dMidTermResults = CalculateResult(dMidTermScore, MIDTERM_MAX, MIDTERM_WEIGHT);

    ...
}
Similarly use the function for finding HomeWorkResults and FinalResults

And for final results sum up all three variables

Code:
int main()
{
    ...

    dTotalScore = dMidTermResults + dHomeWorkResults + dFinalResults;

    ...
}

By using these method you can easily modify the program when the requirements change
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K