HackerRank C- Boxes through a Tunnel




You are transporting some boxes through a tunnel, where each box is a parallelepiped, 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.



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_HEIGHT 41
  4. struct box
  5. {
  6. int length,width, height;
  7. /**
  8. * Define three fields of type int: length, width and height
  9. */
  10. };
  11. typedef struct box box;
  12. int get_volume(box b) {
  13. return b.length*b.width*b.height;
  14. /**
  15. * Return the volume of the box
  16. */
  17. }
  18. int is_lower_than_max_height(box b) {
  19. /**
  20. * Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise
  21. */
  22. if(b.height<MAX_HEIGHT){
  23. return 1;
  24. }
  25. else{
  26. return 0;
  27. }
  28. }
  29. int main()
  30. {
  31. int n;
  32. scanf("%d", &n);
  33. box *boxes = malloc(n * sizeof(box));
  34. for (int i = 0; i < n; i++) {
  35. scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
  36. }
  37. for (int i = 0; i < n; i++) {
  38. if (is_lower_than_max_height(boxes[i])) {
  39. printf("%d\n", get_volume(boxes[i]));
  40. }
  41. }
  42. return 0;
  43. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube