Hello,
I'm trying to multiply 2 fractions by using ONLY addition operations. Does anyone know how to do that?
Thanks.
Printable View
Hello,
I'm trying to multiply 2 fractions by using ONLY addition operations. Does anyone know how to do that?
Thanks.
What, apart from log tables, you mean?
How about something like:
...damn that's messy. Why can't you use the multiplicative operators?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;
}
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.
What are the instructions / object of the exercise?
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.
Can the decimals be evenly expressed as p / q?
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.
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 :(
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 ;)
Unless of course you can output the result as a fraction, which is easy.
i guess i'm screwed in C++ but i'll give myself a try
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 tooCode: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;
}
[Edited by kedaman on 12-02-2000 at 06:03 AM]