Description
Problem Solving and Program Design Using C
(CSE 3942)
Department of Computer Science & Information Technology
Faculty of Engineering & Technology (ITER)
Bhubaneswar, Odisha – 751030
Lab Assignment-2
2. Programming project on Top-Down Design With Functions
2.1 You have saved $500 to use as a down payment on a car. Before beginning your car shopping, you decide to write a program to help you figure out what your monthly payment will be, given the car’s purchase price, the monthly interest rate, and the time period over which you will pay back the loan. The formula for calculating your payment is
payment =
where P = principal (the amount you borrow) i = monthly interest rate of the annual rate) n = total number of payments
Your program should prompt the user for the purchase price, the down payment, the annual interest rate and the total number of payments (usually 36, 48, or 60). It should then display the amount borrowed and the monthly payment including a dollar sign and two decimal places.
2.2 Write two functions, one that displays a triangle and one that displays a rectangle. Use these functions to write a complete C program from the following outline:
int main(void)
{
/* Draw triangle. */
/* Draw rectangle. */
/* Display 2 blank lines. */
/* Draw triangle. */
/* Draw rectangle. */
}
2.3 Add the functions from Fig. 3.14 to the ones for Programming Project 2 from the Text Book. Use these functions in a program that draws a rocket ship (triangle over rectangles over intersecting lines), a male stick figure (circle over rectangle over intersecting lines), and a female stick figure (circle over triangle over intersecting lines) standing on the head of a male stick figure. Write function skip 5 lines and call it to place five blank lines between drawings.
2.4 For any integer n > 0,n ! is defined as the product n × n − 1 × n − 2… × 2 × 1.0 ! is defined to be 1 . It is sometimes useful to have a closed-form definition instead; for this purpose, an approximation can be used. R.W. Gosper proposed the following such approximation formula:
Create a program that prompts the user to enter an integer n, uses Gosper’s formula to approximate n !, and then displays the result. The message displaying the result should look something like this:
5! equals approximately 119.97003
Your program will be easier to debug if you use some intermediate values instead of trying
to compute the result in a single expression. If you are not getting the correct results, then you can compare the results of your intermediate values to what you get when you do the calculations by hand. Use at least two intermediate variables – one for 2 and one for
. Display each of these intermediate values to simplify debugging. Be sure to
use a named constant for PI, and use the approximation 3.14159265. Test the program on nonnegative integers less than 8.
2.5 Write a program that takes a positive number with a fractional part and rounds it to two decimal places. For example, 32.4851 would round to 32.49, and 32.4431 would round to 32.44. (Hint: See “Rounding a number” in Table 2.12 and function scale in Fig. 3.23 from the Text Book.)
2.6 Four track stars have entered the mile race at the Penn Relays. Write a program that scans in the race time in minutes (minutes) and seconds (seconds) for a runner and computes and displays the speed in feet per second (fps) and in meters per second (mps). (Hints: There are 5,280 feet in one mile, and one kilometer equals 3,282 feet.) Write and call a function that displays instructions to the program user. Run the program for each star’s data.
Minutes Seconds
3 52.83
3 59.83
4 00.03
4 16.22
2.7 In shopping for a new house, you must consider several factors. In this problem the initial cost of the house, the estimated annual fuel costs, and the annual tax rate are available. Write a program that will determine the total cost of a house after a five-year period and run the program for each of the following sets of data.
Initial House Cost Annual Fuel Cost Tax Rate
67,000 2,300 0.025
62,000 2,500 0.025
75,000 1,850 0.020
To calculate the house cost, add the initial cost to the fuel cost for five years, then add the taxes for five years. Taxes for one year are computed by multiplying the tax rate by the initial cost. Write and call a function that displays instructions to the program user.
2.8 A cyclist coasting on a level road slows from a speed of 10 mi/hr to 2.5 mi/hr in one minute. Write a computer program that calculates the cyclist’s constant rate of acceleration and determines how long the cyclist will take to come to rest, given an initial speed of 10 mi/hr. (Hint: Use the equation
where a is acceleration, t is time interval, v1 is initial velocity, and vf is final velocity.) Write and call a function that displays instructions to the program user and a function that computes a, given t,vf, and vr.
2.9 A manufacturer wishes to determine the cost of producing an open-top cylindrical container. The surface area of the container is the sum of the area of the circular base plus the area of the outside (the circumference of the base times the height of the container). Write a program to take the radius of the base, the height of the container, the cost per square centimeter of the material ( cost ), and the number of containers to be produced ( quantity ). Calculate the cost of each container and the total cost of producing all the containers. Write and call a function that displays instructions to the user and a function that computes surface area.
2.10 Write a program to take a depth (in kilometers) inside the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit. The relevant formulas are
Celsius = 10 (depth) + 20 (Celsius temperature at depth in km)
Fahrenheit = 1.8 (Celsius) + 32
Include two functions in your program. Function celsius at depth should compute and return the Celsius temperature at a depth measured in kilometers. Function fahrenheit should convert a Celsius temperature to Fahrenheit.
2.11 The ratio between successive speeds of a six-speed gearbox (assuming that the gears are evenly spaced to allow for whole teeth) is
p5
M/m
where M is the maximum speed in revolutions per minute and m is the minimum speed. Write a function speeds ratio that calculates this ratio for any maximum and minimum speeds. Write a main function that prompts for maximum and minimum speeds (rpm), calls speeds ratio to calculate the ratio, and displays the results in a sentence of the form
The ratio between successive speeds of a six-speed gearbox with maximum speed rpm and minimum speed rpm is .
2.12 Write a program that calculates the speed of sound (a) in air of a given temperature T (◦F). Formula to compute the speed in ft/sec :
Be sure your program does not lose the fractional part of the quotient in the formula shown. As part of your solution, write and call a function that displays instructions to the program user.
2.13 After studying the population growth of Gotham City in the last decade of the 20th century, we have modeled Gotham’s population function as
P(t) = 52.966 + 2.184t
where t is years after 1990, and P is population in thousands. Thus, P(0) represents the population in 1990, which was 52.966 thousand people. Write a program that defines a function named population that predicts Gotham’s population in the year provided as an input argument. Write a program that calls the function and interacts with the user as follows:
Enter a year after 1990 2015
Predicted Gotham City population for 2010 (in thousands):
107.566
Reviews
There are no reviews yet.