Russian Peasent

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?...

  1. #16
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    well tabstop regarded that i need to discard of my remainders. so idk?

  2. #17
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok so ive simplfied the code down to:
    Code:
    #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);
      }
    but it still doesnt work! what am i doing wrong

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  4. #19
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok so i made x,y integers..
    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);
      
      }
    but im getting 0 in most inputs as answers and 7 for x=7 and y=7.... wth?

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well if x is an int, why not just use the &#37; 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.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since floor(y/2) != 1 is false when y = 2 or 3, you are quitting too soon.

  7. #22
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    well if i enter 3 and 3 it should come out to 9 i presume?

  8. #23
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Awe man... I thought the bold "or" would be a subtle enough hint.

  9. #24
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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.

  10. #25
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    SO what should my loop condition be?

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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"?

  12. #27
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    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);
      
      }

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So for what y is floor(y/2)>1 false?

  14. #29
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    uhhhh... 1? OR 2. i changed it to equal too but it doesnt work

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-22-2009, 08:03 AM
  2. Russian student attacks Estonia
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-25-2008, 10:35 AM
  3. Russian stuffffffffff
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-22-2004, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21