Chapter Overview

Learn about the mixed fraction case study, which we will solve using different approaches.

Imperative programming forms the foundation of various programming paradigms, emphasizing a step-by-step sequence of operations for the computer to execute a specific task. In this paradigm, programmers provide a series of statements that outline the program’s execution flow, guiding the computer through the instructions. As one of the most widely used and fundamental programming approaches, imperative programming remains at the core of many programming languages.

Within the domain of imperative programming, there are several subparadigms that have evolved over time. Three of the most prominent subparadigms are procedural programming, structured programming, and object-oriented programming (OOP). Each of these subparadigms has its own unique characteristics and ways of organizing code, but they are all built upon the foundation of imperative programming.

Throughout this chapter, we will delve into the intricacies of procedural programming (PP), structured programming, and object-oriented programming (OOP). We will conduct a thorough examination of these approaches, grasping their appropriate applications and considering their respective advantages and disadvantages. Additionally, we will explore three primary data handling approaches, starting with primitive variables and advancing to the benefits of using structs and classes.

To facilitate our study, we will utilize a captivating case study titled “Mixed Fraction Calculator.” Brace yourself for an engaging exploration of programming paradigms and their nuances.

Before we dive in, let’s quickly review some mathematical concepts relevant to our case study. We will be translating these concepts into both procedural (using primitive variables and structs) and object-oriented paradigms (using classes).

Mixed Fraction: A case study

Before we start writing code for our case study, we need to understand the problem statement to develop a clear thought process. Our task is to create a mixed fraction arithmetic calculator that performs the following operations:

  1. Addition

  2. Subtraction

  3. Multiplication

  4. Division

What is a mixed fraction?

A mixed fraction has three components: whole number, numerator, and denominator, as shown in the diagram below:

Press + to interact
Mixed fraction
Mixed fraction

Generally, to perform any arithmetic operation on mixed fractions, they must first be converted into improper fractions. In an improper fraction, we only have numerators and denominators. Here are the required mathematical steps for its conversion:

  1. Multiply the denominator of the mixed fraction with its whole number.

  2. Add the resultant of the product obtained in Step 1 to the numerator value of the mixed fraction to obtain the new numerator for the improper fraction.

  3. The denominator for both fractions will remain the same.

  4. Reduce the obtained fraction (convert to simplest form) by dividing the improper fraction’s numerator and denominator by their greatest common divisor (GCD).

Note: If the mixed fraction is a negative value, we take absolute values for each component while computing the numerator of the improper fraction. Sign is associated with the whole fraction and not on individual components.

Press + to interact
Mixed fraction to Improper fraction
Mixed fraction to Improper fraction
1 of 13

Now that we know how to convert our term into an improper fraction, the next step is to learn the logic behind each arithmetic operation.

Addition and subtraction

Addition and subtraction are the two faces of the same coin. They follow the same mathematical sequence except at the last step, where we add or subtract the two values. To perform these operations, we need to have the same denominator for each fraction. To understand the method, let’s calculate the sum (addition) of two mixed fractions: 1121\frac{1}{2} and 3143\frac{1}{4}.

The first step, as you learned earlier, is to convert these mixed fractions to improper fractions:

A:11232A:1 \frac{1}{2} \rightarrow \frac{3}{2}

B:314134B:3\frac{1}{4} \rightarrow \frac{13}{4} .

Now, to add them, we can follow the following approach:

  1. Making the same denominators for both fractions.

    1. Multiply the fractionAA's numerator and denominator with the denominator of the fraction BB—this will be our new fraction AA.

    2. Multiply the fractionBB's numerator and denominator with the denominator of the fraction AA —this will be our new fraction BB.

Note: We can simply compute the resulting fraction’s denominator by multiplying the denominators of AA and BB.

  1. Add the numerator of both fractions—this will be the resultant numerator, and the denominator will be the same as calculated in Step 2 (we can pick anyone’s fraction denominator).

  2. Reduce the fraction by dividing the final numerator and denominator with their GCD.

  3. Convert it back to a mixed fraction.

Notes: Steps 3 and 4 can be performed in either order.

These steps are demonstrated below:

For subtraction, in Step 2, we will subtract the numerator values for both fractions to get the resultant numerator. The rest of the method remains the same.

Multiplication and division

Multiplication is very straightforward for fractions. Let’s talk about our previous example of fractions AA and BB. To multiply both fractions, we will multiply the numerator of AA with a numerator of BB and the denominator of AA with the denominator of BB, followed by reducing the fraction by dividing the final numerator and denominator with their GCD.

In division, we take the reciprocal of the second fraction and then perform the multiplication operation.

Note: Here, we get a proper fraction when division is performed. Therefore, we don’t need to convert it to mixed fraction form.

Now, we know the process to perform each of these arithmetic operation on fractions. This will help us understand and formulate the coding logic for our case study.

In upcoming lessons, we will discuss different programming approaches and how to integrate their style into our mixed calculator problem.