Lab 4: Composition with Functions

This assignment assumes that you have read up through Chapter 5. Please bring your book to lab, as you will need it as a reference. Each lab assignment is more work than the previous one, so get started early.

The objectives for this assignment are for you to:

  • Demonstrate how to prototype, define, and invoke functions in C.
  • Gain experience in decomposing large programs into a series of small tasks.
  • Get more practice writing event driven and graphical programs.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab4
    cd cse20211/lab4
    
    For this assignment, you will be using the simple graphics library created for this class. Follow that link, download the example program, and skim through the function reference. (Return to it later as needed.)

    Part 1: Numeric Typewriter

    Write a program (numbers.c) that opens a window and waits for user input. When the user presses a key, do the following:
  • 0-9: Draw that numeral at the current mouse location.
  • =: double the size of future numerals
  • -: halve the size of future numerals
  • q: quit the program
  • The size of each numeral should initially be 100 pixels high. The width of each numeral should be half its height. The equals and minus keys won't change the symbols on the screen, but the size of symbols that will be drawn next. This program must make extensive use of functions. Each shape should definitely have its own function, and accept parameters that control that location and the size of the symbol.

    Try writing several functions in this order:

  • void draw_one( int x, int y, int height );
    Draws the numeral 1 as a stick figure, where the width is exactly one half of the height. The parameters x and y indicate the upper left hand corner of the figure.
  • void draw_two, draw_three, ..., draw_nine, draw_zero
    Draws the given numeral as above.
  • void draw_numeral( int x, int y, int n, int height );
    Draws the single numeral 'n' (0-9) at the given position
  • Part 2: Graphing Calculator

    Write a program (graph.c) that draws a detailed graph of the Taylor-series expansion of sin(x):
    T(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
    
    The program should open a large window and display the X and Y axes on the range domain [-10,10] with tics and appropriate numeric labels. The function should then be graphed in a color distinct from the axes.

    The program will how show the approximation varies with the number of terms computed. Initially, you should only display the first-term approximation of simply T(x)=x. If the user presses the plus (or minus) key, that should increase (or decrease) the number of terms used in the function by one and cause the function to be re-plotted. The number of terms used in the current display should be shown on the screen.

    That is, the program should initially plot the function T(x)=x, if the user presses the plus key, then it should plot T(x)=x - x^3/3! and wait for more input. As the number of terms is increased, the function T(x) will look more and more like sin(x).

    As above, make extensive use of functions to keep your code short and simple. You should have a function to compute the Taylor series at a point x, a function to plot the function on the screen, a function to draw the X axis, a function to draw an integer, and so forth. If any one function is more than 5-10 lines long, then it is probably getting too complicated, and you should break it into smaller functions.

    Part 3: Carnival Ride

    Design an outrageous carnival ride guaranteed to make the riders turn green. Write a program (carnival.c) that smoothly animates the motion of the ride continuously until the user presses the q key. The ride must contain at least four different axes of motion all chained together, some of them circular and some linear.

    For example, you might have a slider moving left and right, from which is hung a pendulum that swings back and forth, at the end of which are four arms spining in a circle, each with a piston sliding in and out radially, each with a little car with a rider at the end.

    Keep the program simple and easy to read by using functions extensively. Each axis of motion should be its own function that draws some part of the structure, then calls one or more function(s) to draw the next part(s) with its own axis of motion.

    Be creative!

    Turning In

    Turn in numbers.c, graph.c, carnival.c, and report.txt. Your lab report should explain the final part of the assignment, explaning how it works from the user perspective, how the program works internally, and how you verified that the output of the program is correct.

    Turning In

    This assignment is due on Monday, Sept 30th at noon Wednesday, October 2nd, at 5PM. Late assignments are not accepted.

    Please review the general instructions for turning in.