02393 C++ Programming Exercises (Solution)

$ 29.99

Description

Assignment 6
Hand-in via https://dtu.codejudge.net/02393-e17/assignment/
A class for fractions of integers The goal of the exercise of this week is to implement a class of fractions of integers supporting some basic operations like addition and multiplication. You can use the following interface for the class as a starting point and modify it at will: class fraction {
private:
// Internal representation of a fraction as two integers int numerator; int denominator;
public:
// Class constructor fraction(int n, int d);
// Methods to update the fraction void add(fraction f); void mult(fraction f); void div(fraction f);
// Display method void display(void);
};
The main idea above is that a fraction is represented by two integers (the numerator a and the denominator b) and several methods are provided to support arithmetic operations on fractions. For example: fraction(int n, int m) constructs the fraction ; void add(fraction f) updates a fraction by adding fraction f to it. void mult(fraction f) updates a fraction by multiplying it by fraction f. void div(fraction f) updates a fraction by dividing it by fraction f.
Your program should read sequences of simple expressions from cin of the form: a / b + c / d, a / b * c / d or a / b div c / d and provide the
result as a simplified fraction. For example, if the input is
1 / 4 + 1 / 2
your program should provide the following output to cout
3 / 4
which results from which after simplification is .
Challenge The suggested internal representation poses some limits on the actual domain of integral numbers that the fraction class can cover. For example, the largest number would be INT1MAX and the smallest positive fraction would be
1 . A possible remedy to mitigate this would be to exploit the fact that every
INT MAX
integer can be represented by a product of prime numbers (see e.g. https:// en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic). This was indeed what we exploited in assignment 2. For example
137200 = 2 · 2 · 2 · 2 · 5 · 5 · 7 · 7 · 7
To avoid repeating the same prime number many times, we could equivalently represent 137200 with exponentiation of prime numbers:
137200 = 24 · 52 · 73
We could thus represent all the exponents in an array of exponents, where the i-th element of the array represents how many times the i-th prime number occurs. So the representation of 137200 would be
4 0 2 3 0 0 …
array index 0 1 2 3 4 5 … array corresponding prime number 2 3 5 7 11 13 …
Arithmetic operations can also exploit such representation. For instance, the multiplication of two numbers in such representation can be obtained by adding the corresponding exponents. For instance, multiplying the factorizations of 12 and 10 would be done as follows
primes 2 3 5
2 1 0
1 0 1
12
10
Adding exponents gives:
3 1 1
120
The challenge is to implement the class of fractions with this representation technique.
2

Reviews

There are no reviews yet.

Be the first to review “02393 C++ Programming Exercises (Solution)”

Your email address will not be published. Required fields are marked *