I'm writing a simple program to calculate the surface area and volume of a cylinder. Instead of writing all my code in main(), I'm using 3 functions.
The first function reads the input data of height and radius, passing the values back to the main program.
The second function uses the arguments height and radius to calculate the values of area and volume, passing each one back to the main program.
The third function uses the arguments area and volume in a print statement.
#include<stdio.h>
#define PI 3.14159
static void inputData(double *ph, double *pr);
static void computeResults(double h, double r, double *pa, double *pv);
static void displayAnswer(double a, double v);
main()
{
double h,r,a,v;
inputData(&h, &r);
computeResults(h, r, &a, &v);
displayAnswer(a, v);
return;
}
static void inputData(double *ph, double *pr)
{
printf("Height: ");
scanf("%g", ph);
printf("Radius: ");
scanf("%g", pr);
}
static void computeResults(double h, double r, double *pa, double *pv)
{
*pa = (2 * PI * h * r);
*pv = (PI * h * r * r);
}
static void displayAnswer(double a, double v)
{
printf("The Area is: %f", a);
printf("The Volume is: %f", v);
}
I'm having an issue with the scanf lines in the inputData functions.
Error message:
surfaceAreaCylinder.c:22:8: warning: format ‘%g’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=] scanf("%g", ph); ^
surfaceAreaCylinder.c:24:8: warning: format ‘%g’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=] scanf("%g", pr);
I have a fundamental understanding of pointers, and know that scanf must return the information to the caller, therefore the arguments after the control string must use call by reference. Please help me understand!
pass by valuenot by reference - krpramath.hthere is a value of PI - Ed Heal*pa = (2 * PI * h * r);-->*pa = 2 * PI * r *(h + r);? - BLUEPIXY