Write a program cylinder.c that accomplishes the following: Read a list of radii
ID: 666281 • Letter: W
Question
Write a program cylinder.c that accomplishes the following:
Read a list of radii and heights from a file(cylinder_input.data). For each pair of values, calculate the
cylinder's volume and surface area. Display the results on the screen and save the results to a file.
File: cylinder_input.data
1.5 10.2
2.21 20
3 30.243
55.23 2.2
12.1 45.989
The contents of the output file “cylinder_output.data”
1.500000 10.200000 72.099551 110.269902
2.210000 20.000000 306.877054 308.404496
3.000000 30.243000 855.100680 626.615787
55.230000 2.200000 21082.525775 19929.377237
12.100000 45.989000 21153.127133 4416.305811
Explanation / Answer
cylinder.c
/*
Objective :-
Given the Radii & Height of a cylinder,
Write `cylinder.c` program to Calculate the Volume & Surface Area of the cylinder,
and display the results in the specified format on the screen & store them in output file as well.
Area & Volume are computed by below formula :-
area = (2*pi*radii)*(height + radii);
volume = pi * (radii*radii) * height;
where `pi` is universal constant with value of 3.14159265359
The C program written below is well explained in terms of what is done by various statements.
The flow of program goes this way :-
1). Declaration of
*File pointers which are used as refernece to the input file & output file.
*Character Buffer which stores the computed data in this format 'radii height volume area'
2). Opening the files
*the input file is opened using `freopen`
*the output file is opened using `fopen`
- if we would have used `freopen` for opening the output file, with the third argument as `stdout`,
then all the output functions including the `printf`, would have redirected the output to the output file, not to the console.
So, `fopen` is used, so that we can redirect the output to console using `printf` and to the file using `fputs`
3). Reading the values of radii & height from the file.
4). Computing the Volume & Area with the read data.
5). Storing the result in `buf`(a character buffer) in this format 'radii height volume area'
6). Output the `buf` value on screen using `printf`
7). Output the `buf` value on file using `sputs`
8). Once the whole reading/computing/outputting is done. Close the two files, using `fclose()`.
*/
#include<stdio.h>
#include<stdlib.h>
#define pi 3.14159265359
int main()
{
/* declaration part */
FILE *fpr, *fpw;
double radii, height,area,volume;
char buf[10000];
/* Opening the files */
fpr = freopen("cylinder_input.data", "r", stdin);
fpw = fopen("cylinder_output.data", "w");
/* Traversing the input file till the EOF is reached */
while(getc(fpr) != EOF)
{
fseek(fpr, -1, SEEK_CUR);
scanf("%lf %lf",&radii,&height);
area = (2*pi*radii)*(height + radii);
volume = pi * (radii*radii) * height;
/* Storing the computed results in the character array `buf` */
sprintf(buf, "%lf %lf %lf %lf ", radii, height, volume, area);
/* Printing the value of `buf` on the console */
printf(buf);
/* Storing the value of `buf` in the output file */
fputs(buf, fpw);
}
fclose(fpr); fclose(fpw);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.