How do i make this program work 1/100

It says that the for-statement doesnt do anything
i try to find a k that 1/(k)+1/(k+1)+...+1/(k+1000) is smaller than 1/1000.
but i cant seem to get the damn thing to work.
How do you make it work?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
double testsum (int k){
double sum=0;
int i;
for (i=0;i++;i<=1000)
1/(k+i);
sum+1/i;
return 0;
}
int main(){
int k=1000000;
while(testsum(k)>=0.001)
k++;
cout << "The value is " << k;
}
Last edited on
A for loop is as follows:
for(declaration; conditional; incremental)

Your for loop is backwards, and it should be as follows:
for(i=0;i<=1000;i++)

Try that and see if you have the same problems.
Oh right! SHould have seen that. Thanks for the answer.
now i get the errors for line 7 and 8:
"ComeauTest.c", line 7: warning: expression has no effect
1/(k+i);
^

"ComeauTest.c", line 8: warning: expression has no effect
sum+1/i;
^

Seems like they have no effect?

thanks
You have no assignment operator, meaning there is no equals sign. All you're doing is creating a value and just letting it float of into emptiness.
How do i do that? so the program works

thanks
I try to make it so that when the testsum method is lower than 1/1000, the main program prints out k
What exactly are you trying to do? Do you have an actual problem you can show me? I don't understand from the three numbers you showed me.

Also, your function testsum will always return 0. This means no matter how many times you call it, you'll always get a 0 in return. Also, your while loop in your main will never run. Since you're always returning 0, the while loop never runs.

I can't understand what you're trying to do in your function either, so I can not correct your lack of assignment operators. Like I said, if you could provide clearer instructions, I may be able to help more.
How do i make it so it doesnt return 0? i want it to return the start value k

well the actual problem is:
i try to find a k that 1/(k)+1/(k+1)+...+1/(k+1000) is smaller than 1/1000.
the sum consists of 1001 terms.
the start value of k is 1000000. so the first term is 1/100000. 1000 terms later, the last term is 1/1001000. if that sum is bigger than 1/1000, the program is supposed to start figuring out if 1/1001001+ 1000 terms is smaller than 1/1000. when it finds a start value (k) that gives a sum smaller than 1/1000, the program prints out that start value.

Thanks
Last edited on
I believe this is more of what you wanted for your function:
1
2
3
4
5
6
7
double testsum (int k) {
    double sum=0;
    // the sum consists of 1001 terms.
    for (double i = 0; i <= 1000; i ++)
        sum += (1/(k+i));
    return sum;
}


And at this spot:
1
2
    while(testsum(k)>=0.001)
        k++;


Your instructions tell you the next "k" to test for is 1001001, not 1000001.
Thanks for the help

ill check it out tomorrow, gotta get some sleep...
the book i read says you dont have to repeat any terms... if 1000000 to 1001000 didnt work you can go right away to start value 1001001. so you dont have to try the start values 100001, 1000002 etc. dont know the mathematical reason for it but thats how it goes i suppose.

EDIT: on a closer look, that cant be right though. because there are smaller start values than 1001000 that give a smaller sum than 1/1000. for example 1000400. i guess the book must be wrong about that for some reason
Last edited on
sorry for bothering... how do you make it so the next k is 1000001, instead of 1001001?

thanks
Well k++ will increase the value of k by one. If you want to increase k by more than one, you'd use k += n where n is the amount you want to increase k by.
Ok, thanks for the help!
Last edited on
Topic archived. No new replies allowed.