Solved

Posted on 2006-04-09

I am totally stuck on a problem.The assignment is "Develop a calculator program that can add, subtract, multiply and divide. Using only addition and subtraction functions in Algorithms." I've got an entirely working program now using procedures for each but I can't get a straight answer out of the professor. All I can return is whole numbers in division and multiplication. ( I'm using a counter to subtract the second variable "X" amount of times in DIV and using a counter to add a number to itself "X" amount of times to multiply. ) What I am concerned with is if there is a better way to accomplish this that I could try. Is there some way to do this that will return decimals IE: 3 / 2 =1.5? I tried assuming a number of digits and multiplying the numbers by 1000 and then I still couldn't figure out how to divide back without divide.

15 Comments

And I think you cannot use similar algorithms for decimals. Probably you can for some particular cases but since they will not be universal it does not make much sense.

the task reads as you are not allowed to use the operators DIV, MOD and / . this would be really simple then - your program just has to act like youve learnt mathematics back in the 3rd or 4th class in school. simply use a loop of additions to do a multiplication and a loop of substractions (with zero-boundary-check) for division.

in case you want to display fractions (like 3/4) instead of decimals (like 0.75) this is rather impossible. you could test simple cases like when you have values like 0.75, 0.5... but even 0.33333~ is a problem because this is a matter of how floating point types work.

errm.. yes :) more input please

-> keep track of the counter value (string/output)

Then if the remaining part (resulting from multiple subtract op) is greater then 0, then there is decimals (add decimal point), and,

while the remaing is greater than 0, do :

- multiply remaing part by 10 (calling your multiply procedure),

- reset your counter to 0,

- count the number of substraction you can do now,

- keep track of the counter value (string/output) (new digit)

loop.

count = 0

substract loop

3-2 = 1 count = 1

end of loop (remain=1 < 2) -> 1

( remain = 1 ) -> .

remain := multiply(remain,10); (* 10 *)

decimals loop (while remain>0 and number of decimals <= max)

count=0

substract loop

10-2 = 8 count = 1

10-2 = 6 count = 2

10-2 = 4 count = 3

10-2 = 2 count = 4

10-2 = 0 count = 5

end of loop (remain=0 < 2) -> 5

end of loop (remain=0)

10-2 = 8 count = 1

8-2 = 6 count = 2

6-2 = 4 count = 3

4-2 = 2 count = 4

2-2 = 0 count = 5

First his algorithm for the division id not correct. See my post above.

Furthermore as a consequence, there is not a remaining part! He has to substract until he reaches zero or below zero.

In case he wants to use the last part of your suggestion (which will be quite slow and also will not work always) in case if the zero boundary is passed he has to add the divider to the negative value and after that with this result can proceed to your suggestion.

But I don't think that there is a lot of use in this.

>> (which will be quite slow and also will not work always)

1) not as slow as you think : processor instruction for division take more clock ticks than subtraction

2) that's the question : cannot use * and DIV. What's your solution ?

About the processor instructions, you're again not right. The multiplication and division was much slower in the past. Nowadays they take almost the same clock ticks.

btw I used to write in Assembler several years ago, so I have the knowledge for the processor instructions. :)

another correction of my comment:

remain := multiply(remain,10); (* 10 *)

decimals loop (while remain>0 and number of decimals <= max)

should be:

decimals loop (while remain>0 and number of decimals <= max)

remain := multiply(remain,10); (* multiply by 10 like you do by hand *)

writeln (Divide,'.',Remainderdivid

"I'm using a counter to subtract the second variable "X" amount of times in DIV" That is Where A / B = X

Example A=6 and B=3

looping counter while remainder > B

x = counter

6-3=3 counter = 1

3-3=0 counter = 2

Division works fine for whole numbers. I was mostly concerned that there was a conceptual point that I was missing that was keeping my answer in whole posative integers. So far it seems that the point that I was missing was that I could string both sides of the decimal together in the return. I tried to convert my numbers to binary digits and do the math that way but with the same result only more complicated. My thinking was that the processor doesn't multiply or divide and that would get my process working that way.

I would like to know that that doesn't seem possible to anybody else.

As for dividing, again , Everything works fine now. I am just not happy with the result. The intended question is not one of wether or not I can write an algorithm. It is wether my initial concept was keeping me from seeing a logical answer that would not have the same limitation in the return.

If ">" is replaced with ">=" in "looping counter while remainder > B" then yes, it will work this way but you haven't written it in the initial question.

And this is not right - "processor doesn't multiply or divide". Most of the processor in the world have instructions for multiplying and division. Including yours I suppose :)

But your initial concept is fine, this is a common way to solve the problem, so don't worry about it. :)

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

Delphi ADO query as a string | 4 | 2,484 | |

Using IdHTTP1.Get with Delphi and Indy 9/10 on a specific web server returns exception | 3 | 2,330 | |

creating manifest for my dll that called from activex | 6 | 79 | |

find a node in VST | 2 | 36 |

how to add IIS SMTP to handle application/Scanner relays into office 365.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**13** Experts available now in Live!