Docsity

Programming Assignment for Introduction to Computer Science | CS 1410, Assignments of Computer Science

Toggle sidebar
bg1
1
CS 1410 Introduction to Computer Science – CS2
Section 2: MWF 9:30 a.m. – 10:20 a.m.
Instructor: Xiaojun Qi
Programming Assignment #4
Given: Feb. 3, 2009
Due Date: February 13, 2009
Submit electronically via the eagle system before 11:59 p.m.
Total Points: 25 points
Introduction to Rational Numbers
A rational number is one that can be represented as a fraction whose numerator and denominator
are both integers. For example, 1.6875 can be represented as 27/16. The square root of 2, on the
other hand, cannot be represented as the ratio of two integers and thus is not rational.
Rational numbers can be useful in some computations. For example, the C++ statements
float x = 1.0 / 3.0;
float y = 3.0 * x;
might not yield a value of 1.0 for y because of the limitations on the accuracy of floating point
numbers (there are only so many bits allocated to hold the floating point number 0.33333...).
Storing x and y as rational numbers, with numerators and denominators stored separately,
provides a means of calculating × so that the product is exactly 1, not approximately 1.
Assignment
This assignment involves creating a class called Rational that performs arithmetic with
fractions.
This class uses integer variables to represent the private data members of the class: the numerator
and the denominator. It uses a constructor to initialize members of the class when it is declared.
The constructor contains default values in case no initial values are given and stores the fraction
in reduced form. For example, the fraction is stored in an object as
This class should provide public member functions for each of the following operations:
• Addition of two Rational numbers (objects). The result should be stored in reduced
form.
• Subtraction of two Rational numbers (objects). The result should be stored in reduced
form.
• Multiplication of two Rational numbers (objects). The result should be stored in
reduced form.
• Division of two Rational numbers (objects). The result should be stored in reduced
form.
pf3
pf4

Partial preview of the text

Download Programming Assignment for Introduction to Computer Science | CS 1410 and more Assignments Computer Science in PDF only on Docsity!

CS 1410 Introduction to Computer Science – CS

Section 2: MWF 9:30 a.m. – 10:20 a.m.

Instructor: Xiaojun Qi

Programming Assignment

Given: Feb. 3, 2009

Due Date: February 13, 2009

Submit electronically via the eagle system before 11:59 p.m.

Total Points: 25 points

Introduction to Rational Numbers

A rational number is one that can be represented as a fraction whose numerator and denominator

are both integers. For example, 1.6875 can be represented as 27/16. The square root of 2, on the

other hand, cannot be represented as the ratio of two integers and thus is not rational.

Rational numbers can be useful in some computations. For example, the C++ statements

float x = 1.0 / 3.0;

float y = 3.0 * x;

might not yield a value of 1.0 for y because of the limitations on the accuracy of floating point

numbers (there are only so many bits allocated to hold the floating point number 0.33333...).

Storing x and y as rational numbers, with numerators and denominators stored separately,

provides a means of calculating × so that the product is exactly 1, not approximately 1.

Assignment

This assignment involves creating a class called Rational that performs arithmetic with

fractions.

This class uses integer variables to represent the private data members of the class: the numerator

and the denominator. It uses a constructor to initialize members of the class when it is declared.

The constructor contains default values in case no initial values are given and stores the fraction

in reduced form. For example, the fraction is stored in an object as

This class should provide public member functions for each of the following operations:

• Addition of two Rational numbers (objects). The result should be stored in reduced

form.

• Subtraction of two Rational numbers (objects). The result should be stored in reduced

form.

• Multiplication of two Rational numbers (objects). The result should be stored in

reduced form.

• Division of two Rational numbers (objects). The result should be stored in reduced

form.

  • The reciprocal of a Rational is.
  • Taking a Rational to a power, is c times.
  • Printing Rational numbers in the form a/b where a is the numerator and b is the denominator.
  • Printing Rational numbers in floating-point format.

NOTES:

  • Use the following driver code to test your Rational class. You must also write additional tests to fully demonstrate your Rational class handles all cases.

#include #include "Rational.h" using namespace std;

int main() { Rational c(1, 3), d(7, 9), x; c.printRational(); cout << " + "; d.printRational(); x = c.addition(d); cout << " = "; x.printRational(); cout << “\n”; x.printRational(); cout << " = "; x.printRationalFloat(); cout << "\n\n"; c.printRational(); cout << " - "; d.printRational(); x = c.subtraction(d); cout << " = "; x.printRational(); cout << ’\n’; x.printRational(); cout << " = "; x.printRationalFloat(); cout << "\n\n"; c.printRational(); cout << " * "; d.printRational(); x = c.multiplication(d); cout << " = "; x.printRational(); cout << ’\n’; x.printRational(); cout << " = "; x.printRationalFloat(); cout << "\n\n"; c.printRational();

void Simplify(int &num, int &denom) { int gcd=ComputeGCD(num,denom);

num=num/gcd; denom=denom/gcd; }

int ComputeGCD(int num, int denom) {

int remainder= denom % num; while (remainder != 0) { denom = num; num = remainder; remainder= denom % num; }

return num; }