summation of arrays element problem

It should sum elements of the array of x from 135.1 to 24.1 and the output be 594.4
but the output is 570.3 which is the sum from 135.1 to 24.6 not 24.1
it works as the if condition is sum + x[i] smaller than 594.4 only and ignores the equal condition.
am very confused about it.

1
2
3
4
5
6
	double x[12] = {135.1,73.1,63.2,59.1,59.1,53.1,36.1,35.1,31.8,24.6,24.1,50.2};
	double sum = 0;
	for (int i = 0; i < 12; i++)
		if (sum + x[i] <= 594.4)
			sum += x[i];
	cout << sum << endl;
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

int main(){
	double x[12] = {135.1,73.1,63.2,59.1,59.1,53.1,36.1,35.1,31.8,24.6,24.1,50.2};
	double sum = 0;
	for (int i = 0; i < 11; i++)
		sum += x[i];
	std::cout << std::setprecision(42) << std::fixed << sum << std::endl;
}
594.400000000000090949470177292823791503906250
Topic archived. No new replies allowed.