Here is the assignmnent I need to complete:

"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)."

I've gotten the part of the program using the "double" variables to execute, but whenever I slip in the "float" variables part of the program, the window is blank when I try to run it. I know a float variable can only hold 7 digits, but I'm not sure how to resolve this. I appreciate any tips.

#include <iostream>
using namespace std;

void main ()
{
float f;
float fsum;
double i;
double Sum;


for (f = 1, fsum = 0; f <= 100000000; f++)
fsum = fsum + (1/f);	
cout << "The sum of all numbers 1/1 through 1/100000000 is " << fsum << endl;

for (f = 100000000, fsum = 0; f >= 1; f--)
fsum = fsum + (1/f);	
cout << "The sum of all numbers 1/100000000 through 1/1 is " << fsum << endl;

for (i = 1, Sum = 0; i <= 100000000; i++)
Sum = Sum + (1/i);	
cout << "The sum of all numbers 1/1 through 1/100000000 is " << Sum << endl;

for (i = 100000000, Sum = 0; i >= 1; i--)
Sum = Sum + (1/i);	
cout << "The sum of all numbers 1/100000000 through 1/1 is " << Sum << endl;

Try declaring your variables f and i as intergers. Maybe you have to change a little your formula (fsum = fsum + (1/f)). Look into type casting.

Here is the assignmnent I need to complete:

"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)."

I've gotten the part of the program using the "double" variables to execute, but whenever I slip in the "float" variables part of the program, the window is blank when I try to run it. I know a float variable can only hold 7 digits, but I'm not sure how to resolve this. I appreciate any tips.

#include <iostream>
using namespace std;

void main ()
{
float f;
float fsum;
double i;
double Sum;


for (f = 1, fsum = 0; f <= 100000000; f++)
fsum = fsum + (1/f);	
cout << "The sum of all numbers 1/1 through 1/100000000 is " << fsum << endl;

for (f = 100000000, fsum = 0; f >= 1; f--)
fsum = fsum + (1/f);	
cout << "The sum of all numbers 1/100000000 through 1/1 is " << fsum << endl;

for (i = 1, Sum = 0; i <= 100000000; i++)
Sum = Sum + (1/i);	
cout << "The sum of all numbers 1/1 through 1/100000000 is " << Sum << endl;

for (i = 100000000, Sum = 0; i >= 1; i--)
Sum = Sum + (1/i);	
cout << "The sum of all numbers 1/100000000 through 1/1 is " << Sum << endl;

Your instructor means for you to use double. You perhaps heard him/her say "use floating point numbers" and assumed that it meant you had to declare the variables as float. Doubles are floats--it means double precision floating point number.

Your instructor means for you to use double.

Unless the exercize is to expose certain limitations of each format.

sizeof(float)  : 4
sizeof(double) : 8

I have to use both doubles and floats. The assignment states that I will display four answers -- two for doubles and two for floats.

The first thing is that you should always use integers to iterate in your for loops. In your example we need to go trough a loop 100000000 times. So we declare a variable as an integer and increase it every time we went trough the loop (i++).

for(int i=1; i=<100000000;i++)

is just fine and you don't need float or double for that.

The second problem is also about types. Try running the following code and see the result:

float sum = 1;
sum = sum + 1/2;
cout << sum;

You will see that the result is not as expected. It is because the result of 1/2 is automatically converted to an integer and only after that it is added
to "sum". To avoid this you could define a constant and declare it as float, for example:

float sum = 1;
const float one = 1;
const float two = 2;
sum = sum + one/two;
cout << sum;

You can see that the result will be different then the previous one. Because you declared "one" and "two" as floating point types, the result of operations whit these will also be a floating point type (not an integer as previous).

There are other ways you could do this (sum = sum + 1./2 would also work) but i prefer to always use constants or variables over blank numbers in a source code, it is clearer and if you have to change the numbers, you need to change them only in one place.

Hope i helped.

You're close with

I know a float variable can only hold 7 digits, ...

.
It can only represent about 7 significant decimal digits. Meaning that once you exceed 16,777,216 (just tested it here on my own machine), f++ doesn't do what you expect.

A simple alternative (iterating over integers as suggested by others) is:

int i;
float f, fsum;
for (i = 1, fsum = 0;  i < 100000000; i++) {
    f = i;
    fsum += 1/f;
}

Once you have it working, what can you say about which direction you iterate? Assuming you get different answers, which one is "correct" and why? (Hint: the answer is closely related to why your loop didn't work!)

Edited 5 Years Ago by raptr_dflo: declare f

This article has been dead for over six months. Start a new discussion instead.