Practice Program Question.

Hello all,

Currently I am working out of "Problem Solving with C++ Tenth Edition" to prepare for next semester and am working on the current problem.

"In an exam, students are given a set of questions and 1 point is awarded for every correct answer and 0.25 points are deducted for every incorrect answer. Write a program that prompts the user for the number of questions that the student answered correctly and the number of incorrect answers. The program should use a function calcMarkAsPercentage to calculate the student’s final mark as a percentage. Carefully consider the order of operations required to calculate the correct score and if you need to cast any values"

To do this part I wrote this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

double calcMarkAsPercentage(int QRight, int QWrong);
//Calculate grade percentage based off of questions answered rigth and questions answered wrong.
int main()
{
	int QRight, QWrong;
	
	std::cout << "Please input the number of questions answered correctly.\n";
	std::cin >> QRight;
	std::cout << "You have entered: " << QRight << " correct answers.\n";

	std::cout << "Please input the number of questions answered wrong.\n";
	std::cin >> QWrong;
	std::cout << "You have entered: " << QWrong << " wrong answers.\n";
	
	double PercentageGrade = calcMarkAsPercentage(QRight, QWrong);
	
	std::cout << "The total amount of points possible is: " << (QRight + QWrong) << " points.\n";
	std::cout << "The total amount of points scored is: " << ((QRight * 1) + (QWrong * -0.25)) << " points.\n";
	std::cout << "The score for this exam is: " << PercentageGrade << "%";
}

double calcMarkAsPercentage(int QRight, int QWrong)
{
	//TotalQ is also Total Points
	int TotalQ = QRight + QWrong;
	//Sum of right points and wrong points converted to percentage
	double PercentageGrade = (((QRight * 1) + (QWrong * -0.25)) / TotalQ) * 100;
	return PercentageGrade;
}


However, the next part of the problem is as follows.

"Modify your program from the previous Practice Program to ask the user for the number of students whose marks are to be entered. Your program should then prompt the user for the marks of the required number of students. If a student gets a score lower than 0, their mark should be set to and output as 0. Your program should then print the highest, lowest, and average score of the students whose scores were calculated."

With a little research, I found people bringing up Array's which is a topic not yet discussed in the book, and I am trying to use what I have learned so far which is the basics of variables, flow of control, and function declarations. Any insight would be greatly appreciated!
Last edited on
program should then print the highest, lowest, and average score of the students whose scores were calculated

This does not require array. You don't need (to keep and show) the actual marks, just the highest, lowest, and ... what does one need to calculate an average?
I think for the average I would just get a total of all the marks and then divide by the number of marks. But how do I go about identifying the highest and lowest marks? The first thing that comes to mind is some sort of if then statement comparing the entry of the current mark to the entry of the last mark.
Last edited on
The first thing that comes to mind is some sort of if then statement comparing the entry of the current mark to the entry of the last mark
Almost. You want to compare the current mark the highest one seen so far. If the current mark is higher then change the "highest seen to far" variable to the current mark. Then do the same kind of thing to find the lowest mark.
The only "trick" part there is that the first mark is by definition both highest so far and lowest so far, and there is nothing to compare it to.

There are two approaches:
1. Somehow treat the first mark differently than the rest
or
2. Have the initial value of highest/lowest so far variables such that after the test the variables equal the first mark.
See example in http://www.cplusplus.com/reference/limits/numeric_limits/ for how to get the initial values
Last edited on
Hey thanks everyone for the advice, will implement these ideas. Appreciate it!
Topic archived. No new replies allowed.