assignment grading project problem

This is what it's asking:

Write a program that calculates and prints the total grade for n assignments
as a percentage. Prompt the user to enter the value of n, followed by the number
of points received and number of points possible for each assignment. Calculate
and print the total number of points received, total number of points possible,
and the overall percentage: (total points received / total points possible) * 100.

This is what I got:

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
32
33
#include <iostream>

using namespace std;

int main() {

	int count = 1;
	int assignments, points, pPoints, tPoints, tPPoints;
	double grade;
	
	cout << "Enter number of assignments to input:";
	cin >> assignments;
	
	do {
		
		cout << "Enter number of points received for assignment " << count << ":";
		cin >> points;
		tPoints = tPoints + points;
		
		cout << "Enter number of possible points for assignment " << count << ":";
		cin >> pPoints;
		tPPoints = tPPoints + pPoints;
		
		count++;
		
	} while (count <= assignments);
	
	cout.precision(2);
	grade = static_cast<double>(tPoints)/tPPoints * 100;
	cout << "Your total amount of points is " << tPoints << " out of " << tPPoints << ", or " << grade << "%.";
	
	return 0;	
}


output I'm getting:
Enter number of assignments to input:Enter number of points received for assignment 1:Enter number of possible points for assignment 1:Your total amount of points is 159062883 out of 32864, or 4.8e+05%.

Can anyone tell me where I'm going wrong in my math or code?
Firstly, please don't double post. I deleted your post in General.

Now, you seem to be saying that your program is not asking for input. I don't see how that is happening. Clicking the little gear to the upper-right of your code listing runs it in cpp.sh where it does in fact ask for input.

A problem I do see is that you haven't initialized your totals to 0. If you don't initialize they can initially be anything.
Last edited on
Hi,

A golden rule: Always initialise your variables. On line 18 tPoints is not initialised, so when you add points to it, it will still be a garbage value. The compiler tells you this clearly:

In function 'int main()':
22:32: warning: 'tPPoints' may be used uninitialized in this function [-Wmaybe-uninitialized]
18:29: warning: 'tPoints' may be used uninitialized in this function [-Wmaybe-uninitialized]


On line 29 there is integer division, despite the cast, the division happens before the cast. Why not make the variables double to start with?

Also, if you mean double, then make the literal double, as in 100.0 not 100
Last edited on
On line 29 there is integer division, despite the cast, the division happens before the cast.

That's not correct. The cast happens first. Then the division. Then the multiplication.

And he probably wants to say cout << fixed; in conjunction with the cout.precision(2) in order to display two digits after the decimal point. Otherwise input like 855 out of 1000 will be displayed as 86%.
@dutch

My Bad :+|

@RecklessCoder
However the OP could avoid all the casts by making the types double to begin with.

Eliminating casts where ever possible is a good thing, especially when there is lots of data. As a beginner, one is asked to write programs that deal with tiny amounts of data. In the real world, we process millions or billions of items of data, so eliminating a seemingly trivial thing actually makes a difference.

There are compound operators, so line 18 can be written like this:

tPoints += points;

Choose meaningful names, I would rather TotalPoints rather than tPoints. If one does a good job of naming variables and functions, the code should tell a story of what is happening.
But TheIdeasMan that would defeat the point of having different types. Why store values that are sure to be integers as double? There's nothing wrong with casting, it just changes the type. But when you change int to double as a datatype, you're allocating more memory, losing precision, using a part of the CPU that was designed for floating calculations for integer addition and who knows what!

Oh and also if he were to use double for tPoints and pPoints, and if the two values were more than two digits then they would be seen as floating representation because of cout.precision(2).

is 233 better or 2.3e+02 better to display total points, total points is obviously an integer.
2.3e+02 could be 233 or 239 you'd never know and it gets worse if the total is 4digits.

To avoid that he would have to cast output to int. But weren't we doing this to avoid casting, so then OP would have to figure a way to eliminate cout.precision(2), this is getting unnecessarily complex for a simple edit.

So in my opinion, all OP needs to do is initialize his variables and the code is perfect as is. No need to change any types.

Last edited on
> Why store values that are sure to be integers as double?

Yes, favour integers; unlike floating values, they are exact.

