Multiple variables in one equation

Ok so I'm working with parrel arrays and I'm needing to calculate how many points are needed for each grade with the numberOfPoints array while doing it in one equation that uses variables that change each time through the loop. I'm not allowed to use 5 assignment operators so I'm kinda stuck on how to calculate and display that, any help would be appreciated!

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	// DECLARE ARRAYS
	char letters[5] = { 'A', 'B', 'C', 'D', 'F' };
	int numberOfPoints[5] = { 0 };

	//DECLARE VARIABLES
	double totalPoints = 0.0;
	double points = 0.0;
	double grades = 0.0;
	bool found = false;
	string name = " ";	

	//STATEMENTS
	cout << "Enter The Name Of The Student: " << endl;
	getline(cin, name);
	cout << "------------------------------------------------------------------------- " << endl;
	
	cout << "Please Enter The Total Possible Points: " << endl;
	cin >> totalPoints; cout << endl;
	
	cout << "Please Enter The Actual Points Recieved: " << endl;
	cin >> points; cout << endl;

	//LOOP
	while (totalPoints > points)
	{
		numberOfPoints[0] = totalPoints * .9;
		numberOfPoints[1] = totalPoints * .8;
		numberOfPoints[2] = totalPoints * .7;
		numberOfPoints[3] = totalPoints * .6;
		numberOfPoints[4] = totalPoints * .5;
	}
	
	//MORE LOOPS
	for (int x = 0; x < 5 && found == false; x++)
	{
		if ( points >= numberOfPoints[x])
		{
			cout << "Your Grade is: " << numberOfPoints[x] << ": Your Letter Grade Is: " << letters[x] << endl << endl;
			found = true;
		}
	}
	system("pause");
	if (found == false)
		{
		system("cls");
		cout << "Invalid Option, Please Try Again" << endl;
		}
	
	return 0;
}
I normally don't like to just do it for you but this is a little tricksy and once you have seen it, you will understand a lot. if you can't use a for loop.. keep reading.
for(int i = 1; i < 6; i++)
{
numberOfPoints[i-1] = totalPoints * (1.0 - (double)(i)/10.0);
}

a while loop:
int i{1}; //the for loop did this for us
while(i != 6) //while only has the condition part of the loop
{
as above, but you also need
i++; //the for loop did this for us
}

which an observant coder may note that a for loop, then, if missing the front and end parts, is a while loop:
for(; condition ;)

ahh You're a lifesaver, thank you so much, I don't believe I've learned the (double)(I) does that make the I a double now?
Last edited on
it is a 'cast' -- actually, its a lazy C style cast. It makes the i variable be treated as a double briefly. It probably isnt necessary there, sometimes I put them in without bothering to check if its 100% required.

read about the C++ casts:
https://en.cppreference.com/w/cpp/language/explicit_cast

C casts are just (type) in front of the variable as I showed. The link explains how that is unraveled as well.
Oh, ill check this out then thanks for all your help!
You don't in this case need to cast i to a double - as int / double gives a double as does double / int. Ony int / int gives int result. As 10.0 is used, this is a double as opposed to 10 which is in int.

hmmm I tried (1 - (i) / 10) but it came out to nothing, I'm not sure why though cause isn't that the same as basically (1.0 - (double)(I) / 10)?
no, as seeplus said, that is integer / integer.

5/10 is zero.
5/10.0 is 0.5

one of the arguments needs to force the compiler to do the math in double space instead of integer space. 10.0 will do that. (double)i/10 will do it. I doubled down above (hahah punz) and did it on both sides. This will bite you again if you don't remember it. Its very easy to forget and do int/int and lose your value.

its true on the decimal side too.
100/3 is 33
100/3.0 is 33.33333... if you need the precision, you have to work in floating point.
Last edited on
Thank you for all your help on this! it had me kinda confused but I think it's making some more sense.
Topic archived. No new replies allowed.