Addition of Rational Numbers (1)

  1. Write a program to calculate the addition of rational numbers.
  2. You need to develop a function add() to perform the addition, and a function print() to display a rational number.
  3. Test your functions with the following main program, in which the prototypes of add() and print() have been declared.
    
    void add(int n1, int d1, int n2, int d2, int& n3, int& d3);
    void print(int a, int b);
    
    int main()
    {
        const unsigned short N = 5;
        int a_numerator[N]   = { 1, 1, 1, 1, 1 };
        int a_denominator[N] = { 1, 2, 3, 4, 5 };
        int b_numerator[N]   = { 1, 2, 3, 4, 5 };
        int b_denominator[N] = { 1, 1, 1, 1, 1 };
        int c_numerator[N];
        int c_denominator[N];
        int i;
        for (i=0; i<N; i++)
        {
            add(a_numerator[i], a_denominator[i],
                   b_numerator[i], b_denominator[i],
                   c_numerator[i], c_denominator[i]);
            print(a_numerator[i], a_denominator[i]);
            cout << " + ";
            print(b_numerator[i], b_denominator[i]);
            cout << " = ";
            print(c_numerator[i], c_denominator[i]);
            cout << endl;
        }
        return 0;
    }