Rationalise (Canonise) the Fractions before Addition
Normally before fractions are added, they are rationalised;
i.e., they are put in a form where their denominators become
the same. This same denominator is the LCM of the
denominators of all the separate fractions.
After this is done, the new numerators can then be added together.
Steps for Addition of Fractions C++ code
The following steps will guide us in writing our code.
Let's illustrate with
2/5 + 7/4
Step 1:
Find the LCM of the denominators.
⇒ LCM of 5 & 4 = 20
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((2 x 4) + (7 x 5))/20
= (8 + 35)/20
Step 3:
Go ahead and add the numerators.
⇒
43/20
Create a new class file;
Call them AddFraction.;
Type out the adjoining C++ code for adding fractions.
Note: The code module for
finding LCM
is from the Primary Category.
Create a new Class File called LCM in your current
project and copy the L.C.M. code into it.
You can comment out the DivideFraction C++ object
code in the main class from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of numerators and denominators for fractional addition.
C++ code for AddFraction Header File
#pragma once
#include "LCM.h"
#include <vector>
using namespace std;
class AddFraction {
public:
AddFraction(vector<unsigned>, vector<unsigned>);
virtual ~AddFraction();
void doAdd();
unsigned int answer;
unsigned int lcm;
protected:
vector<unsigned> numerators;
vector<unsigned> denominators;
vector<unsigned> new_numerators;
void canonizeFraction();
};
AddFraction Class File
#include "stdafx.h"
#include "AddFraction.h"
AddFraction::AddFraction(vector<unsigned> num, vector<unsigned> denom) {
numerators = num;
denominators = denom;
answer = 0;
}
void AddFraction::canonizeFraction() {
LCM l_c_m(denominators);
lcm = l_c_m.getLCM();
for (unsigned i = 0; i < denominators.size(); i++) {
new_numerators.push_back(lcm / denominators[i] * numerators[i]);
}
new_numerators.shrink_to_fit();
}
void AddFraction::doAdd() {
canonizeFraction();
for (unsigned i = 0; i < new_numerators.size(); i++) {
answer += new_numerators[i];
}
}
AddFraction::~AddFraction() {
}
Main class
#include "stdafx.h"
#include "AddFraction.h"
#include <iostream>
#include <vector>
#include <exception>
using namespace std;
int main() {
cout << "\n Welcome to our demonstration sequels";
cout << "\n Hope you enjoy (and follow) the lessons.\n\n";
vector<unsigned> numerators;
vector<unsigned> denominators;
try {
numerators = { 1, 1, 1, 1 };
denominators = { 4, 4, 4, 4 };
cout << "\n Solving:\n";
for (unsigned n : numerators) {
printf("%13u", n);
}
printf("\n%12s", " ");
for (unsigned i = 0; i < numerators.size() - 1; i++) {
cout << "- + ";
}
printf("%2s\n", "-");
for (unsigned d : denominators) {
printf("%13u", d);
}
cout << "\n";
AddFraction add_fract(numerators, denominators);
add_fract.doAdd();
printf("\n%26u\n", add_fract.answer);
printf("%26s\n", "Answer = -");
printf("%26u\n", add_fract.LCM);
cout << "\n\n";
} catch (exception& e) {
cout << "\n" << e.what() << "\n";
}
return 0;
}