Thread: Russian Peasant Multiplication Alogrithm using 'for' loops

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Russian Peasant Multiplication Alogrithm using 'for' loops

    How to create C program for the Russian Peasant Multiplication Algorithm using for loops ,,, ??

    Check out my program below ,,, which doesn't multiply right upon execution ,,, !
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void main ()
    	  {
    	   int val_1,val_2,lesser,greater,result=0;
    	   clrscr ();
    	   printf("Russian Peasant Multiplication Algorithm\n");
    	   printf("\nEnter multiplier:");
    	   scanf("%d",&val_1);
    	   printf("\nEnter multiplicand:");
    	   scanf("%d",&val_2);
    	   greater=((lesser=val_1<val_2?val_1:val_2)==val_1)?val_2:val_1;
    	   for (lesser;lesser/=2;greater*=2)
    	       {
    	         result+=lesser%2?greater:0;
                    }
     printf("%d\n",result);
     getch();
    
     }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Someone gave you that solution on another forum, right?

    There is no way you can convince me that you don't know C and you wrote that - or someone has been teaching you some VERY bad habits.

    Code:
    	   greater=((lesser=val_1<val_2?val_1:val_2)==val_1)?val_2:val_1;
    That's so obfuscated that I can't immediately make out what it does, and I can easily say that I believe I'm in the top 20 of the regulars here when it comes to writing and understanding obscure/obfuscated code.

    You will NOT learn to program by copying code like this.

    Use regular if/else statements for this sort of thing!

    But if you really want to fix the existing unreadable mess, your for-statement should be this:
    Code:
    	for (;lesser;lesser/=2,greater*=2)
    Then it works (at least for 22 * 35, which was the test-case I found on a web-site somewhere).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by matsp
    Someone gave you that solution on another forum, right?

    There is no way you can convince me that you don't know C and you wrote that - or someone has been teaching you some VERY bad habits.
    Why the hell is it so common to treat a newcomer\noobs in such a negative way ,,, ?!
    Why on Earth would you think that I would copy from other sites ,,, ??

    greater=((lesser=val_1<val_2?val_1:val_2)==val_1)? val_2:val_1;
    It's the given in my text book ,,, ! and in the exercise it says for some modification to the program ,,, but I was stuck with the for loop problem ,,, you see ,,,
    I would modify that ternary operator to if-else ,,, but my exercise directed me not to do so ,,,
    Now my apologies ,,, I will try fixing such misunderstand codes while copying from my .cpp file ,,,

    Now back to the topic ,,,
    Thanks alot ,,, it worked ,,, !
    isn't middle part of for loop supposed to end that loop ,,, hence isn't it appropriate for lesser/=2which ends the loop after its divisions ,,, ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cyansnow View Post
    Why the hell is it so common to treat a newcomer\noobs in such a negative way ,,, ?!
    Because people are lazy.
    Quote Originally Posted by Cyansnow View Post
    Why on Earth would you think that I would copy from other sites ,,, ??
    Because people are lazy.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cyansnow View Post
    Now back to the topic ,,,
    Thanks alot ,,, it worked ,,, !
    isn't middle part of for loop supposed to end that loop ,,, hence isn't it appropriate for lesser/=2which ends the loop after its divisions ,,, ?
    If that REALLY is what your book says, I would STRONGLY suggest getting a different book. That code has ABSOLUTELY no place in a teaching book. I'm not surprised you are asking for a tutor if this is the typical style of your book.

    So, whilst I'm sorry I suggest you got it from the internet, I'm right in saying that you didn't write the code yourself, which was more of my point. It just didn't occur to me that someone would produce such obfuscated code in a book intended for teaching (unless it's under the subject of "here's how not to do this").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    10
    kk ! Whatever ! You too try to be lazy and no need for off topic replies !

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cyansnow View Post
    kk ! Whatever ! You too try to be lazy and no need for off topic replies !
    Don't blame me for YOU going off topic. You asked a question, I answered it. If you don't want people going off topic, stop asking off topic questions.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by quzah
    Don't blame me for YOU going off topic. You asked a question, I answered it. If you don't want people going off topic, stop asking off topic questions.
    Oh sorry ! But it was directed to Matsp ! Look he still don't believe me !

    Matsp, why are you skeptic about that matter ?? You need me to explain how that ternary operator works or do you want me to convert it to if-else or do you want a page of it from my text book ?!
    And of course my text book maybe that bad for it's making me confuse at places ! But I can't change it as that book is a part of my bca degree subject !
    Last edited by Cyansnow; 05-21-2009 at 06:07 AM. Reason: added the reason cant change book

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cyansnow View Post
    Oh sorry ! But it was directed to Matsp ! Look he still don't believe me !

    Matsp, why are you skeptic about that matter ?? You need me to explain how that ternary operator works or do you want me to convert it to if-else or do you want a page of it from my text book ?!
    And of course my text book maybe that bad for it's making me confuse at places ! But I can't change it as that book is a part of my bca degree subject !
    My point is that IF you wrote that code (rather than copy it from the book), then you wrote it badly. If you copied it from the book, then the book is bad. And what I'm skeptical about isn't so much YOU as the book that you are quoting from. Is the author of this book connected with your university?

    For one thing, assigning two values in one statement is definitely not a good thing - it is very hard to debug such a statement, since any C level debugger will see it as ONE statement. _IF_ there is a bug in the statement, then you can only see what happens at the end. If you split it into two statements, you can at least see what it does in each of the two assignments separately.

    I'd still suggest that you look at the book suggestions section, in order to get a second C book, and use that to help you understand things better. Most books cover the same things in roughly the same order, so you should be able to correlate chapter X in your book with chapter Y in another book.

    But I think we have solved the issue itself - even if the whole code is horribly written.

    [It actually looks very similar to something I could be posting as a "joke" answer to someone asking for homework solutions].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by matsp
    Is the author of this book connected with your university?
    Yes.

    Quote Originally Posted by matsp
    assigning two values in one statement is definitely not a good thing
    I had apologized against it ! Here I say SORRY again !

    Quote Originally Posted by matsp
    I'd still suggest that you look at the book suggestions section, in order to get a second C book
    It's too late now! It seems I won't make it on my exams !
    I will try buy it after my exams !

Popular pages Recent additions subscribe to a feed