C Program to Add Two Distances (in inch-feet system) using Structures

Adding two distances in the inch-feet system can be efficiently handled using structures in C. Structures allow us to group related variables, which makes managing complex data types easier. This article will provide three different examples to demonstrate how to add two distances using structures in C. Each example will include an explanation, the program code, and the output.

Prerequisites

Before we begin, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with structures in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Basic Addition of Two Distances

1.1 Explanation

In this approach, we define a structure to represent distances with feet and inches. We then create a function to add two distances and return the result.

1.2 Program

C
#include <stdio.h>

struct Distance {
    int feet;
    float inches;
};

// Function to add two distances
struct Distance addDistances(struct Distance d1, struct Distance d2) {
    struct Distance result;
    result.feet = d1.feet + d2.feet;
    result.inches = d1.inches + d2.inches;

    // Convert inches to feet if inches >= 12
    if (result.inches >= 12) {
        result.feet += (int)(result.inches / 12);
        result.inches = (int)result.inches % 12 + (result.inches - (int)result.inches);
    }

    return result;
}

int main() {
    struct Distance d1, d2, result;

    // Input first distance
    printf("Enter feet and inches for the first distance: ");
    scanf("%d %f", &d1.feet, &d1.inches);

    // Input second distance
    printf("Enter feet and inches for the second distance: ");
    scanf("%d %f", &d2.feet, &d2.inches);

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

    // Display the result
    printf("Sum of distances: %d feet %.1f inches\n", result.feet, result.inches);

    return 0;
}

1.3 Output

C
Enter feet and inches for the first distance: 5 9.5
Enter feet and inches for the second distance: 3 4.5
Sum of distances: 9 feet 2.0 inches

Solution 2: Using Pointers to Add Distances

2.1 Explanation

This approach uses pointers to pass structures to the addition function. This method is useful for modifying the original data or when dealing with larger structures.

2.2 Program

C
#include <stdio.h>

struct Distance {
    int feet;
    float inches;
};

// Function to add two distances using pointers
void addDistances(struct Distance *d1, struct Distance *d2, struct Distance *result) {
    result->feet = d1->feet + d2->feet;
    result->inches = d1->inches + d2->inches;

    // Convert inches to feet if inches >= 12
    if (result->inches >= 12) {
        result->feet += (int)(result->inches / 12);
        result->inches = (int)result->inches % 12 + (result->inches - (int)result->inches);
    }
}

int main() {
    struct Distance d1, d2, result;

    // Input first distance
    printf("Enter feet and inches for the first distance: ");
    scanf("%d %f", &d1.feet, &d1.inches);

    // Input second distance
    printf("Enter feet and inches for the second distance: ");
    scanf("%d %f", &d2.feet, &d2.inches);

    // Add the distances using pointers
    addDistances(&d1, &d2, &result);

    // Display the result
    printf("Sum of distances: %d feet %.1f inches\n", result.feet, result.inches);

    return 0;
}

Output

C
Enter feet and inches for the first distance: 6 8.2
Enter feet and inches for the second distance: 2 5.8
Sum of distances: 9 feet 2.0 inches

Solution 3: Using Arrays of Structures

3.1 Explanation

In this example, we demonstrate how to handle multiple pairs of distances using an array of structures. We will loop through the array to input, add, and display the distances.

3.2 Program

C
#include <stdio.h>

struct Distance {
    int feet;
    float inches;
};

// Function to add two distances
struct Distance addDistances(struct Distance d1, struct Distance d2) {
    struct Distance result;
    result.feet = d1.feet + d2.feet;
    result.inches = d1.inches + d2.inches;

    // Convert inches to feet if inches >= 12
    if (result.inches >= 12) {
        result.feet += (int)(result.inches / 12);
        result.inches = (int)result.inches % 12 + (result.inches - (int)result.inches);
    }

    return result;
}

int main() {
    struct Distance distances[3][2], results[3];

    // Input distances for each pair
    for (int i = 0; i < 3; i++) {
        printf("Enter feet and inches for the first distance of pair %d: ", i+1);
        scanf("%d %f", &distances[i][0].feet, &distances[i][0].inches);

        printf("Enter feet and inches for the second distance of pair %d: ", i+1);
        scanf("%d %f", &distances[i][1].feet, &distances[i][1].inches);

        // Add the distances
        results[i] = addDistances(distances[i][0], distances[i][1]);
    }

    // Display the results
    for (int i = 0; i < 3; i++) {
        printf("Sum of distances for pair %d: %d feet %.1f inches\n", i+1, results[i].feet, results[i].inches);
    }

    return 0;
}

Output

C
Enter feet and inches for the first distance of pair 1: 3 7.5
Enter feet and inches for the second distance of pair 1: 4 9.3
Enter feet and inches for the first distance of pair 2: 1 11.9
Enter feet and inches for the second distance of pair 2: 2 1.2
Enter feet and inches for the first distance of pair 3: 0 10.0
Enter feet and inches for the second distance of pair 3: 0 2.5

Sum of distances for pair 1: 8 feet 4.8 inches
Sum of distances for pair 2: 4 feet 1.1 inches
Sum of distances for pair 3: 1 feet 0.3 inches

Conclusion

Using structures in C to manage and manipulate complex data types such as distances in the inch-feet system can significantly enhance code readability and maintainability. This article presented three different methods to add two distances using structures: a basic approach, using pointers, and handling multiple pairs of distances with arrays. Each method serves different needs and can be chosen based on the specific requirements of your application. By understanding these techniques, you can effectively manage structured data in your C programs.