Results 1 to 11 of 11

Thread: Multiply Numbers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253

    Question

    Hello,

    I'm trying to multiply 2 fractions by using ONLY addition operations. Does anyone know how to do that?

    Thanks.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What, apart from log tables, you mean?

    How about something like:
    Code:
    int numA, denA, numB, denB, numR, denR;
    int i;
    
    numA = 2; // two fifths
    denA = 5;
    numB = 3; // three quarters
    denB = 4;
    
    numR = numA;
    denR = denB;
    
    for(i = 1; i <= numB; i++) {
        numR += numR;
    }
    for(i = 1; i <= denB; i++) {
        denR += denR;
    }
    ...damn that's messy. Why can't you use the multiplicative operators?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253

    Question

    I thought about your solution, but the problem is that I have 2 decimal fractions (the user needs to enter them) and I can't use the "*" operator or something similar because these are the instructions of the exercise I have to do.

    Do you have another solution?

    Thanks.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What are the instructions / object of the exercise?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253

    Question

    I need to write a function that returns the multiplication result of 2 numbers using only addition operations. I know how to do that if at least one of the numbers is integer, but if both are fractions, what can I do?

    Thanks.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Can the decimals be evenly expressed as p / q?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Posts
    253
    I think the user cannot enter them as "p/q", but maybe I can convert them to this form with string operations, although I'm not sure I can use string operations in the code of the exercise.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    All I can say is - I really have no idea. (Anyway, it's much quicker to multiply than attempt to use addition, so why bother?)

    Who came up with this obviously contrived question? Do the right thing...tell them NO...okay, maybe that's not a good idea.

    I still don't know
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you have a rational number, you should be able to devise an algorithm to express the number as a fraction. For instance, given the number 34.21, you can shift the decimal point right 2 places and you have the fraction 3421/100. A similar but slightly more complicated method can be used to convert recurring numbers to integral fractions, but since the user can't enter them it doesn't matter

    Can you only use addition? What about division & subtraction? If you've got division then it's easy, if not then you can do it with subtraction. If you don't have subtracion then I would think that, technically speaking, you're screwed
    Harry.

    "From one thing, know ten thousand things."

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Unless of course you can output the result as a fraction, which is easy.
    Harry.

    "From one thing, know ten thousand things."

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    i guess i'm screwed in C++ but i'll give myself a try
    Code:
    while( round(X)!=X){
      X = X + X + X + X + X + X + X + X + X + X;
      C++;
    }
    while( round(Y)!=Y){
      Y = Y + Y + Y + Y + Y + Y + Y + Y + Y + Y;
      C++;
    }
    if (Y<0){
      Y=-Y;
      X=-X;
    }
    
    for(i = 0; i < Y; i++) {
        R += X;
    }
    Well then comes the problem i think harry was talking about. If you could make it a string and then put the "." at position C from right, but i don't know how to do that in C++. It would be easy with division too

    [Edited by kedaman on 12-02-2000 at 06:03 AM]
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width