---
title: "Height difference using nested structure"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:struct-height-feet-inches"
source: https://www.bctengineer.com/q/semester_1/computer_programming/struct-height-feet-inches
site: BCT Engineering
---

# Height difference using nested structure

Computer Programming · Chapter 8: Structures

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/struct-height-feet-inches
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Write a program to read the heights of two students and display the difference between their heights. Use feet and inches as members of a structure to define height.

## Short answer

Convert each height to total inches (feet*12 + inches), calculate the absolute difference, then convert back: feet = diff/12, inches = diff%12. For input 5 9 and 5 4, the difference is 0 feet 5 inches.

## Answer

**Method.** The structure `height` has the members `feet` and `inches`. Each height is converted to total inches (feet × 12 + inches), the smaller is subtracted from the larger, and the result is converted back with `/ 12` (feet) and `% 12` (inches). This avoids a separate borrow step (1 foot = 12 inches).

```c
#include <stdio.h>

struct height {
    int feet;
    int inches;
};

int main(void)
{
    struct height h1, h2, d;
    int t1, t2, diff;

    printf("Height of first student (feet inches): ");
    scanf("%d %d", &h1.feet, &h1.inches);
    printf("Height of second student (feet inches): ");
    scanf("%d %d", &h2.feet, &h2.inches);

    t1 = h1.feet * 12 + h1.inches;      /* convert to inches */
    t2 = h2.feet * 12 + h2.inches;
    diff = t1 > t2 ? t1 - t2 : t2 - t1;

    d.feet = diff / 12;
    d.inches = diff % 12;
    printf("Difference = %d feet %d inches\n", d.feet, d.inches);
    return 0;
}
```

**Sample run** (5 ft 9 in and 5 ft 4 in)

Input: `5 9 5 4`

```text
Height of first student (feet inches): Height of second student (feet inches): Difference = 0 feet 5 inches
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the program find the difference of the heights?
   A. Subtract feet and inches separately without borrow
   B. Add the heights
   C. Convert each to total inches, subtract the smaller from the larger, convert back
   D. Multiply them
   Answer: C. Total inches avoids a separate borrow step. (Key point: Method)

2. How is height in inches computed?
   A. feet + inches * 12
   B. feet * 10 + inches
   C. feet / 12 + inches
   D. feet * 12 + inches
   Answer: D. 1 foot = 12 inches. (Key point: Conversion)

3. How are feet and inches obtained from diff?
   A. feet = diff / 12 and inches = diff % 12
   B. feet = diff % 12 and inches = diff / 12
   C. both = diff / 12
   D. feet = diff * 12
   Answer: A. Integer division gives feet and remainder gives inches. (Key point: Back conversion)

4. Why is the conditional t1 > t2 ? t1 - t2 : t2 - t1 used?
   A. To swap the structures
   B. So the difference is never negative
   C. To sort the heights
   D. To round the answer
   Answer: B. The smaller is always subtracted from the larger. (Key point: Absolute difference)

5. For 5 ft 9 in and 5 ft 4 in, the difference is?
   A. 1 foot 5 inches
   B. 0 feet 13 inches
   C. 0 feet 5 inches
   D. 5 feet 5 inches
   Answer: C. 69 - 64 = 5 inches = 0 ft 5 in. (Key point: Sample)

## Related in Computer Programming

- [College records with nested date structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-college-estdate) — Computer Programming, chapter 8
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Sending structure members to a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astructure-members-to-function) — Computer Programming, chapter 8
- [Structure of a FORTRAN program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-structure) — Computer Programming, chapter 11, asked 5×
- [Declaration vs definition; C program structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adeclaration-definition-structure-of-c) — Computer Programming, chapter 3
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×

## Related in other subjects

- [Program using static members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-program) — Object Oriented Programming, chapter 4
- [Two groups: best, consistent, SE of difference](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Atwo-groups-best-consistent-se) — Probability and Statistics, chapter 1
- [T/F: sag absent with supports at different heights](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Atf-sag-absent-different-heights) — Transmission and Distribution System Design, chapter 4
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6
