Home C++ Tutorial C++ Program: Adding Two Distances (in inch-feet) using Structures

C++ Program: Adding Two Distances (in inch-feet) using Structures

by Anup Maurya
29 minutes read

In this article, we will discuss a C++ program that adds two distances measured in the inch-feet system. We will use structures to represent the distances and perform the addition operation. The program will take input from the user and display the output.

Problem Statement

We are given two distances in the inch-feet system, and we need to add them together.

Program Solution

To solve this problem, we will define a structure called ‘Distance’ with two members: ‘feet’ to store the number of feet and ‘inch’ to store the number of inches. Then, we will define a function called ‘addDistances’ that takes two Distance objects as parameters and returns the sum of the distances.

#include <iostream>
using namespace std;

// Structure to represent a distance in inch-feet system
struct Distance {
    int feet;
    int inch;
};

// Function to add two distances
Distance addDistances(Distance d1, Distance d2) {
    Distance sum;
    sum.feet = d1.feet + d2.feet;
    sum.inch = d1.inch + d2.inch;

    // Normalize the sum if inches exceed 12
    if (sum.inch >= 12) {
        sum.feet += sum.inch / 12;
        sum.inch = sum.inch % 12;
    }

    return sum;
}

int main() {
    Distance d1, d2, sum;

    // Input the first distance
    cout << "Enter the first distance:" << endl;
    cout << "Feet: ";
    cin >> d1.feet;
    cout << "Inch: ";
    cin >> d1.inch;

    // Input the second distance
    cout << "Enter the second distance:" << endl;
    cout << "Feet: ";
    cin >> d2.feet;
    cout << "Inch: ";
    cin >> d2.inch;

    // Add the distances
    sum = addDistances(d1, d2);

    // Display the sum
    cout << "Sum of distances: " << sum.feet << " feet " << sum.inch << " inches" << endl;

    return 0;
}

Explanation

  1. We start by including the necessary header files and specifying the ‘std’ namespace for simplicity.
  2. We define the structure ‘Distance’ with two members: ‘feet’ and ‘inch’.
  3. The function ‘addDistances’ takes two Distance objects as parameters and returns a Distance object as the sum.
  4. Inside the ‘addDistances’ function, we add the corresponding feet and inches of the input distances and store the result in the ‘sum’ Distance object.
  5. If the sum of inches is greater than or equal to 12, we normalize it by incrementing the ‘feet’ member and setting the ‘inch’ member to the remainder.
  6. In the ‘main’ function, we declare three Distance objects: ‘d1’, ‘d2’, and ‘sum’.
  7. We prompt the user to input the values of ‘feet’ and ‘inch’ for ‘d1’.
  8. Similarly, we prompt the user to input the values of ‘feet’ and ‘inch’ for ‘d2’.
  9. We call the ‘addDistances’ function with ‘d1’ and ‘d2’ as arguments and assign the returned Distance object to ‘sum’.
  10. Finally, we display the resulting sum of distances in the inch-feet system.

Output

Let’s run the program and see some sample output:

Enter the first distance:
Feet: 5
Inch: 11
Enter the second distance:
Feet: 3
Inch: 9
Sum of distances: 9 feet 8 inches

Conclusion

In this article, we discussed a C++ program that adds two distances in the inch-feet system. We used structures to represent the distances and performed the addition operation. The program took input from the user and displayed the sum of the distances. This program can be easily modified to handle more complex distance calculations or to work with different measurement systems.

related posts

Leave a Comment

Enable Notifications OK No thanks