Computer problems,Computer help
*AX SOFT>>>Programming & Design

What's the code you'd use in a simplified division function?


I know for multiplying, a simplified function can use addition.

Ask for two numbers to be multiplied.

mult( x, y);

And then simply

Add x, y times.

But given two numbers, how could you carry out division in a similarly simplified manner? That is without using "/".

div(x,y)


say you wanted to do x / y


for example 9 / 2

int result = 0;
while(x > y){
x = x - y;
++result;
};

int remainder = x;

that would work like:

while 9 > 2

result = 1

x = 7;

while 7 > 2

result = 2

x = 5

while 5 > 2

result = 3;

while 3 > 2

result = 4

x = 1

while 1 > 2 is false, so loop breaks

remainder = 1


so the result is 4 remainder 1

So division just using subtraction? Okay, here you go in C++:

int divide ( int x, int divisor, int & remainder );
int divide ( int x, int divisor, int & remainder )
{
if ( x > divisor )
return 1 + divide ( x - divisor, divisor, remainder );
else
{
remainder = x;
return 0;
}
}

int main(int argc, char* argv[])
{
int remainder;
int result = divide ( 105, 8, remainder );
printf ( "105/8 = %d, remainder = %d\n", result, remainder );

getchar();
return 0;
}

If you are dividing a number by a power of two (i.e. x / 2^k), then by FAR the fastest divide function possible is to do a bit-wise right shift. In c/c++, this would be x>>k.

To divide normal numbers, do it like you did in fourth grade. Have an accumulator and subtract the dividend by the divisor, incrementing the accumulator each time, until the dividend is less than zero.

Example (c/c++):

int divide(int x, int y)
{
int result = 0;

while ( true )
{
x = x - y;
if ( x < 0 ) break; // jump out of the while loop

result++;
}

return result;
}

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • C++ help! why a number 1 keeps on popping up in my priming read?

    cout << "Enter region: " << regions; because of that ... The program is OK ... but there's some changes will make shorter ... instead of writing the 4 using:...

  • What's the difference between URL, Direct Link, HTML and IMG???

    url = universal resource location direct link = the direct link to the source html = hyper text transfer protocol img = image

    ...
  • Questions removed?

    Did it have any answers? If it expires, with no answers, a question gets deleted. If more than one person considered to be a "reliable user" reports a question, then it will automatic...

  • What would be the pseudo code for this piece of Visual Basic code that is underneath???

    start function Load if gfGetYellow is false then exit function for li as 1 to the last element of laParkData if the value of the li'th item at position 1 in laparkdata is not equal t...

  • What is a fair salary for an asp.net and C# programmer in north carolina?

    I am not sure about north carolina but I think the pay also depends on the work. Light asp and C# work in this economy you might be able to find someone for around $35k. Managing multiple sites wit...

  • Question for those who use google adsense?

    I have ad sense on 7 different websites. I have been extremely successful with ad sense. I make my living off of the add revenue . The revenue from each site it different. Revenue is base on the a...

  • For a Web design position. What are the advantages and disadvantages of pursuing this type of position?

    First, be clear about where your interests and talents lie. A web designer is someone with artistic ability who designs the site - layout, color scheme, fonts, graphics, etc. Often, web desig...

  • I need a website that factors polynomial equations using javascript so that i can convert it into ti-basic?

    I hope that this helps: ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster