|
-
Nov 27th, 2000, 12:59 PM
#1
Thread Starter
Addicted Member
Hello,
I'm trying to multiply 2 fractions by using ONLY addition operations. Does anyone know how to do that?
Thanks.
-
Nov 27th, 2000, 01:23 PM
#2
Monday Morning Lunatic
What, apart from log tables, you mean?
How about something like:
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;
}
...damn that's messy. Why can't you use the multiplicative operators?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 27th, 2000, 01:33 PM
#3
Thread Starter
Addicted Member
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.
-
Nov 27th, 2000, 01:54 PM
#4
Monday Morning Lunatic
What are the instructions / object of the exercise?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 28th, 2000, 04:35 AM
#5
Thread Starter
Addicted Member
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.
-
Nov 29th, 2000, 01:20 PM
#6
Monday Morning Lunatic
Can the decimals be evenly expressed as p / q?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 29th, 2000, 02:04 PM
#7
Thread Starter
Addicted Member
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.
-
Nov 29th, 2000, 03:18 PM
#8
Monday Morning Lunatic
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
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 29th, 2000, 03:58 PM
#9
Frenzied Member
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
Harry.
"From one thing, know ten thousand things."
-
Nov 29th, 2000, 04:02 PM
#10
Frenzied Member
Unless of course you can output the result as a fraction, which is easy.
Harry.
"From one thing, know ten thousand things."
-
Dec 2nd, 2000, 06:00 AM
#11
transcendental analytic
i guess i'm screwed in C++ but i'll give myself a try
Code:
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]
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|