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

  • C/C++
  • Thread starter carl123
  • Start date
  • Tags
    C++ Coding
In summary: I recommend using proper comments and documentation for better understanding of the code. In summary, the program should calculate a student's total course percentage based on scores on three items of different weights: 20% for homework, 30% for the midterm exam, and 50% for the final exam. The suggested steps to finish the program include running it, completing the midterm exam calculation using constant variables, completing the final exam calculation using constant variables, modifying the program to include a quiz score with new weights of 10% for homework, 15% for quizzes, 30% for the midterm exam, and 45% for the final exam, and introducing variables to compute each part first and then combining them using the weights into the course value
  • #1
carl123
56
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
  • #2
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
 

Related to C++ Coding question (How do I do it?)

1. How do I declare a variable in C++?

In C++, variables are declared by specifying the data type followed by the variable name. For example:
int myVariable;
This declares a variable named myVariable of type int.

2. How do I print something to the console in C++?

In C++, the cout function is used to print something to the console. For example:
cout << "Hello World!" << endl;
This will print the string "Hello World!" to the console and then move to the next line.

3. How do I use loops in C++?

C++ has three types of loops: for, while, and do-while. The syntax for a for loop is:
for (initialization; condition; increment/decrement) {
     // code to be executed
}

The while and do-while loops have similar syntax but with different structures. These loops are used for repeating a block of code until a certain condition is met.

4. How do I handle user input in C++?

To handle user input in C++, the cin function is used. For example:
int number;
cout << "Enter a number: ";
cin >> number;

This will prompt the user to enter a number and store it in the number variable.

5. How do I use functions in C++?

To use functions in C++, they must first be declared and defined. The syntax for declaring a function is:
return_type function_name(parameters);
The function can then be defined outside of the main function using the same syntax but with the code to be executed inside the curly braces. Functions are used to modularize code and make it more organized and reusable.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
Back
Top