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>
usingnamespace 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;
}
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;
^
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.
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
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.