9.) (30) Code a program to read in the radius (r) and the height (h) of a cylind
ID: 3759144 • Letter: 9
Question
9.) (30) Code a program to read in the radius (r) and the height (h) of a cylinder from a file. Calculate the surface area (2(pi r^2) + (2 pi r) h) and volume (pi r^2 h) of the cylinder. Print the answers to the screen with appropriate column headings. There will be multiple sets of data - choose your own end of file handling (sentinel, count first, physical end of the file). The calculations and output must be done In at least three separate methods (other than main) which will be called by main. Use all local variables, except pi, which may be set up as a global constant. Use the bottom of this sheet.Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
const float pi = 3.14;
void readFromFile(ifstream &file, float &r, float &h){
file>>r>>h;
}
void calc(float r, float h, float &surfaceArea, float &volume) {
surfaceArea = 2*pi*r*r* + 2*pi*r*h;
volume = pi*r*r*h;
}
void print(float r, float h, float &surfaceArea, float &volume) {
printf("%.2f %.2f %.2f %.2f ", r, h, surfaceArea, volume);
}
int main() {
ifstream file;
file.open("read.txt");
printf("Radius Height Surface Area Volume ");
while (!file.eof()) {
float r,h, surfaceArea, volume;
readFromFile(file, r, h);
calc(r, h, surfaceArea, volume);
print(r, h, surfaceArea, volume);
}
cout<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.