Why Rationalise or Canonise Fractions before Addition | Maths Explanation for C++ Kids
In this C++ tutorial for junior secondary students, we explore how to add fractions. Before performing the addition,
we rationalise or canonise the fractions to ensure accuracy. This method uses Finding LCM in C++
class to align denominators, making it ideal for math programming beginners.
This C++ tutorial teaches young students how to add fractions with different denominators.
Before fractions are added, they are rationalised; i.e., they are put in a form where their denominators become
the same. This identical denominator is the LCM of the previous denominators of all the separate fractions.
After this is done, the new numerators can then be added together.
Step-by-Step Guide for Addition of Fractions - C++ Algorithm
The following steps will guide us in writing our C++ code for adding fractions.
Let's illustrate the steps to follow with the example fractional expression
2/5 + 7/4
Step 1:
Using the Find LCM in C++
class from the Primary Category, 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 C++ class file;
Call them AddFraction.;
Type out the adjoining C++ code for adding fractions.
Note: The code module for Learn how to find LCM in C++
is from the Primary Category.
Create a new C++ 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! C++ Fun Practice Exercise - Add Fractions
As a fun practice exercise, feel free to try out your own fractions with different numerators and denominators,
and see how the C++ code adds these fractions.
C++ Code for Adding Fractions - 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();
};
C++ Code for Adding Fractions - 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() {
}
C++ Code for Adding Fractions - 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;
}