Author Topic: Divide Function and Multiply Function.  (Read 3427 times)

Zorb Burger

  • Visitor
  • *
  • Posts: 4
    • View Profile
Divide Function and Multiply Function.
« on: 2009-05-04, 12:15:26 PM »
Greetings everyone.

I have two questions for anyone willing to try them (they're not very difficult).  They are programming questions.  Basically, you need to fill out these functions.  I'll provide comments on what they should accomplish, though they should be self explanatory.

/* First question */
// This function takes in a number and multiplies it by 7...pretty simple.
// However, you can not use the "*" (multiply) operator.  Try to make it as concise
// as possible.
int mult7(int num)
{
}

/* Second question */
// This function takes in a numerator and a denominator and must perform
// division.  This one was a little convoluted for me the first time trying to
// crack it, but it made sense in the end.  Oh yeah... you can use the "/" (divide)
// and the "*" (multiply) operators. One more thing, this is integer division, so don't
// worry about integer truncation.  Ex.  7/2 = 3 not 3.5.
int divide(int num, int den)
{
}

All right, this is suppose to be fun, so have at it, y'all.

Edit:
Topic was just called "Divide Function"...had to include the multiplication one.

~Zorb Burger

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Divide Function and Multiply Function.
« Reply #1 on: 2009-05-04, 02:47:03 PM »
Code: [Select]
int mult7 (int i) {
   return 2*(i<<2)-i;
}

Do you mean I can or cannot use the multiply and divide functions for the second one?  (You said I can)

Code: (uses divide) [Select]
int divide(int n, int d) {
   return n/d;
}

Code: (doesn't use divide, causes infinite loop on d==0) [Select]
int divide(int n, int d) {
   int i=0,a=Math.Sign(n)!=Math.Sign(d)?-1:1;
   for (n=Math.Abs(n),d=Math.Abs(d);n>d;i+=a,n-=d) ;
   return i;
}
« Last Edit: 2009-05-04, 03:01:37 PM by durnurd »
Edward Dassmesser

Zorb Burger

  • Visitor
  • *
  • Posts: 4
    • View Profile
Re: Divide Function and Multiply Function.
« Reply #2 on: 2009-05-04, 05:23:18 PM »
DAAYUUMM!!

*sigh*

I knew I messed something up in there... I meant that you could not use the multiplication and division operators...for both of them.

Also, about the mult7 function, it can be reduced further:

int mult7(int num)
{
     return (num<<3) - num;
}

and here's what I got for the divide:

int divide(int num, int den)
{
   int orig = den;
   int shifts = 0;
   while( num > den)
   {
      den <<= 1;
      ++shifts;
   }
   den >>= 1; --shifts;
   int count = 0;
   while(den > orig && num > den)
   {
      count += 1 << shifts;
      num -= dem;
      dem >>= 1;
   }
   if( num > dem )
      ++count;
   return count;
}

Thanks for participating in the questions, durnurd.  I figured you'd try it out.
Well...the next question I have will deal with strings.  :)

// Basically this function calculates the length of the null terminated string.
// You cannot use any functions that you didn't make yourself and you can't
// use loops.
int strlen(char *sz)
{
}

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Divide Function and Multiply Function.
« Reply #3 on: 2009-05-04, 08:55:35 PM »
I'm curious, why are you asking these questions?

Also, I'm compelled to point out that your divide function does not seem to work in at least some cases.  55 / 5 should return 11, but it returns 8.  I found a solution that works, but I don't know if I "dare" to post it without knowing why we're doing this :).

For your string question, is a recursive function considered a loop?

Zorb Burger

  • Visitor
  • *
  • Posts: 4
    • View Profile
Re: Divide Function and Multiply Function.
« Reply #4 on: 2009-05-04, 09:09:01 PM »
Hey, bluemonkmn.

The reason why I'm asking these questions is simply for fun.  I was asked the questions on an entrance exam for EA Games.  I've already gone through programming "boot camp" so I'm not trying to get answers for a test or anything.

Also, a recursive function iis allowed, just not "for-loops", "while-loops" and "do-while loops".  That's a very nice way of figuring out that question.

And about my way of doing the divide question, I'm going to need look over that... I thought for sure it worked, but nobody's perfect.

~Zorb Burger

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Divide Function and Multiply Function.
« Reply #5 on: 2009-05-05, 05:26:17 AM »
Here's my solution to the Divide question:
Code: [Select]
int Divide(int num, int den)
{
   if (num < den)
      return 0;
   int result = 1;
   int shiftedDen = den;
   while (num >= shiftedDen)
   {
      shiftedDen <<= 1;
      result <<= 1;
   }
   shiftedDen >>= 1;
   result >>= 1;
   num -= shiftedDen;
   return result + Divide(num, den);
}

I could probably figure out a non-recursive solution, but recursion is easier, and this recursion won't occur more than 32 times (worst case) per request, so it's not too bad.

Here's my answer to strlen:
Code: [Select]
int strlen(char *sz)
{
   return (*sz == 0)?0:1+strlen(sz+1);
}

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Divide Function and Multiply Function.
« Reply #6 on: 2009-05-05, 07:28:52 AM »
Whoops, I probably could have figured out that mult7 question.  Why multiply by 2 if you're shifting already! silly me.
And I'd like to point out that my divide solution is more concise than the other solutions, regardless of silly things like execution speed.  pshaw! :)
Edward Dassmesser