Click to See Complete Forum and Search --> : Multiply Numbers
Forest Dragon
Nov 27th, 2000, 11:59 AM
Hello,
I'm trying to multiply 2 fractions by using ONLY addition operations. Does anyone know how to do that?
Thanks.
parksie
Nov 27th, 2000, 12:23 PM
What, apart from log tables, you mean?
How about something like:
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?
Forest Dragon
Nov 27th, 2000, 12:33 PM
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.
parksie
Nov 27th, 2000, 12:54 PM
What are the instructions / object of the exercise?
Forest Dragon
Nov 28th, 2000, 03:35 AM
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.
parksie
Nov 29th, 2000, 12:20 PM
Can the decimals be evenly expressed as p / q?
Forest Dragon
Nov 29th, 2000, 01:04 PM
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.
parksie
Nov 29th, 2000, 02:18 PM
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 :(
HarryW
Nov 29th, 2000, 02:58 PM
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 ;)
HarryW
Nov 29th, 2000, 03:02 PM
Unless of course you can output the result as a fraction, which is easy.
kedaman
Dec 2nd, 2000, 05:00 AM
i guess i'm screwed in C++ but i'll give myself a try
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]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.