Understanding the Math Behind Fraction Multiplication | Maths Explanation for C++ Kids
Learning how to multiply fractions in C++ is a great way to combine math skills with coding.
This tutorial is designed for junior secondary students who want to understand fraction multiplication
using simple C++ classes and constructors.
Multiplying fractions is pretty straightforward:
Cancel out all common factors between numerators and denominators,
then multiply whatever is left numerator to numerator and
denominator to denominator.
In this lesson, we'll walk through the step-by-step method of multiplying fractions using C++.
You'll learn how to define a class, use constructors, and apply logic to find mutual factors and simplify results.
Step-by-Step Explanation of Algorithm to Multiply Fractions in C++
This C++ algorithm for fractions shows how to multiply two fractions and reduce them to their lowest terms.
It's a great math coding project for beginners.
Understanding how to multiply multiple fractions in C++ helps students build both computational thinking and math fluency.
It's a foundational skill for more advanced topics like algebra and data science.
If we have
4/9 x 21/8;
Step 1:
Find any common factor between any numerator and any denominator.
Step 2:
Cancel out any such common factor.
Step 3:
Repeat Steps 1 & 2 recursively until there are no
more common factors.
Create a new C++ class file;
call it MultiplyFraction.
Type out the adjoining C++ code for multiplying fractions.
Note: You can comment out the LowestTerm C++ object
code in the main class from the previous lesson or simply continue from where it stopped.
So! C++ Fun Practice Exercise - Multiply 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 multiplies those fractions.
C++ Code for Multiplying Fractions - Header File
#pragma once
#include <vector>
using namespace std;
class MultiplyFraction {
public:
MultiplyFraction(vector<unsigned>, vector<unsigned>);
virtual ~MultiplyFraction();
void doMultiply();
unsigned int answer[2];
protected:
vector<unsigned> numerators;
vector<unsigned> denominators;
unsigned int n_index, d_index;
unsigned int trial_factor;
bool mutual_factor;
};
C++ Code for Multiplying Fractions - Class File
#include "stdafx.h"
#include "MultiplyFraction.h"
MultiplyFraction::MultiplyFraction(vector<unsigned> num, vector<unsigned> denom) {
numerators = num;
denominators = denom;
trial_factor = 0;
n_index = 0;
d_index = 0;
answer[0] = 1;
answer[1] = 1;
for (unsigned n : num) {
if (n > trial_factor) {
trial_factor = n;
}
}
for (unsigned d : denom) {
if (d > trial_factor) {
trial_factor = d;
}
}
}
void MultiplyFraction::doMultiply() {
while (trial_factor > 1) {
while (n_index < numerators.size()) {
mutual_factor = false;
if ((numerators[n_index] % trial_factor) == 0) {
while (d_index < denominators.size()) {
if ((denominators[d_index] % trial_factor) == 0) {
mutual_factor = true;
break;
}
d_index++;
}
break;
}
n_index++;
}
if (mutual_factor) {
numerators[n_index] /= trial_factor;
denominators[d_index] /= trial_factor;
continue;
}
n_index = 0;
d_index = 0;
trial_factor--;
}
for (int i = 0; i < numerators.size(); i++) {
answer[0] *= numerators[i];
answer[1] *= denominators[i];
}
}
MultiplyFraction::~MultiplyFraction() {
}
C++ Code for Multiplying Fractions - Main Class
#include "stdafx.h"
#include "MultiplyFraction.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 = { 16, 20, 27, 20 };
denominators = { 9, 9, 640, 7 };
cout << "\n Solving:\n";
for (unsigned n : numerators) {
printf("%13u", n);
}
printf("\n%12s", " ");
for (unsigned i = 0; i < numerators.size() - 1; i++) {
cout << "- X ";
}
printf("%1s", "-");
cout << "\n";
for (unsigned d : denominators) {
printf("%13u", d);
}
cout << "\n";
MultiplyFraction mul_fract(numerators, denominators);
mul_fract.doMultiply();
printf("\n%26u\n", mul_fract.answer[0]);
printf("%26s\n", "Answer = -");
printf("%26u\n", mul_fract.answer[1]);
cout << "\n\n";
} catch (exception& e) {
cout << "\n" << e.what() << "\n";
}
return 0;
}