programming project help c language

7.30 (Calculating Circle Circumference, Circle Area or Sphere Volume Using Function Pointers)

Using the techniques you learned in Fig. 7.28, create a text-based , menu-driven program that allows
the user to choose whether to calculate the circumference of a circle, the area of a circle or the
volume of a sphere.

The program should then input a radius from the user, perform the appropriate calculation and
display the result. Use an array of function pointers in which each pointer represents a function
that returns void and receives a double parameter . The corresponding functions should each display
messages indicating which calculation was performed, the value of the radius and the result of the
calculation.

For the purposes of this exercise, assume pi is equal to 3.1415926. In your calculation functions,
display a message that indicates the value for radius read in and the result of the calculation. In
both cases, display the value to 7 digits of precision.

If the user enters a selection not present on your menu, indicate the error and try again until a
correct selection is made. Also, do not accept values for the radius that are less than 0.0.



My code so far :
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include<math.h>

void circumference(double radius);
void area(double radius);
void volume(double radius);
void print_menu();
const float PI=3.1415926;

int main()
{
	void(*f[3])(double)={circumference,area,volume};

	size_t selection=0;

	while(selection!=3)
	{

		print_menu();
		do
		{
			scanf("%d",&selection);
		}while(selection<0||selection>3);

		if(selection!=3)
		{
			(*f[selection])(selection);
		}
		else
		{
			printf("Done. Program terminated.");
		}
	}

	while(1);
	return 0;
}

void print_menu( void )
{
	printf( "=== Circles & Spheres Menu ===\n\n"
        "Please select from the options below:\n\n"
	"(0) Calculate the circumference of a circle.\n"
	"(1) Calculate the area of a circle.\n"
	"(2) Calculate the volume of a sphere.\n\n"
	"Enter your selection:");
}

void circumference (double radius)
{
	puts("Enter the radius of your figure:");
	scanf("%f",&radius);

	printf("\nRadius= %f",radius);

	double circ = 2*PI*radius;
	printf("%s%.2f%s\n\n","\nThe circumference of the circle is ",circ," meters.");
}

void area (double radius)
{
	printf("Enter the radius of your figure:");
	scanf("%f",&radius);

	printf("\nRadius= %f",radius);

	double area = PI*radius*radius;
	printf("\n%s%.2f%s\n\n","The area of the circle is ",area," square meters.");
}

void volume (double radius)
{
	puts("Enter the radius of your figure:");
	scanf("%f",&radius);

	printf("\nRadius= %f",radius);

	double volume = (4/3)*PI*pow(radius,3);
	printf("%s%.2f%s\n\n","\nThe volume of the sphere is ",volume," cubed meters.");
}

Last edited on
First: Don't use while(1), use something like getchar so you can analyze the output while you can exit properly when needed. Also, since the query message and code is the same for all functions, query it in main and pass the values correctly, otherwise the parameters are meaningless.

Also, you didn't specify a problem. I presume it's not behaving as expected, so what's the matter?
Topic archived. No new replies allowed.