C program to find amount of volume passed through a tunnel


Suppose there is a tunnel whose height is 41 and width is very large. We also have a list of boxes with length, width and height. A box can pass through the tunnel if its height is exactly less than tunnel height. We shall have to find amount of volume that are passed through the tunnel. The volume is length * width * height. So we have a number N, an 2D array with N rows and three columns.

So, if the input is like N = 4 boxes = [[9,5,20],[3,7,15],[8,15,41],[6,3,42]], then the output will be 900 and 315 we can pass first two boxes, the volumes are 9 * 5 * 20 = 900 and 3 * 7 * 15 = 315. The height of other boxes are not supported.

To solve this, we will follow these steps −

  • Define Box data type with length, width and height

  • Define a function volume(), this will take box,

  • return box.length * box.width * box.height

  • Define a function lower(), this will take box,

  • return true if box.height < 41 otherwise false

  • From the main method, do the following:,

  • for initialize i := 0, when i < N, update (increase i by 1), do:

    • if lower(boxes[i]) is true, then:

      • display volume(boxes[i])

Example

Let us see the following implementation to get better understanding −

#include <stdio.h>
#define N 4
struct Box{
    int length, width, height;
};
int volume(struct Box box){
    return box.length*box.width*box.height;
}
int lower(struct Box box){
    return box.height < 41;
}
int solve(struct Box boxes[]){
    for (int i = 0; i < N; i++)
        if (lower(boxes[i]))
            printf("%d
", volume(boxes[i])); } int main(){     struct Box boxes[N] = {{9,5,20},{3,7,15},{8,15,41},{6,3,42}};         solve(boxes); }

Input

4, {{9,5,20},{3,7,15},{8,15,41},{6,3,42}}

Output

900
315

Updated on: 08-Oct-2021

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements