CSCE 150A (Fall 2009) Homework 2

Assigned Monday, September 28
Due Monday, October 5 at 11:59pm
Total points: 100


When you hand in your results from this homework, you should submit the following:

  1. The source code of your programs, in text files, named as specified below in Problems 3–6.
  2. An optional text file called README that contains any notes about your programs that you want the graders to know.
  3. A single .pdf file with your writeup of the results for all homework problems that require a writeup (Problems 1, 2, and 6). Only pdf will be accepted, and you should only submit one pdf file, with the name username.pdf, where username is your username on cse. We will not accept any hard copy submissions of your homework!

Put your name in all your files, and submit everything by the due date and time using the web-based handin program.

On this homework, you must work on your own and submit your own results written in your own words.


  1. (10 pts) Rewrite the following mathematical expressions using math functions from the C standard math library (assume all variables are of type double):
    1. ytan 5y )/ y
    2. a3b2 – 5ab cos C
    3. ex sin x
    4. (sin x)2 + (cos y)3x ⋅ 2y
    5. |x4 – 20|

  2. (5 pts) Write a function called manhattan that takes as input four double values x_1, y_1, x_2, y_2 that specify two points (x1, y1) and (x2, y2) and returns the "Manhattan" distance between them computed by using the formula:
    |x1x2| + |y1y2|

  3. (20 pts) Write two functions, one that displays a triangle and one that displays a rectangle. Use these functions to write a complete C program shapes.c from the following outline:
    int main (void)
    {
       /* Draw triangle */
       /* Draw rectangle */
       /* Display two blank lines */
       /* Draw triangle */
       /* Draw rectangle */
    }
    

  4. (20 pts) In addition to the functions from the previous problem, implement the functions draw_circle and draw_intersect from Chapter 3. Use these in a program shapes2.c 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 on the head of a male stick figure. Write a function skip_5_lines and call it to place five blank lines between each pair of drawings.

  5. (20 pts) Write a program projectile.c that computes the duration of a projectile's flight and its height above the ground when it reaches the target. As part of your solution, write and call a function that displays instructions to the program's user, as well as a function to compute the time and a function to compute the height.

    Problem Constant
    G 32.17  /* gravitational constant */                         g
    
    Problem Inputs
    double theta     /* input - angle (radians) of elevation */   θ
    double distance  /* input - distance (feet) to target */      d
    double velocity  /* input - projectile velocity (ft/sec) */   v
    
    Problem Outputs
    double time     /* output - time (sec) of flight */           t
    double height   /* output - height at impact */               h
    
    Relevant Formulas
    t = d / (v cos θ)
    h = v t sin θg t2 / 2

  6. (25 pts) 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 container.c 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 the surface area.

    In your file username.pdf, you are to decompose this problem as exemplified in Chapter 3 (e.g. in the weight of a batch of washers case study). In file container.c, you should put your final program.

Back
Last modified 16 August 2011; please report problems to sscott.