C++ Visual studio only, in file comments would be appreciate it Data file (Data3
ID: 3849878 • Letter: C
Question
C++ Visual studio only, in file comments would be appreciate it
Data file (Data3.txt):
Note: use simple math functions to solve the problem:
Note: The file has 27 numeric values, the first value 26, tells you there are 26 values after this number. The number 26 is not to be used in calculations.
Read from the file (Data3.txt) and calculate the volume of a sphere. The values that you are reading are in centimeters. After this done you will need to evaluate if the volume of the sphere is over 1.250 m^3 you will assign the value of the sphere as large. Others will be assigned a value of small. You will then need to write the properties to a text file. The output should resemble the table below. The volume needs to be output to 2 digits with units. The output file name will be volume.txt.
Number Volume Radius Property 1 2.14m^3 0.80m large 2 0.14m^3 0.32m smallExplanation / Answer
c++ code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string filename = "Data3.txt";
string line;
ifstream myfile (filename.c_str());
ofstream myoutfile ("Volume.txt");
if (myfile.is_open())
{
vector< int > tokens;
while ( getline (myfile,line) )
{
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
// Create vector to hold our words
while (ss >> buf)
{
tokens.push_back(atoi(buf.c_str()));
}
}
myfile.close();
myoutfile << "Number" << " " << "volume" << " " << "radius" << " " << "Property" << endl;
for (int i = 1; i < tokens.size(); ++i)
{
float r = float(tokens[i])/100.0;
float volume = (4.0/3.0)*3.14*r*r*r;
if(volume > 1.25 )
{
myoutfile << i<< setprecision(2) << " " << volume<< " " << r << " " << "Large" << endl;
}
else
{
myoutfile << i << setprecision(2)<< " " << volume << " "<< r << " " << "Small" << endl;
}
}
myoutfile.close();
}
else
{
cout << "Unable to open file" << endl;
exit(1);
}
return 0;
}
Sample Data3.txt
9 30 40 60 10 50 20 10 90 99
Sample Output:
Number volume radius Property
1 0.11 0.3 Small
2 0.27 0.4 Small
3 0.9 0.6 Small
4 0.0042 0.1 Small
5 0.52 0.5 Small
6 0.033 0.2 Small
7 0.0042 0.1 Small
8 3.1 0.9 Large
9 4.1 0.99 Large
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.