# Pascal calculator project. Multiplying and dividing using Addition and subtraction.

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.

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
About the division I think you're wrong (at least the way you've described it): you have to substract the smaller number from the greater one until the result becomes zero or less. And then in the case you reach exactly zero the number of subtractions will be the answer. In the case if you pass zero the answer will be the number of subtractions minus 1.
0
Commented:
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.
0
Commented:
im not quite sure if i understood everything. homework itself is not allowed here on EE, but since youre not questioning for the project itself, youre on the right way :) can you post us the code youre using for multiplication and division?

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
0
Commented:
For decimal division, your algorithm is ok for the integer part.
-> 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.

0
Commented:
Calculating 3/2, for example
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)

0
Commented:
correction, in decimals loop :

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
0
Commented:
Webstorm, you're wrong.
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.
0
Commented:

>> (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 ?

0
Commented:
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. :)
0
Commented:

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   *)

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Webstorm, You understood exactly what I was asking. I think that you hit on the point that I may have been missing. I am basically doing the same counter that I am already using but adding
writeln (Divide,'.',Remainderdivided); to have the answer show as a decimal and calculate left and right of the decimal seperately. I think that makes sense to me. That is what you were getting at, right?
0
Commented:
You first have to correct yoi division. It doesn't seem right the way you've described it.
0
Author Commented:
My division is fine. As I said, I already have a working program. I was just worried that I a was only returning whole numbers.
"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.

0
Commented:
Hi,
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. :)
0
Commented:
I suggest to split the points: WelkinMaze and Webstorm
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.