50007. Yard, Foot, and Inch

I'm a slow walker, but I never walk backwards.

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.

Subtasks

  • 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

1
2
3
4
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]);

length.c

1
2
3
4
5
6
7
8
9
10
11
12
void init(int length[3]) {
    /* add your code */
}
void add(int length[3], int i) {
    /* add your code */
}
void sum(int lengtha[3], int lengthb[3], int lengthc[3]) {
    /* add your code */
}
void diff(int lengtha[3], int lengthb[3], int lengthc[3]) {
    /* add your code */
}

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <assert.h>
#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;
}

Sample Input 1

1

Sample Output 1

0 0 0

Sample Input 2

2
12

Sample Output 2

0 1 0

Discussion