Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. When asked to create a program to nd the volume of any cone, given its radius

ID: 3624613 • Letter: 3

Question

3. When asked to create a program to nd the volume of any cone, given its radius and
height, a programmer created this program to implement the formula
V = 1/3 r2 h. It compiles, and it runs, but it gives completely incorrect results.

A serious problem lies in the ComputeVolume function. Additionally, the programmer
was confused about parameters and local variables. Make this function work correctly,
and also improve its use of parameters, changing main as necessary. Note any other
minor or stylistic errors you might nd throughout the program. You can simply note
changes on the program listing.

# include <stdio .h>
# define PIE 3.14
float ComputeVolume ( float r, float h, float ans , float r2 );
int main () {
float radius , height , ans , r2;
printf (" Radius : ");
scanf ("%g", & radius );
printf (" Height : ");
scanf ("%g", & height );
ans = ComputeVolume (radius , height , ans , r2 );
printf (" Volume : %g ", ans );
return 0;
}
float ComputeVolume ( float r, float h, float ans , float r2)
{
r2 = r*r;
ans = (1/3)* PIE *r2*h;
return ans ;
}

Explanation / Answer

#include<stdio.h>
#include<conio.h>
# define PIE 3.14
float ComputeVolume ( float r, float h);   // changes are highlighted in red colour.
int main () {
float radius , height , ans , r2;
printf(" Radius : ");
scanf("%g", & radius );
printf(" Height : ");
scanf("%g", & height );
ans = ComputeVolume (radius , height);
printf(" Volume : %f ", ans );
getch();
return 0;
}
float ComputeVolume ( float r, float h)
{
      float ans;
float r2 = r*r;
ans = (PIE *r2*h)/3;
return ans ;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote