Home

Boxes through a Tunnel

You are transporting some boxes through a tunnel, where each box is a parallelopiped and is characterized by its length, width, and height.
The height of the tunnel 41 feet and the width can be assumed to be infinite. A box can be carried through the tunnel only if its height is strictly less than the tunnel's height. Find the volume of each box that can be successfully transported to the other end of the tunnel. 
Note: Boxes cannot be rotated.
Write a program to find the volume.
C Program
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
    int length;
    int width;
    int height;
};

typedef struct box box;

int get_volume(box b)
{

    if (b.height < 41)
        return b.height * b.length * b.width;
    else
        return 0;
}

int is_lower_than_max_height(box b)
{

    if (b.height < 41)
        return 1;
    return 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    box *boxes = malloc(n * sizeof(box));
    for (int i = 0; i < n; i++)
    {
        scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
    }
    for (int i = 0; i < n; i++)
    {
        if (is_lower_than_max_height(boxes[i]))
        {
            printf("%d\n"get_volume(boxes[i]));
        }
    }
    return 0;
}
Input:

4 5 5 5 1 2 40 10 5 41 7 2 42
Output:

125 80


No comments:

Post a Comment