Can anybody help me to solve this C Problem?

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;
}

float calculatevolume (float radius, float height, float volume)
{
volume=PI*radius*radius*height;
return volume;
}

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;
}

float calculatevolume (float radius, float height, float volume)
{
float t_volume = PI*radius*radius*height;
return t_volume;
}

Still there is one error! It can not read main_volume !!!
And yes it will be %f... can anybody help again??
Don't forget this:
The function returns no value to the calling function.
That means there will be no "return volume"?
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.
Last edited on
closed account (1vRz3TCk)
[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.


void calculatevolume(float radius, float height, float * volume)
\__/ \_____________/ \________________________/  \____________/
  4        1                     2                       3
Last edited on
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;
}

float calculatevolume (float radius, float height, & volume)
{
float t_volume = PI*radius*radius*height;
return fvolume;
}

Is it correct???
Is it correct???


Your assignment came with test cases, so you can run it and check yourself. That aside, no, it isn't.


The function returns no value to the calling function.

Your function returns a float and you appear to have defined a new type that you have labelled "&".
1
2
3
4
float calculatevolume (float radius, float height, & volume)
{
  float t_volume = PI*radius*radius*height;
  return fvolume;}

Last edited on
#include <stdio.h>
#define PI 3.14159

void calculatevolume(float radius, float height, float * volume);

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;
}

void calculatevolume(float radius, float height, float * volume)
{
float t_volume = PI*radius*radius*height;
}

Error 3 error C2143: syntax error : missing ';' before 'type'

Error 4 error C2065: 'fvolume' : undeclared identifier

Oh! It is not working!
closed account (1vRz3TCk)
I think you need to read up on pointers and functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#define PI 3.14159

void 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("%f", &radius);
        if(radius != -1)
        {
            printf("Please enter the height of the cylinder: "); 
            scanf("%f", &height);
            calculatevolume(radius, height, &volume);
            printf("The volume of the cylinder is %f\n", volume);
        }
    }
    return 0;
}

void calculatevolume(float radius, float height, float * volume)
{
    *volume = PI*radius*radius*height;
}


___________________
PS use [code] and [/code] to post code
I just wrote the code and came here reply for this post, but CodeMonkey already replied for it... :-(

@rizwanul..
you need to read on pointers and functions section..
Last edited on
Topic archived. No new replies allowed.