well tabstop regarded that i need to discard of my remainders. so idk?
This is a discussion on Russian Peasent within the C Programming forums, part of the General Programming Boards category; well tabstop regarded that i need to discard of my remainders. so idk?...
well tabstop regarded that i need to discard of my remainders. so idk?
ok so ive simplfied the code down to:but it still doesnt work! what am i doing wrongCode:#include <FPT.h> int main() { double x,y,a; x=inD(); y=inD(); a=0; while(floor(y/2)!=1){ if(fmod(x,2)!=0){ a=a+x; x=x*2; y=(y/2); } else{ x=x*2; y=(y/2); } } outD(a); }
Yeah, ignore my post about precision completely. Just use integers all around. Unless you need x to support decimals, in which case y need only be an integer.
ok so i made x,y integers..but im getting 0 in most inputs as answers and 7 for x=7 and y=7.... wth?Code:#include <FPT.h> int main() { double a; int x,y; x=inD(); y=inD(); a=0; while(floor(y/2)!=1){ if(fmod(x,2)!=0){ a=a+x; x=x*2; y=(y/2); } else{ x=x*2; y=(y/2); } } outD(a); }
Well if x is an int, why not just use the % operator. And the previous code didn't address the issue tabstop mentioned.
Now your algorithm has also changed a little bit. What is the result supposed to be? Obviously at some point you are probably just multipling by zero. Or your loop exits too soon. So back to my point about seeing what the result of your loop should be.
Since floor(y/2) != 1 is false when y = 2 or 3, you are quitting too soon.
well if i enter 3 and 3 it should come out to 9 i presume?
Awe man... I thought the bold "or" would be a subtle enough hint.
I'm not sure what OutD does, but I don't think you can just output any old integer as a double and get the right number. It needs to be multiplied by 1.0 or something to convert it right first.
I'll stress that peasant multiplication only works with integers anyway so why not just dump the concept of floating point altogether.
SO what should my loop condition be?
Did you read the post with the algorithm in it (or read your textbook/reference with the algorithm in it)? Did you see what it says right before "then stop"?
so ive modified my condtion which i thought would solve the problem but i guess it didnt.Code:#include <FPT.h> int main() { double a; int x,y; x=inD(); y=inD(); a=0; while(floor(y/2)>1){ if(fmod(x,2)!=0){ a=a+x; x=x*2; y=(y/2); } else{ x=x*2; y=(y/2); } } outD(a); }
uhhhh... 1? OR 2. i changed it to equal too but it doesnt work
0, 1, 2, and 3.
Since the loop should only stop when y equals 0 (you definitely need to add the last term in when y equals 1) why the heck don't you just compare y to 0?
(Oh, and in your checking-for-odd thing, you need to be checking that y is odd, not x. I don't know how we've managed to miss that all this time.)