• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

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.

0
Geekout
Asked:
Geekout
  • 6
  • 6
  • 2
  • +1
2 Solutions
 
WelkinMazeCommented:
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
 
WelkinMazeCommented:
Furthermore your multiplication seems correct.
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
 
AmigoJackCommented:
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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
WebstormCommented:
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
 
WebstormCommented:
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
 
WebstormCommented:
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
 
WelkinMazeCommented:
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
 
WebstormCommented:

>> (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
 
WelkinMazeCommented:
About the solution - just read my posts above.
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
 
WebstormCommented:

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
 
GeekoutAuthor 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
 
WelkinMazeCommented:
You first have to correct yoi division. It doesn't seem right the way you've described it.
0
 
GeekoutAuthor 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
 
WelkinMazeCommented:
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
 
WebstormCommented:
I suggest to split the points: WelkinMaze and Webstorm
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now