Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rational Numbers Let p and q be integers, with q being non-zero integer. A ratio

ID: 3592623 • Letter: R

Question

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 )= a c /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.

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;

}

}

Explanation / Answer

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 )= a c /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.

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;

}