Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise #1 Rational Problem (25pts) Rational Numbers Let p and q be integers, with q being non-zero integer. A rational number n is a number

Exercise #1 Rational Problem (25pts) Rational Numbers Let p and q be integers, with q being non-zero integer. A rational number n is a number which can be expressed as the following: n= p q It should be noted that any integer can be expressed as a rational number, since the integer q may be equal to 1. As an example the double value in C++ 0.95 can be expressed as the following rational number: 19 20 Arithmetic operations can be performed on rational numbers. For instance two rational numbers can be added, subtracted from, multiplied and divided. When adding and subtracting two rational numbers, the denominators first need to be reduced to a common denominator. As an example: 5 4 + 1 6 = 15+2 12 = 17 12 When multiplying two rational numbers, the numerators and the denominators are multiplied separately. Using integers a, b, c, and d as an example: ( a b )*( c d )= ac b d When dividing two rational numbers, the second rational number is first inverted (known as its reciprocal) and then is multiplied with the first rational number like above:

( a b )( c d )=( a b )* ( d c )= a d b c It should be noted that rational numbers can be expressed in simplest form as well, for instance given the following rational number: 10 15 If both the numerator and the denominator are each divided by 5, we obtain the following reduced rational number: 2 3 Instructions For this assignment, you are to write a C++ program which will read in rational numbers using the following convention (as an example): 50 / 12 If in the code two integers n1 and d1 were used, after reading in the above from cin n1 would be set to 50 and n2 would be set to 12. (As a hint you can read the forward slash (/) into any arbitrary char data type.) Your program will prompt the user for two rational numbers. Your program will then display the following menu for the user: 1. ADDITION 2. SUBTRACTION 3. MULTIPLICATION 4. DIVISION If the user presses (1), the two rational numbers are added together. If the user presses (2) the two rational numbers are subtracted from each other. If the user presses (3) the two rational numbers are multiplied together. If the user presses (4) the two numbers are divided. After the answer is shown to the user, the user is prompted if he/she would like to run the program again. Requirements To receive full credit for this assignment, the following conditions must be met: If a user enters a zero value for either denominator, your program will loop until the user enters the rational number correctly. If the user enters an incorrect menu selection (correct values are 1, 2, 3 and 4), your program will loop until a correct value is entered.

The program will loop as many times necessary until the user decides to quit. The output of each operation is a rational number expressed in reduced lowest terms. To express this, the greatest common divisor (GCD) must be obtained from the numerator and the denominator. The following C++ code can be used to calculate the GCD (which can be used to divide both the numerator and the denominator to reduce the number): int gcd, p, q; for (int i=1; i<=p && i<=q; i++) { if((p%i == 0) && (q%i == 0)) { gcd=i; } } Sample Output THIS PROGRAM PERFORMS ARITHMETIC OPERATIONS ON RATIONAL NUMBERS. **************************************************************** Please enter the first rational number: 1 / 2 Please enter the second rational number: 1 / 6 OPERATIONS (1) ADDITION (2) SUBTRACTION (3) MULTIPLICATION (4) DIVISION Please select an operation (1, 2, 3 or 4): 4 1 / 2 % 1 / 6 = 3 / 1 Would you like to run this program again? N

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions