The volume of the cylinder can be calculated using the
following equation :
volume ( v ) = π * r * r * h
Write a C program that reads the radius (r) and height (h) of
cylinder from the screen, and determine volume (v) of
cylinder. The program should continue request user to enter r
and h, until the value of (-1) is entered for the radius. Then
the program is terminated.
Use function called “calculatevolume” to determine volume (v)
of cylinder. The function “calculatevolume” should have 3
arguments. The first two arguments are floating-point values
for radius and height. The last argument is reference argument
(pointer) for volume. The function returns no value to the
calling function.
An example of input/output dialog is shown below
Enter radius : 1.62
Enter height : 6.23
Volume of cylinder = 51.3651
Enter radius : 2.86
Enter height : 7.52
Volume of cylinder = 193.2412
Enter radius : -1
My wrong answer :
#include <stdio.h>
#define PI 3.14159
float calculatevolume(float radius,float height,float volume);
int main()
{
float radius, height, volume; //declare variables;
radius=height=volume = 0; //initialize variables as 0;
while(radius != -1)
{
printf("Please enter the radius of the cylinder (-1 to quite): ");
scanf("%d", &radius);
if(radius != -1)
{
printf("Please enter the height of the cylinder: ");
scanf("%d", &height);
volume=calculatevolume(radius, height, volume);
printf("The volume of the cylinder is %d\n", volume);
}
}
return 0;
}
you have defined volume in the main method. then you set that variable which should not be visible in the volume method. then you return it.
1. Keep volume global in your class, then put it into your header file e.g. as private and set that in the volume method --> then you dont have to return anything; you just set in in the method.
2. or make the volume var local in the method
int main()
{
float radius, height; //declare variables;
radius=height= 0; //initialize variables as 0;
while(radius != -1)
{
printf("Please enter the radius of the cylinder (-1 to quite): ");
scanf("%d", &radius);
if(radius != -1)
{
printf("Please enter the height of the cylinder: ");
scanf("%d", &height);
float main_volume=calculatevolume(radius, height, volume);
printf("The volume of the cylinder is %d\n", main_volume);
// I am not sure about the format %d, it might be %f for float numbers?
}
}
return 0;
}
That's correct. The function is meant to accept a pointer to volume, so will change the value of volume in the main function without the need to return a value.
[1] The function “calculatevolume” should have 3 arguments.
[2] The first two arguments are floating-point values for radius and height.
[3] The last argument is reference argument (pointer) for volume.
[4] The function returns no value to the calling function.
int main()
{
float radius, height; //declare variables;
radius=height= 0; //initialize variables as 0;
while(radius != -1)
{
printf("Please enter the radius of the cylinder (-1 to quite): ");
scanf("%f", &radius);
if(radius != -1)
{
printf("Please enter the height of the cylinder: ");
scanf("%f", &height);
float fvolume=calculatevolume(radius, height, &volume);
printf("The volume of the cylinder is %f\n", fvolume);
}
}
return 0;
}
int main()
{
float radius, height; //declare variables;
radius=height= 0; //initialize variables as 0;
while(radius != -1)
{
printf("Please enter the radius of the cylinder (-1 to quite): ");
scanf("%f", &radius);
if(radius != -1)
{
printf("Please enter the height of the cylinder: ");
scanf("%f", &height);
float fvolume=calculatevolume(radius, height, volume);
printf("The volume of the cylinder is %f\n", fvolume);
}
}
return 0;
}