189k views
0 votes
Write a C console application that will be used to determine if rectangular packages can fit inside one of a set of spheres. Your program will prompt the user for the three dimensions that define a rectangular box; the length, the width, and the height. The interior diameter of a sphere is used to identify its size. Spheres are available in the following five sizes: 4- inch, 6-inch, 8-inch, 10-inch, and 12-inch. Your program will execute repeatedly until the user enters a value of zero for one or more of the rectangular box dimensions. After obtaining the dimensions of the rectangular box, your program will call a function named getSphereSize that determines if the box will fit inside one of the five spheres. The formula for calculating the diagonal of a rectangular box is:

1 Answer

13 votes

Answer:

#include <cmath>

#include <iostream>

using namespace std;

int getSphereSize(double length, double breadth, double height) {

double diagonal = sqrt(length * length + breadth * breadth + height * height);

if (diagonal <= 4)

return 4;

if (diagonal <= 6)

return 6;

if (diagonal <= 8)

return 8;

if (diagonal <= 10)

return 10;

if (diagonal <= 12)

return 12;

return 0;

}

int main() {

double length, breadth, height;

int sphereCounts[5] = {0};

int sphereSize;

while (true)

cout << "\\Number of 4-inch spheres: " << sphereCounts[0];

cout << "\\Number of 6-inch spheres: " << sphereCounts[1];

cout << "\\Number of 8-inch spheres: " << sphereCounts[2];

cout << "\\Number of 10-inch spheres: " << sphereCounts[3];

cout << "\\Number of 12-inch spheres: " << sphereCounts[4];

cout << endl;

return 0;

}

Step-by-step explanation:

The "cmath" library is included in the c++ program. The getSphereSize function is used to return the sphere size the rectangle dimension can fit into. It program continuously prompts the user for the length, breadth, and height of the rectangle and passes the values to the getSphereSize function in the while but breaks if any or all of the variable value is zero.

The sizes of the sphere objects in inches are collected in an array of five integer values of zeros and are incremented by one for every match with a rectangle.

User Chris Kent
by
5.1k points