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) {
// Get dimensions of the box
cout << "Enter the dimensions of the box:\n";
cout << "Length: ";
cin >> length;
cout << "Breadth: ";
cin >> breadth;
cout << "Height: ";
cin >> height;
if (length <= 0 || breadth <= 0 || height <= 0)
break;
sphereSize = getSphereSize(length, breadth, height);
if (sphereSize == 0)
cout << "The box cannot fit in any of the spheres";
else
cout << "The box can fit in the " << sphereSize << "-inch sphere";
// Increment the counter
if (sphereSize == 4)
sphereCounts[0]++;
else if (sphereSize == 6)
sphereCounts[1]++;
else if (sphereSize == 8)
sphereCounts[2]++;
else if (sphereSize == 10)
sphereCounts[3]++;
else if (sphereSize == 12)
sphereCounts[4]++;
cout << "\n\n";
}
cout << "\nNumber of 4-inch spheres: " << sphereCounts[0];
cout << "\nNumber of 6-inch spheres: " << sphereCounts[1];
cout << "\nNumber of 8-inch spheres: " << sphereCounts[2];
cout << "\nNumber of 10-inch spheres: " << sphereCounts[3];
cout << "\nNumber of 12-inch spheres: " << sphereCounts[4];
cout << endl;
return 0;
}
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.