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

create a text-based,menu-driven program that allows the user to choose whether t

ID: 3553146 • Letter: C

Question

create a text-based,menu-driven program that allows the user to choose whether to calculate the circumference of a circle,the are 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 represent a function that returns void and receives a double parameter.The corresponding functions should each display message indicating which calculations was performed,the value of the radius and the result of the calculation

Explanation / Answer

#include <stdio.h>

#include <math.h>


void show_circumference(double r) {

printf("Circumference of a circle of radius %lf is %lf ", r, 2 * M_PI * r);

}


void show_area(double r) {

printf("Area of a circle of radius %lf is %lf ", r, M_PI * pow(r, 2));

}


void show_volume(double r) {

printf("Volume of a sphere of radius %lf is %lf ", r, 4 * M_PI * pow(r, 3) / 3);

}


int main() {

int choice = 0;

double r = 0;

void (* functions[4])(double) = {NULL, show_circumference, show_area, show_volume};

printf("Press 1, calculate the circumference of a circle. ");

printf("Press 2, calculate the area of a circle. ");

printf("Press 3, calculate the volume of a sphere. ");

printf("Enter your choice: ");

while (scanf("%d", &choice) == 0 || choice < 1 || choice > 3) {

while (getchar() != ' ');

printf("No kidding! Your choice can be only 1, 2 or 3: ");

}

while (getchar() != ' ');

printf("Enter the radius: ");

while (!scanf("%lf", &r)) {

while (getchar() != ' ');

printf("Please! A decimal number as radius: ");

}

functions[choice](r);

return 0;

}