Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

(RationalNumber Class) A rational number is a number that can be represented as the quotient of two
integers. For example, 1/3, 5/7, 7/2, and so forth are rational numbers (By 2/1, etc., we mean the
everyday meaning of the fraction, not the integer division this expression would produce in a C++
program.) In general a rational number is represented as a/b where a and b are the numerator and
the denominator, respectively, and the denominator does not equal to 0.
Suppose a/b and c/d are rational numbers. Arithmetic operations on rational numbers are defined
by the following rules:
a / b + c / d = (ad + bc) / bd
a / b - c / d = (ad - bc)/bd
a / b x c / d = ac / bd
(a / b) / (c / d) = ad / bc, where c / d ? 0
Rational numbers are compared as follows: a/b op c/d if ad op bc, where op is any of the
relational operations. For example, a/b < c/d if ad < bc.
In this exercise, define a class named as RationalNumber. Suppose x, y, and z are objects of type
RationalNumber. The class RationalNumber
? Includes private member variables
o numerator and denominator of type int.
o str of type char*. This member variable is a C string with the format
numerator/denominator
and is managed by new and delete.
? Includes a member function reduction to normalize the values stored so that, after any
operation (construction, initialization, assignment, any arithmetic operations, etc), the greatest
common divisor (gcd) of numerator and denominator is 1. For example, after reduction, 21/14
becomes 3/2, i.e., the values of numerator and denominator are 3 and 2, respectively, and
str is a C string “3/2”.
? Includes a constructor where the default values of the numerator and the denominator are 0
and 1, respectively.
? Includes a copy constructor, which performs deep copy of class member variables.
? Overloads the stream extraction (>>) and stream insertion (<<) operators.
o If the input is 4/6, the statement “cin >> x;” would store 2/3 into object x.
o If x is 2/3, the statement “cout << x;” would output “2/3” to stdout.
? Overloads the assignment operator (=), and the following arithmetic operators: +, -, *, /.
o For example, the statement “cout << x + y << endl;” sends the resulting value
of x + y in rational number format to stdout.
o For example, the statement “z = x + y;” assigns the sum of x and y into z in
rational number.
? Overloads the following operators: ==, !=, >, >=, <, <=.
Write a driver program called testRationalNumber.cpp to test each of the above functions and
overloaded operators in the RationalNumber class.
Additional requirements: 2

? Include your first name in output and screenshot.
? Add “const” to appropriate formal parameters, and member/nonmember functions.
? State purpose, pre-conditions, and post-conditions for each function.
? Add proper comments to code.
? Check boundary and error conditions without crash.
Turn in the following as separate files:
? RationalNumber.h: class declaration
? RationalNumber.cpp: class implementation
? testRationalNumber.cpp: driver program
? Screenshots of output

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9613083

Have any Question?


Related Questions in C/C++

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As