I need to write a program that displays the numbers -1.1 through 3.4, then display the negative sums and the positive sums. I already wrote the loop that displays the numbers, but I cannot figure out how to get just the negative sum and positive sum from this loop. I would need to assign the sum somewhere in this code:
double deci1 = -1.1;
while (deci1 <= 3.4)
{
cout << deci1 << " ";
deci1 = deci1 + 0.3;
}
.. so that the program will display, “the negative sum is -2.6”, and, “the positive sum is 21”.. after it displays the list of values. Please help. I know this is probably really simple, and I am overthinking the problem.
It would be something like
What you should be careful of is the
deci1 <= 3.4, it might or might not evaluate, since you do have a certain uncertainty with discrete representation of doubles. (your deci1 might be 3.3999999999, or 3.400000000001, one returns true, one false on that loop)