usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

How to Divide Fractions in C++ | Step-by-Step Tutorial with Code Example



Understanding the Math Behind Fraction Division | Maths Explanation for C++ Kids

C++ makes it easy to perform arithmetic operations like dividing fractions. Whether you're simplifying fractions, multiplying them, or coding a math class project, C++ provides clear syntax and powerful tools for beginners and students alike.

Dividing fractions in C++ is a great way to combine coding with math skills. In this tutorial, junior secondary students will learn how to use C++ to divide fractions step-by-step. We'll explore how to invert and multiply fractions, write a C++ class for fraction operations, and understand the logic behind the algorithm.

Division is the inverse operation of multiplication; That is exactly what we'll do with for fractions.
Invert the fractions that come after a division sign, as well as change the division sign to multiplication, and then follow through with multiplication, as already explained in the Multiplying Fractions with C++ tutorial.


Algorithm Steps to Divide Fractions in C++

Say we are to implement a C++ algorithm to divide the given fractional expression
                  21/8 ÷ 7/2;

Inverting this will yield
                  21/8 ÷ 7/2;

Then we can go ahead and multiply.


Create a new C++ class file; Call it DivideFraction.
Type out the adjoining C++ code for dividing Fractions.



Note: You can comment out the MultiplyFraction C++ object code in the main class from the previous lesson or simply continue from where it stopped.


So! C++ Fun Practice Exercise - Divide 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 divides those fractions.







C++ Code for Dividing Fractions - Header File

#pragma once

#include "MultiplyFraction.h"

class DivideFraction : public MultiplyFraction {
public:
    DivideFraction(vector<unsigned>, vector<unsigned>);
    virtual ~DivideFraction();
    void doDivide();
};


C++ Code for Dividing Fractions - Class File

#include "stdafx.h"
#include "DivideFraction.h"


DivideFraction::DivideFraction(vector<unsignednumvector<unsigneddenom) : MultiplyFraction(numdenom) {
}

void DivideFraction::doDivide() {
    unsigned temp;
    // Invert every other fraction but the first
    for (unsigned i = 1; i < numerators.size(); i++) {
        temp = numerators[i];
        numerators[i] = denominators[i];
        denominators[i] = temp;
    }
    doMultiply();
}


DivideFraction::~DivideFraction() {
}

C++ Code for Dividing Fractions - Main Class

// Algebra.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "DivideFraction.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 {
        /*
        * Dividing fractions
        */

        numerators = { 16, 9, 640, 7 };
        denominators = { 9, 20, 27, 20 };

        cout << "\n    Solving:\n";
        // Print as fraction
        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";

        // use the DivideFraction class
        DivideFraction div_fract(numerators, denominators);
        div_fract.doDivide();

        printf("\n%25u\n", div_fract.answer[0]);
        printf("%25s\n""Answer =    -");
        printf("%25u\n", div_fract.answer[1]);

        cout << "\n\n";

    } catch (exception& e) {
        cout << "\n" << e.what() << "\n";
    }

    return 0;
}




<< Previous Next >>