It is possible to write the expression without an explicit cast; that is what I may have done:
1
2
3
4
// double grade ;
// ...
// grade = static_cast<double>(tPoints)/tPPoints * 100;
const double grade = tPoints * 100.0 / tPPoints ;
JLBorges, but I don't understand why explicit cast must be avoided. In your snippet, tPoints would be implicitly casted because of 100.0 being a double.

So won't both cases of explicit and implicit cast be handled the same way? In that case wouldn't explicit casting be more unambiguous?

Is there a reason why explicit cast should have been avoided in this case?
> Is there a reason why explicit cast should have been avoided in this case?

No, an explicit cast would be very acceptable in this case. However, in general, I find it easier to read code containing implicit conversions than one which contains explicit casts.
Okay thanks JLBorges.
@Grime

Not much of what you wrote in the following post made sense to me:

Why store values that are sure to be integers as double?


They are going to be used in division, which usually needs a double. Here we have a / b where a < b

The double type can exactly represent integers up to 32 bits, so we are not losing precision here.

using a part of the CPU that was designed for floating calculations for integer addition and who knows what!


Not addition, division. The fpu is going to be used anyway.

Oh and also if he were to use double for tPoints and pPoints, and if the two values were more than two digits then they would be seen as floating representation because of cout.precision(2).

is 233 better or 2.3e+02 better to display total points, total points is obviously an integer.
2.3e+02 could be 233 or 239 you'd never know and it gets worse if the total is 4digits.

To avoid that he would have to cast output to int. But weren't we doing this to avoid casting, so then OP would have to figure a way to eliminate cout.precision(2), this is getting unnecessarily complex for a simple edit.


Just because it's a double doesn't mean it has to be printed in scientific format.

Another thing, there is usually a trade off between memory used and cpu time. Having the variables as double may take more memory and someone could test whether the casting may possibly take more time. For this really simple problem, IMO it's not worth the toss up between various aspects of of int vs double, even if there are millions of them. One million doubles is about 8 million bytes, a trivial amount of memory these days. For this particular problem one could even use a float, but again I wouldn't worry.

To put this into perspective I have done complicated calculations that involve dozens of double variables per point for tens of millions of 3d points.
TheIdeasMan,

If you want to have tPoints and pPoints as doubles, realize that you need to add values to them as they are 'sum'.

I thought that you wanted the 'values' to be doubles as well. If you wanted the values to be integers then you are contradicting yourself because you would be casting at that point. I thought this was understood.. where's the confusion?

How will there not be addition of doubles? If tPoints and pPoints are doubles, and since you're adding numbers to them, you're inevitable adding doubles either by casting or not casting. That is addition of doubles is it not?

Just because it's a double doesn't mean it has to be printed in scientific format.

Reread what I said and you'll understand why you're wrong. Specifically see the part where I talk about cout.precision(2).

Another thing, there is usually a trade off between memory used and cpu time.
Are you actually saying that one simple type conversion (just one) is so expensive?

-> If you were using int for the values and double for pPoints and tPoints then you are casting every damn single time you're adding, so you would be contradicting yourself.

-> If you were using double for all values, then you're not only wasting memory instead of one simple caste, but you're also not getting any CPU advantage like you stated because you're performing integer arithmetic with fpu.




If you get 0 out of 10 in your first test and 15 out of 20 in your second, what do you think your GPA is?

(i) 50%
(ii) 37.5%



Grime wrote:
Are you actually saying that one simple type conversion (just one) is so expensive?



Some things to note:

1. If we are talking about a toy program with a small number of values, then this whole discussion is irrelevant, the program will finish so quickly any way.

2. I was talking about the scenario where there are millions of students. That cast will happen millions of times.

3. I am saying the situation is not as bad as you think it is, even for 1 million students. Summing ints will be faster than doubles, but I don't think the difference is worth worrying about. The way to test that is to write a test program, that times the various scenarios.

Reread what I said and you'll understand why you're wrong. Specifically see the part where I talk about cout.precision(2).


