Solved

# Sums the series

Posted on 2000-04-08

I wrote two programs that sums the series 1 (1/2) + (1/2) +... + (1/2)

in left-to-right order, and in right-to-left order. Are the results the same or different?

Explain why.

My programs:

First Program

#include <iostream.h>

main(){

double Sum = 0;

long double Denominator = 1.2676506E30;

double Numerator = 1;

int ValuesProcessed = 0;

double Fraction;

while (ValuesProcessed < 100)

{

Fraction = Numerator / Denominator;

Sum += Fraction;

Denominator /= 2;

++ValuesProcessed;

}

cout << "Sum is: " << Sum << endl;

cout << "Values is: " << ValuesProcessed << endl;

return 0;

}

Second program

#include <iostream.h>

main(){

double Sum = 0;

long double Denominator = 1;

double Numerator = 1;

int ValuesProcessed = 0;

double Fraction;

while (ValuesProcessed < 100)

{

Fraction = Numerator / Denominator;

Sum += Fraction;

Denominator *= 2;

++ValuesProcessed;

}

cout << "Sum is: " << Sum << endl;

cout << "Values is: " << ValuesProcessed << endl;

return 0;

}

Thanks,

Jairo Cardenas

Colombia