For Loops Problem

First time doing for loops/nested for loops. The problem I'm trying to work out is this:
Write a program that computes the values of the sum
(1 +.5 + .33 +.25 + … 1/n) - ln(natural log) n
for 100<n<1000 with the step
of 100. Output should be arranged as a table.

I don't need help with the table, I need help understanding how the output of for loops works when they're nested.

Here's the total mess I'm working on so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    
    
    cout << "Integer" << "     " <<    "Output"<<endl;
    cout << " "<<endl;
    
    
        for (double i =1; i<1001; i++)
        {
            int sum = 1;
            for (double n=100; n<1001;n=n+100)
            {
                sum +=1/i;
                cout << n << " ----- " << sum - log(n)<< endl;
            }
            
        }
    
    return 0;
}


Obviously the output is totally wonky. I need it to output only 100,200,300etc. to 1000.

The "sum" variable I have going on is my attempt to solve the part about if 5=n, then the corresponding output is 1 + 1/2 + 1/3 + 1/4 + 1/5. I've had a hard time figuring out how to get it so it doesn't simply raise the denominator, but works in all the fractions below that denominator number.

Any help I'm very thankful for, I know this is a mess
I think this is what you're looking for. Is the result supposed to be approaching 0.57...? Your inside loop should be your outside loop. Also, i and n should be int, not double. sum has to be double or you'll have rounding problems. Take a look at this:

edit... Also, on line 19, you have to use 1.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cmath>

int main()
{
    for(int n = 100; n <= 1000; n+=100)
    {
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
        {
            sum+= (1.0/i);
        }
        //std::cout << "sum: " << sum << std::endl;
        std::cout << n << "---" << sum - log(n) << std::endl;
    }
    return 0;
}


And here's the output:

100---0.582207
200---0.579714
300---0.578881
400---0.578465
500---0.578215
600---0.578049
700---0.57793
800---0.577841
900---0.577771
1000---0.577716
Last edited on
thanks for the help mgoetschius. I'm not sure what kind of answer to expect, but I did change 'n' to start with a lower number (3) to see if the result was correct for (1 + .5 + .33) - log(3) and it's looking like it is. Few things I'll have to edit to make a proper table but it's looking alright.

How is it that there are two separate things going on with sum? Or am I misunderstanding what's happening there?
Last edited on
I'm not sure you're understanding whats happening, or I'm not understanding your question exactly. So I'll break things down and hopefully that will help. Here's what happens when you enter main()

1. int n is set to 100.
2. n is less than 1000 so the code inside the loop runs.
3. double sum is declared and set to 0.0.
4. int i is set to 0. it is less than n (which is 100) so the code inside the loop runs.
5. sum (which is 0) has 1.0/i added to it n times(100). so, sum = 1/1 + 1/2 + 1/3 +...+ 1/100
6. the inner loop ends and you're back in the outer loop.
7. there, n(100) is printed and sum - log(n) is printed
8. the outer loop ends and 100 is added to n so n now = 200
9. back to step 2 with n = 200

10. repeat until n > 1000

Does this answer your question?

Topic archived. No new replies allowed.