How about you write a small program see how a double is printed using precision? And yes grade is a double by necessity, and who says that cout.precision(2) is set in stone? One could use cout.precision(4) to get 2dp for the percentage. Perhaps you could do some research, and discover when when cout prints in scientific format by default, and see if it applies to this problem.
You didn't read my post. I didn't say you must use cout.precision. I said that if you were to change the datatypes to doubled, you would also need to truncate grade.

But I don't understand why you are trying to avoid the cast? And I don't understand what you mean by 'cast will happen millions of times' it only happens once in the program, where grade needs to be calculated. Only one grade is calculated, not for every student. Even if it were a cast for every student, is that bad? Why?

So if there is no advantage cpu wise to using doubles, what is the advantage? Why not use int? I don't get your point. int does have the advantage that it's the natural type for the values (since all values are integers) and that it takes lesser memory.

If you get 0 out of 10 in your first test and 15 out of 20 in your second, what do you think your GPA is?

(i) 50%
(ii) 37.5%


Hmm I get what you mean. I guess it's just about phrasing the statement.

The students have scored 50% of the marks in total of the assignments.
37.5% is the mean percentage of every student in the assignments.

37.5 is probably more appropriate but I guess since the question itself asked to find tPoints and pPoints, we have to do as the question says.
Last edited on
You didn't read my post. I didn't say you must use cout.precision. I said that if you were to change the datatypes to doubled, you would also need to truncate grade.


That doesn't make sense, one uses precision to display the double.

Only one grade is calculated, not for every student.


That doesn't make sense either. Are you saying each student should not have their own grade? You do realize the OP's code was n assignments for one student?

Even if it were a cast for every student, is that bad? Why?


I am not saying it is bad, to use doubles instead of casting is just an alternative. I wrote a program that adds 1 million ints versus 1 million doubles, the difference in execution time was 0.06ms, that is why I am saying you are getting all worked up over a very small difference.

If you want to write your own program to see the differences between other scenarios, that is up to you. I don't care.
Last edited on
tPoints and pPoints also need to be displayed. Although you can set separate precision for them so nevermind.

You suggested OP changed all his types, I wanted to know if there was any reason for it. That's all this conversation has gone too long.
Well your stubbornness is a form of trolling, IMO. I only really replied to all this nonsense to see if things had changed since our last encounter: it hasn't.
I'm stubborn if I ask you to reason your "suggestion"?
And stop using the word trolling, you don't know its meaning.

I asked because I wanted to know whether there really was a point to using your suggestion. But you never answered that you just said "he can do that" you never said "why he should do that". Aren't you the troll who's misleading the guy?

Have the sense that if you're going to suggest something, it must at least have some reason or use. I know very well that you're used to asking people to read documentation without telling them what to look for or telling them "go learn" without telling them what to learn. But if you don't stop, you will be considered as a troll. (hmm does this tone sound familiar? does how the word troll is being used look familiar? GUESS WHAT THATS HOW YOU SPEAK!)

Please explain where I was stubborn? I never said "HE IS WRONG DONT LISTEN TO HIM" I only asked you, and only you, to defend your suggestion. So don't use words whose meaning you don't know, okay? I should ask you to go learn the dictionary but that would be too much of an irony considering that's YOUR LINE!

You've always had a condescending attitude towards me, but I want you to justify yourself today. I have always had a respecting attitude towards you until this thread. From now I will reply to you like you do to me, with IRRELEVANT COMMENTS (like you've done with my other posts) and a condescending attitude. I want to see how welcomed you feel.

This is a programming forum. There is only ask and answer. You want drama? Go elsewhere. And be happy there. Don't come back. (hmm if it sounds familiar its because thats how YOU WRITE)

*drops mic*
Now please become civil. I can become an animal like you too if I want to, just that I choose not to. Okay? I'm sure you're not a kid, so stop behaving like one and stop posting like you do. Answer to the point and nobody wants your comments.

Be civil and be a forumer that people look up to (like every other forumer here except for you, right now).
Last edited on
@dutch
I didn't mean to double post, this is my first time using this site and it wasn't posting my post so I tried again. Come to find out it did post and I was just getting impatient, but now I know.

also, thank you for the solution to my problem. I had revised my code into different functions using a function for user input, calculating the grade, and output. I was still having the same problem but that fixed it. It's hard to catch a slip up like that because my compiler will still run the program.
Topic archived. No new replies allowed.