Procedural/Structured Programming: Fraction Case Study
Apply procedural/structured programming principles to implement our fraction case study in C++.
We'll cover the following
- Case study: Mixed fraction arithmetic calculator
- Finding out the memory requirement
- Implementing the add() function
- Flow of the add() function
- Step 1: Convert the given mixed fractions into improper fractions
- Step 2: Making the denominators the same
- Step 3: Computing the resulting fraction
- Step 4: Simplifying the resulting fraction (converting to reduced form)
- Step 5: Convert to a mixed fraction
- Printing the resulting mixed fraction
- Complete code
Let’s develop a solid foundation in procedural/structured programming by solving a mixed fraction arithmetic calculator case study.
Case study: Mixed fraction arithmetic calculator
We’ll tackle our case study by breaking it down into manageable tasks, called functions, and then use them to write the complete flow (the sequence) of the program. We’ll discuss each step of the coding process and learn how to derive other arithmetic operations from the structure of our addition function. Let’s solve this case study!
We already know how to compute arithmetic operations on mixed fractions mathematically (as we did in the “Chapter Overview” lesson). How can we implement it in code?
Finding out the memory requirement
First, we need something that can store our mixed fraction and let us access its individual components (whole number, numerator, and denominator) to perform our arithmetic operations.
For that, we’ll use variables as shown below:
Get hands-on with 1200+ tech skills courses.