CS-211 Fall 2015 Lab 2 Calling Functions
After you have logged on to one of the Linux Lab machines, open a terminal window like you did last week. (Go back to last week's lab if you forgot how.) Then, make a lab2 subdirectory of your home directory. Download graph.c and move it into you lab2 directory. (Note... in some browsers, you may need to right click on the graph.c hyperlink, and choose the Save Link As... option in order to download)
Compiling and Running graph.c
Make lab2 your current directory, and edit the graph.c code using the command gedit graph.c&, like we learned last week. Browse through the code. Notice that there are five functions defined already... constant, linear, quadratic, graphFn, and main.
- The top level function is "main". Currently main expects you to invoke the program with a single argument that consists of a single letter, either 'C' or 'L'. If the program is invoked with 'C', then main invokes the "graphFn" function with a "constant" argument. If the program is invoked with 'L' then main invokes the graphFn function with a "linear" argument.
- The "constant" function takes a single argument, "x", but returns 8 no matter what. Think of this like "constant(x)=8"
- The "linear" function takes a single argument, "x", and returns two times x plus 5. Think of this like "linear(x)=2x+5"
- The "graphFn" function is much more complicated -- we haven't learned enough C to understand exactly how it works, but the basic idea is that the graphFn function will create an (x,y) graph for you. The first four arguments, fx, fy, tx, and ty give the (x,y) coordinates of the lower left corner of the graph, and the upper right corner of the graph. The fifth argument, "fn", contains a function. Specifically, the function must be a function that takes a single integer argument, and returns a single integer result, like "constant" and "linear". The "graphFn" function will invoke fn for several different x values, and treat the result as the y value. Then, graphFn will print out the resulting graph to the terminal. Browse through the code for the graphFn function and see if you can follow how graphFn works.
Once you have browsed through the code, go back to your terminal window, and compile the code using the command gcc -g -o draw graph.c. Unless you have changed something, the code should compile with no warnings or errors.
Once the code is compiled, try running it. Start out running with no parameters using ./draw. You should get a message telling you that you need a parameter. Try again with ./draw C. You should now see a graph of a constant function on your screen. You may have to scroll up to see the top of this graph. Try ./draw L to see a linear graph.
Making Some Changes
Now, let's start changing the code in graph.c. Go to the edit window.
- First, change the constant function to return a value different than 8. Recompile and rerun. Do you see how the graph changed?
- Play around with the size of the graph. Notice that there are two lines at the top of the file that define the numbere of rows and the number of columns in a graph. Try changing these from 40 to some other values. Recompile and rerun. Did your graphs change?
- The main function invokes the graphFn with hard-coded lower left and the upper right corners of the graphs. Try changing these values, recompiling and rerun. Did this work as you expected?
- Create a new function called quadratic to graph.c. Define this function so that it takes an integer argument called "x", and return -x2 +3x +7. Hint... the easiest way to calculate x2 in C is simply x*x. You will also need to change the "main" function so that when draw is invoked with a first argument of "Q", you invoke graphFn with "quadratic" as the fifth parameter. Compile and invoke your code.
- Can you change the linear function to take co-efficients as arguments rather than hard-coding the slope and the y intercept? What happens when you try to do this?
If you are surprised by any of these results, you may want to debug the code. To run under the debugger, use the commmand gdb draw. Then, set breakpoints on the line or lines you want to stop by typing (for instance) break 7 at the (gdb) prompt to break at line 7 in your code. Then use the (gdb) sub-command run L (or run C or run Q) to start the process and get to the first breakpoint.
Download and edit the following file: lab2_report.txt. Then submit your editted file on Blackboard under the main CS-211 class page (not the lab section page), under "Contents" in the Lab_02 report area.