Express Rational Number as ADT(Abstract Data Type) written by Nischal Lal Shrestha.
Image of Nischal who is author of the post.

By Nischal Lal Shrestha

On Fri 08 June 2018

Express Rational Number as ADT(Abstract Data Type)

Expressing a Data Type('X') in terms of its mathematical and logical properties provides a tool for a programmer who wishes to use the data type correctly.

Abstract Data Type(ADT) is a mathematical model of a data structures that specify

  • types of data stored
  • operations allowed on them
  • types of parameters of operation.

So while expressing a data type we need to touch each of the above details.

One thing we need to know that there is no universal way to express a 'X'(here X = Rational Number) as ADT(Abstract Data Type). So you can borrow notations from anything you are fond of, and suits the expression model.

Rational 101

In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, a numerator p and, a non-zero denominator q.

Addition
Two fractions are added as follows:

    a/b + c/d = (ad + bc)/bd;

Note down this property of Rational Data type, as we are going to use it later.

Express Rational Number as ADT

Note:: Precondition = Necessary Condition for Operation

<START ADT DEFINATION>
    ADT Name = Rational;
    int Num; /* Holds Numerator Value */
    int Den; /* Holds Denominator */

        <START PROCESS>
            Process Name = Addition;
            Return Type = Rational Sum;
            Parameter = Rational A, Rational B;

                <return Sum> ADD(A,B)
                    Rational A,B;
                    Post-Condition: Sum[0] = A[0] x B[1] x A[1] x B[0];
                                    Sum[1] = A[1] x B[1]
                    return Sum;

        </ END PROCESS>

        <START PROCESS>
            Process Name = Multiplication;
            ....
            ....

        </ END PROCESS>

        <START PROCESS>
            Process Name = makeRational;
            ....
            ....

        </ END PROCESS>

</ END ADT DEFINATION>

It was a simple implementation of Addition operation. Similar you can implement

  1. isEqual operation
  2. makeRational operation
  3. Rational Multiplication
  4. Rational Subtraction
  5. Ration Division

Comments