# 50007. Yard, Foot, and Inch

## Problem description

Write a program to manage length. A length has three components -- yard, foot, and inch. A yard is 3 feet and a foot is 12 inches. We will use an array of length 3 to present a length. length[0] will be the yard, length[1] will be the foot, and length[2] will be the inch. All of them are non-negative integers. Now you need to implement the following three functions.

1. void init(int length[3]); This function resets the length to 0.
2. void add(int length[3], int i); This function adds i inches to length. Note that the final length should have inch between 0 and 11, and foot between 0 and 2.
3. void sum(int lengtha[3], int lengthb[3], int lengthc[3]); This function adds lengtha and lengthb and place the sum into lengthc. Note that the lengthc should have inch between 0 and 11, and foot between 0 and 2.
4. void diff(int lengtha[3], int lengthb[3], int lengthc[3]); This function subtract lengthb from lengtha and place the different into lengthc. We guarantee that lengtha is no less than lessb. Note that the lengthc should have inch between 0 and 11, and foot between 0 and 2.

• 10 pt. Implement only init.
• 30 pt. Implement only init and add.
• 30 pt. Implement the first three functions.
• 30 pt. Implement all four functions.

### length.h

 1234 void init(int length[3]);void add(int length[3], int i);void sum(int lengtha[3], int lengthb[3], int lengthc[3]);void diff(int lengtha[3], int lengthb[3], int lengthc[3]);

### main.c

 123456789101112131415161718192021222324252627282930313233343536 #include #include #include "length.h" int main() {    int a[3] = {-1, -1, -1}, b[3] = {-1, -1, -1};    int c[3] = {-1, -1, -1}, d[3] = {-1, -1, -1};    int e[3] = {-1, -1, -1}, f[3] = {-1, -1, -1};    int cmd, x, y, z;    scanf("%d", &cmd);    if (cmd == 1) {        init(a);        printf("%d %d %d\n", a[0], a[1], a[2]);    } else if (cmd == 2) {        init(a);        scanf("%d", &x);        add(a, x);        printf("%d %d %d\n", a[0], a[1], a[2]);    } else if (cmd == 3) {        init(a), init(b);        scanf("%d %d", &x, &y);        add(a, x), add(b, y);        sum(a, b, c);        printf("%d %d %d\n", c[0], c[1], c[2]);    } else if (cmd == 4) {        init(a), init(b), init(c), init(d);        scanf("%d %d %d", &x, &y, &z);        assert(x+y >= z);        add(a, x), add(b, y);        sum(a, b, c);        add(d, z);        diff(c, d, e);        printf("%d %d %d\n", e[0], e[1], e[2]);    }    return 0;}

 1

 0 0 0

 212

 0 1 0