Recklein3

09-16-2009, 06:34 AM

This is what I have so far:

#include <iostream>

using namespace std;

void main ()

{

float first_num,sum;

long double second_num,sum2;

float third_num,sum3;

long double fourth_num,sum4;

sum = 0;

first_num = 1;

while (first_num <= 1000000)

{

sum = sum + 1/first_num;

first_num++;

}

cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) : " << sum << endl;

sum2 = 0;

second_num = 1;

while (second_num <= 1000000)

{

sum2 = sum2 + 1/second_num;

second_num++;

}

cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) : " << sum2 << endl;

sum3 = 0;

third_num = 1000000;

while (third_num >= 1)

{

sum3 = sum3 + 1/third_num;

third_num--;

}

cout << "3.) The sum of the numbers 1/100000000 + ... + 1/1 (Float) : " << sum3 << endl;

sum4 = 0;

fourth_num = 1000000;

while (fourth_num >= 1)

{

sum4 = sum4 + 1/fourth_num;

fourth_num--;

}

cout << "4.) The sum of the numbers 1/100000000 + ... + 1/1 (Double) : " << sum4 << endl;

}

This is what the program is suppose to do:

Write a program to calculate and print the result of the following two operations:

1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/99999999 + 1/100000000

and

1/100000000 + 1/99999999 + 1/99999998 + 1/99999997 + ... + 1/3 + 1/2 + 1/1

Results should be totaled and printed once using float variables for each calculation and once using double variables for each calculation. There should be a total of four answers printed. Make sure that the answers are adequately labeled. You may use any of the three loop mechanisms (while, do-while, or for).

Here's the problem:

Can't put 100,000,000 without the program halting when I run it (1,000,000 works).

Why is that?

Also wondering why the three last sums are different from the first when I do put 1,000,000?

Final question , how can i take it another step and put it all into one loop?

Thanks.

#include <iostream>

using namespace std;

void main ()

{

float first_num,sum;

long double second_num,sum2;

float third_num,sum3;

long double fourth_num,sum4;

sum = 0;

first_num = 1;

while (first_num <= 1000000)

{

sum = sum + 1/first_num;

first_num++;

}

cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) : " << sum << endl;

sum2 = 0;

second_num = 1;

while (second_num <= 1000000)

{

sum2 = sum2 + 1/second_num;

second_num++;

}

cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) : " << sum2 << endl;

sum3 = 0;

third_num = 1000000;

while (third_num >= 1)

{

sum3 = sum3 + 1/third_num;

third_num--;

}

cout << "3.) The sum of the numbers 1/100000000 + ... + 1/1 (Float) : " << sum3 << endl;

sum4 = 0;

fourth_num = 1000000;

while (fourth_num >= 1)

{

sum4 = sum4 + 1/fourth_num;

fourth_num--;

}

cout << "4.) The sum of the numbers 1/100000000 + ... + 1/1 (Double) : " << sum4 << endl;

}

This is what the program is suppose to do:

Write a program to calculate and print the result of the following two operations:

1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/99999999 + 1/100000000

and

1/100000000 + 1/99999999 + 1/99999998 + 1/99999997 + ... + 1/3 + 1/2 + 1/1

Results should be totaled and printed once using float variables for each calculation and once using double variables for each calculation. There should be a total of four answers printed. Make sure that the answers are adequately labeled. You may use any of the three loop mechanisms (while, do-while, or for).

Here's the problem:

Can't put 100,000,000 without the program halting when I run it (1,000,000 works).

Why is that?

Also wondering why the three last sums are different from the first when I do put 1,000,000?

Final question , how can i take it another step and put it all into one loop?

Thanks.