create a structure called shape then in main compute the voume and sort the inpu
ID: 3624725 • Letter: C
Question
create a structure called shape then in main compute the voume and sort the input txt. shapes in ascending order to produce output.txt.
sample input.txt:
3.7 7.1 9.4 blue
5.1 7.8 5.6 green
6.1 2 4.9 pink
7.2 8.4 1.1 black
9.2 2.4 1.2 purple
6.6 10.7 5 purple
1.9 6.4 4.6 red
9.7 10.7 4 blue
4.2 9.8 3 blue
3.3 1 10.9 red
6 9.6 8.8 pink
2.7 3.4 4.7 blue
4.6 8.6 1.5 purple
1.1 10.5 7.8 yellow
Sample output.txt:
David McPherson
ECE 1574: Lab 10
Height Length Width Volume Color
--------------------------------------
9.20 2.40 1.20 26.50 purple
3.30 1.00 10.90 35.97 red
2.70 3.40 4.70 43.15 blue
1.90 6.40 4.60 55.94 red
4.60 8.60 1.50 59.34 purple
6.10 2.00 4.90 59.78 pink
7.20 8.40 1.10 66.53 black
1.10 10.50 7.80 90.09 yellow
4.20 9.80 3.00 123.48 blue
5.10 7.80 5.60 222.77 green
3.70 7.10 9.40 246.94 blue
6.60 10.70 5.00 353.10 purple
9.70 10.70 4.00 415.16 blue
6.00 9.60 8.80 506.88 pink
Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct shape {
double height,length,width,volume;
string color;
};
int main ()
{ifstream in;
ofstream out;
shape vol[50],temp;
int count=0,i,j;
char filename[20];
cout<<"Enter input file name: ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
cout<<"Enter output file name: ";
cin>>filename;
out.open(filename);system("pause");
in>>vol[count].height;
while(in)
{in>>vol[count].length>>vol[count].width>>vol[count].color;
vol[count].volume=vol[count].length*vol[count].width*vol[count].height;
count++;
in>>vol[count].height;
}
for(i=0;i<count-1;i++)
for(j=i+1;j<count;j++)
if(vol[i].volume>vol[j].volume)
{temp=vol[i];
vol[i]=vol[j];
vol[j]=temp;
}
out<<"Height Length Width Volume Color ";
out<<setprecision(2)<<fixed<<showpoint;
for(i=0;i<count;i++)
out<<vol[i].height<<" "<<vol[i].length<<" "<<vol[i].width
<<" "<<vol[i].volume<<" "<<vol[i].color<<endl;
out.close();
in.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.