Introduction to Programming - CS201 Spring 2003 Assignment 04

By    


Assignment No. 4


Deadline


Your assignment must be uploaded/submitted before or on 24th March 2003

Uploading instructions


Please view the assignment submission process document provided to you by the Virtual University to upload the assignment.

 

Rules for Marking


It should be clear that your assignment will not get any credit if:

o                   The assignment is submitted after due date
o                   The submitted assignment does not compileor run
o                   The assignment is copied


Objective


The objective of this assignment is to provide hands on experience of writing user–defined functions and utilizing some useful functions in the C header files. After the completion of this assignment you would be able to:

o                   Write your own functions in C language
o                   Use few functions of conio.c for a better user interface

Console Operations

C provides a facility of console (at command prompt) functions for better output of your program. Following are two useful functions:

o       clrscr ( ) ;

You can use this function whenever you want to clear the command prompt window. It will remove all the contents of the output window and then you can display anything you want on the cleared screen. It will be displayed on the top left corner and the rest of the screen will be cleared.

o       getch ( ) ;

You can use this function whenever you want to stop your program at a certain point. You should have noticed the system (“PAUSE”); function in your Dev C++ editors. It stops your program at the end and you receive a message
“Press any key to continue. . . .”

By using getch ( ); you can stop your program at any time. For example:
           
cout  << “Enter a character” << endl ;
getch ( ) ;
cout << “Virtual University” ;

            In the above example your program will display

                                      Output


Enter a character
_
Virtual University

Notice that the program will stop after printing “Enter a character”. It will not proceed until you press a character from the keyboard and then press Enter. After you have pressed any key followed by Enter from the keyboard it will display “Virtual University”.

Note

          Dev-C++ does not support conio.h. So you have to include conio.c in your program as a header file.

Rational Numbers

Rational Numbers are very important in mathematical computing. The Rational Numbers is in the form numerator / denominator. The arithmetic operations can be performed in the following way:

o       Addition

If A and B are two Rational Numbers then
           
A + B = ((Numerator of A * Denominator of B) + (Numerator of B * Denominator of A))
              ------------------------------------------------------------------------------------------------------
((Denominator of A * Denominator of B)

o       Subtraction

If A and B are two Rational Numbers then
           
A - B = ((Numerator of A * Denominator of B) - (Numerator of B * Denominator of A))
              ------------------------------------------------------------------------------------------------------
((Denominator of A * Denominator of B)

o       Multiplication

If A and B are two Rational Numbers then
           
A * B =          (Numerator of A * Numerator of B)
  -------------------------------------------------------
     (Denominator of A * Denominator of B)

o       Division

If A and B are two Rational Numbers then
           
A / B =              (Numerator of A * Denominator of B)
       -------------------------------------------------------
(Denominator of A * Numerator of B)

Note

          Negative numbers in denominators are not allowed so if there is a negative sign in the denominator of the result during the Arithmetic operations then the numerator and denominator both are multiplied by -1. Also if there is a 0 in the denominator then it is not a valid Rational Number.

Assignment

Write a program that performs the above mentioned operations on rational numbers with the help of above mentioned formulae.

The program should have

·        Four user–defined functions for the operations
·        clrscr ( ) and getch ( ) functions for better user interaction

The program should check the criteria:

  • If the user enters a zero in the denominator / numerator then the program should display Zero is not allowed in the denominator” else the resultant rational number is displayed

  • If there is a 1 in the denominator the program should display only the numerator of the result else the resultant rational number is displayed but the numerator remains 1

  • If there is a 0 in the numerator then it should display a 0 else the resultant rational number is displayed and the denominator is also set to zero

  • If there is a negative number in the denominator then both the numerator and denominator should be multiplied by -1 and then displayed else the resultant rational number is displayed.


The following menu of commands should be displayed at the start of execution.

Press 'a' to add two Rational numbers

Press 's' to subtract two Rational numbers

Press 'm' to multiply two Rational numbers

Press 'd' to divide two Rational numbers

Press 'q' to quit

Please enter your choice:   a

After the user selects a choice, prompt the user to enter the numbers/values on which the selected mathematical operation is to be performed, and then display the result. For example, if the user presses ‘a’ then your output should be similar to:

Enter numerator: 14
Enter denominator: 12

The rational number is:         14 / 12

Enter numerator: 4
Enter denominator: 2

The rational number is:         4 / 2

The sum is 76 / 24.

Press any key to continue . . .

After the menu if the user selects ‘s’

Enter numerator: 1
Enter denominator: 0

Zero is not allowed in denominator

Enter the number again


Enter numerator: 11
Enter denominator: 13

The rational number is:         11 / 13

Enter numerator: 4
Enter denominator: 1

The rational number is:         4

The difference is. - 41 / 13

Press any key to continue . . .

The screen should be cleared when the user presses any key and the main menu should be displayed again.

Read the above specifications and:

a.      Write a C program to solve the problem based on your flow-chart

Submit ONLY the .cpp file as your assignment.