Do the following exercises in C. Make sure that you save the file as
.c not .C, and compile using gcc instead of g++. You will
find that the gcc compiler is faster and that your executable code takes
up less space.
Exercise #1
Write a program that computes the volume of any planet in cubic kilometers. Have the volume computation performed by a function named sphere. Arrange to provide the radius of the planet at run time.
Hint: Volume of sphere is 4/3 * pi * r * r * r
To test your program, try these radii values:
Earth's radius = 6378.2 km
Jupiter's radius = 71492 km
Venus' raduius = 6051.8 km
Exercise #2
The energy of a moving mass is given by the formula 1/2 * m * v * v. Write a program that determines the energy ratio of a car moving at two speeds provided at run time. Write and use a function named square to calculate the square of v. Also, write and use a function named energy to calculate the energy of a car. Use your program to determine how much more energy a car has when moving at 80 miles per hour than when it is moving at 55 miles per hour.
Prompt the user for the mass of the vehicle and the two velocities to be
compared.
Exercise #3
Array exercise
- Your program should ask the user to enter the number of float values to
be entered and then allow the user to enter those floating point values.
- Store these values in an array of floats.
- Calculate the mean, variance, standard deviation, smallest value,
and largest value. Print out those values.
- You may want to put all the values into a file and redirect the
standard input to come from this file.
- If you feel ambitious, try using functions to break the program
up. Write a function that returns the mean. Write a function that
returns the variance. Write a function that returns the standard
deviation.
- mean
- - average of all the values
- variance
- - average of the squared distances between each value and
the mean
- standard deviation
- - square root of the variance