Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ assist

Name: C++ Noob 2010-09-23 23:58

Ok so I am learning C++ and I suck.

Trying to add 1/1 + 1/2 + ... + 1/99 + 1/100

The problem is I can't get the denominator to increase as I attempt to add them.
Anyone care to point out my most likely obvious mistake?

This is what I have


#include <iostream>

using namespace std;

void main ()
    {
    const    long    MaxNum (100);
    double    i;
    double    Sum;
    double    x;
    for (x = 1, i = 1/1, Sum = 0; x <= MaxNum; x++)
        Sum += i;
    cout << "Sum is " << Sum << endl;
}

Name: Anonymous 2010-09-24 1:59

>>4

Sum = 0;
x = 1;
for (;;)
{
 Sum += 1/x;
 x++;
 if (x > MaxNum)
   break;
}

ENTERPRISE QUALITY READABILITY

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List