Boxes Task : Evaluate whether you can load various sizes of jars into boxes. For
ID: 3872796 • Letter: B
Question
Boxes
Task: Evaluate whether you can load various sizes of jars into boxes. For each test, you will input the length, width, and height of a box as well as the diameter and height of a cylindrical jar. The algorithm will determine whether each jar will fit in its corresponding box. The jar can sit upright in the box, or it can lie on its side with its top parallel to one of the sides of the box. Assume that a clearance allowance of .25 inch is needed in all three directions for the jar to fit in the box.
C++ program
Input: Use the following data, which can be found in the P4Boxes.txt file. Each row of data includes the 3 inner dimensions of a box (length, width, height) and the two outer dimensions of a jar (diameter, height). The dimensions are all in inches.
Box: Jar:
L W H D H
6.0 6.0 10.3 5.0 10.0
6.0 5.0 10.3 5.0 10.0
12.0 3.3 4.0 3.0 11.0
12.0 3.2 4.0 3.0 11.0
9.5 6.5 7.5 6.0 9.5
9.5 6.5 7.5 6.0 9.0
4.5 8.0 4.5 4.0 7.5
4.0 8.0 4.5 4.0 7.5
7.3 7.3 17.0 7.0 16.0
6.8 7.3 17.0 7.0 16.0
7.3 7.3 16.2 7.0 16.0
7.2 7.3 16.3 7.0 16.0
Output: Send the output to an external file. Line up decimal points in the output columns. The output file should include column labels, an appropriate header in addition to the column labels, the given points, and a “yes” or “no” verdict as to whether each jar will fit in its box.
Sample output (you may change the layout):
Box: Jar: Jar Fits?
L W H D H
6.0 6.0 10.3 5.0 10.0 yes
6.0 5.0 10.3 5.0 10.0 no
12.0 3.3 4.0 3.0 11.0 yes
Programming: Include at least one function in your program.
Explanation / Answer
1: Here is An algorithm using pseudocode:
Algorithm for the Boxes Problem.
Input: Read L, W, H_Box, D, H_Jar data from the Data.tx file
Output: Output file with all the Data after Calculation.
1- open Data.txt file
2- Read data from the Data.txt file.
3- if (Length of Box+0.25 > Diameter of Jar and Width of Box +0.25 > Diameter and Hight of Box+0.25 > Hight of Jar)
4- then
5- print yes;
6- else
7- print no;
8- print the output on the screen.
9- print result into output file result.txt;
10-end
-----------------------------------------------------------------------------------------------------------------------------------
2: Program: Here is a C++ program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<math.h>
#include<fstream>
//Namespace declaration
using namespace std;
//Function Prototype
void print_layout();
//Starting of main function
int main()
{
//Variable data tye declaration
double L, W, H_Box, D, H_Jar;
string out;
//ifstream, ofstream file declaration
ifstream infile;
ofstream outfile;
infile.open("Data.txt");
if(!infile)
{
cout <<"file does note exist"<<endl;
return 1;
}
outfile.open("result.txt");
if(!outfile)
{
cout <<"file does note exist"<<endl;
return 1;
}
//print_layout function calling
print_layout();
outfile<<" "<<endl;
outfile<<" Box: Jar: Jarfit? "<<endl;
outfile<<setw(10)<<left<<"L"<<setw(10)<<left<<"W"<<setw(10)<<left<<"H_Box"<<setw(10)<<left<<"D"<<setw(10)<<left<<"H_Jar"<<endl;
//reading data from the file
while(!infile.eof())
{
infile>>L>>W>>H_Box>>D>>H_Jar;
if(L+0.25 > D && W+0.25 > D && H_Box+0.25 > H_Jar)
{
out = "yes";
}
else
{
out = "No";
}
//Display output on the screen as well as in the output file
cout<<" ";
outfile<<" ";
cout<<setw(10)<<left<<L<<setw(10)<<left<<W<<setw(10)<<left<<H_Box<<setw(10)<<left<<D<<setw(10)<<left<<H_Jar<<setw(10)<<left<<out<<endl;
outfile<<setw(10)<<left<<L<<setw(10)<<left<<W<<setw(10)<<left<<H_Box<<setw(10)<<left<<D<<setw(10)<<left<<H_Jar<<setw(10)<<left<<out<<endl;
}
infile.close();
outfile.close();
return 0;
}//end of the main function
//print_layout funcrtion defination
void print_layout()
{
cout<<" "<<endl;
cout<<" Box: Jar: Jarfit? "<<endl;
cout<<setw(10)<<left<<"L"<<setw(10)<<left<<"W"<<setw(10)<<left<<"H_Box"<<setw(10)<<left<<"D"<<setw(10)<<left<<"H_Jar"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.