Question

7.30 (Calculating Circle Circumference, Circle Area or Sphere Volume Using Function Pointers) Using the techniques you...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import math  #import math for using pow function
pi=3.1415926   #declaring value of pi

#function to calculate circumferece
def circum(radius):
    print("circumference of circle is calculated")
    print("entered radius {}".format(radius))
    print("result {}".format(2*pi*radius))

#function to calculate Area of circle
def area(radius):
    print("Area of circle is calculated")
    print("entered radius {}".format(radius))
    print("result {}".format(pi*radius*radius))

#function to calculate Volume of sphere
def volume(radius):
    print("Volume of sphere is calculated")
    print("entered radius {}".format(radius))
    print("result {}".format((4/3)*pi*pow(radius,3)))

#function that will call other function acc. to choice
def calc(radius ,choice):
    if choice == 1:
        return circum(radius)
    elif choice == 2:
        return area(radius)
    elif choice == 3:
        return volume(radius)
    else:
        print("invalid choice!!!!")
        print("----again choose please------------")
        main()

def main():
    # menu-driven
    print("choose what you want")
    print("enter 1 for circumference of a circle:")
    print("enter 2 for area of a circle:")
    print("enter 3 for volume of a sphere:")

    # enter choice here
    choice = int(input("enter your choose:"))
    radius = float(input("enter radius:"))
    if radius >= 0.0:  # condition that check value of radius
        calc(radius, choice)
    else:
        print("Radius should be greater than 0")

main()
Add a comment
Know the answer?
Add Answer to:
7.30 (Calculating Circle Circumference, Circle Area or Sphere Volume Using Function Pointers) Using the techniques you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT