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