Multiplying without the astrerisk

This is a discussion on Multiplying without the astrerisk within the C++ Programming forums, part of the General Programming Boards category; Rewrite the following function so that the following conditions are satisfied: A) the multiplication operator ('*') is not used. B) ...

  1. #1
    grmoo
    Guest

    Multiplying without the astrerisk

    Rewrite the following function so that the following conditions are
    satisfied:
    A) the multiplication operator ('*') is not used.
    B) the division operator ('/') is not used.
    C) the function does not make use of loops.
    D) And no sneaky using assembly either!

    int MultiplyBy321(int value)
    {
    int rslt;

    rslt = value * 321;

    return rslt;
    }

    how would something like that be done?

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    The first thing would be to pay attention to your teacher in the future so you don't have to ask other people to do your homework. The second would be to look up bit shifting and break 321 down into bitshiftable bites.

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well how abt using goto statements.. It is some what a loop.. can GOTO be used..

  4. #4
    grmoo
    Guest
    this isnt for school or anything like that this is for my own use, it is on that gearbox test that i requested and i have no idea how to do it....and i think using the goto statement would be considered a loop

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    The only way a goto would work in this situation would be ugly as sin (same as using a for loop to "fake" it). Look up bitshifting. Salem's example is right on for what I'm talking about. Bitshifting gives the same outcome as multiplying but is faster and only works when one of the numbers is a power of 2 (2,4,8,16,etc).

    For numbers that aren't (like 321), you have to break it down into ones that are and add them up.

  6. #6
    grmoo
    Guest
    does anyone know of an online tutorial for that?

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by grmoo
    does anyone know of an online tutorial for that?
    Read the FAQ
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    321 =>
    256 + 64 + 1 =>
    2^8 + 2^6 + 2^0 =>
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    Or "cheat"
    result = pow(10, log10(val)+log10(321) );
    I think it is the best idea I've ever seen.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int MultiplyBy321(int Number)
    {
      return (Number + Number + Number + ... + Number); //321 times
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 05:53 PM
  2. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 01:34 PM
  3. Algorithm for multiplying linked lists?
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 11:59 AM
  4. multiplying by 321 without using * or / operators
    By Silvercord in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2003, 05:47 AM

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