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!
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 ;)
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.
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.
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.