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

  • Thread starter Thread starter carl123
  • Start date Start date
  • Tags Tags
    C++ Coding
AI Thread 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. Participants suggest incremental steps, starting with running the program and progressively adding calculations for the midterm and final exams. They recommend using constant variables for maximum scores and weights, and introducing a function to streamline the calculation process for each assessment. Additionally, the program can be modified to include quiz scores, adjusting the weights accordingly. Overall, the emphasis is on structuring the code for clarity and ease of modification.
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;

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

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

cout << "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 %

cout << endl << "Your course percentage (FIXME): ";
cout << 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top