Assignment three questions and answers

Here are some questions and answers about assignment three. Suggestions for additions to this list are welcome (via e-mail).


Q: I get an "undefined reference" error when I use any math library function, even though I am #including <math.h>.

A: You need to link with -lm as well. To use a math library function, you need two things:

  1. an 'extern' declaration in the file which calls it
  2. compile with the file which defines it
This is just like the calling of plot() from main.c or testmain.c. You #include "plot.h" to provide the 'extern' declaration, but still if you type "gcc -Wall main.c" you will get an error that plot() is not defined. You have to type "gcc -Wall main.c plot.c" (and also functions.c once you use something from functions.c).

Similarly, when you call a math library function, you both need the #include <math.h> in the file calling it and to link with the file containing that function. You can ask to link with all relevant functions from the math library by putting "-lm" at the end of your compilation command, e.g. "gcc -Wall main.c plot.c functions.c -lm" (which is how the grading software will be compiling your program for automatic testing).

If more than one different .c file calls a math library function, each .c file which calls any math library function contains #include <math.h> near the top, but you only need one "-lm" in the gcc command line.


Q: In the "odd" function, how do you leave the value undefined for certain inputs? Surely you can't omit the "return" statement...

A: Indeed, your function always must return a value, if it is not declared as "returning" type "void".

In this assignment, we have the concept that some input points can be specified as not being in the domain of the function.

Internally in this program, it returns a value of -999 to indicate "undefined". The caller must test for this or else somehow cause a return value of -999 to mean that an asterisk is not plotted for this 'y' value.


Q: What does it mean in the introduction when it talks about ten rows of output? Aren't there 21?

A: Yes, there are 21. That sentence in the introduction is from an earlier version; it's wrong. Sorry.


Q: I still don't understand what the output is supposed to look like.

A: Please see sample output at http://www.dgp.toronto.edu/~ajr/180/a3/samples.html


Q: My output looks slightly different from yours. For example, "sine 100" in my program is a straight line over the y axis.

A: You are rounding differently. I think my way of doing it is better, but you don't have to do this better way for the assignment.

For example, the sine of 2 radians is about 0.9, times three is 2.7. On a scale of size 100 instead of 10, this is 0.27. You are rounding this to zero and printing your asterisk at character position 21, the y axis. I am rounding this to 0.5 and printing my asterisk at character position 22, halfway between the axis and the next position. Since two horizontal character-spaces are one vertical character-space, we can position asterisks more accurately in the horizontal dimension than in the vertical dimension.

For the "sine 100" graph, this produces a different graph but it's not necessarily superior. For some of the other sine graphs, and especially for some of the cubic examples there, I think I get a better result.

But as I said, this is entirely optional. You don't have to do it for this assignment, and if you're asking this question, you're probably already done, and it's ok.


Q: How do I deal with the user input? How do I process the "quit" command?

A: Use fgets() and sscanf(), as in lab 8. After you sscanf() the line into a string and a possible number, the sscanf() return value will tell you whether there is a number there. And you can check if strcmp(string, "quit") == 0 to determine whether the user typed "quit".


Q: Instead of using floor(), can I use the "round" function on page 526 of the textbook? Or does the statement in the assignment mean I have to use floor()?

A: The "round" function on page 526 of the textbook DOES use floor(). Copying this function into your program is quite reasonable. If you do so, then you did use floor().


Q: If I type "chim chiminee" to my main loop, it treats this as a function "chim" with a default scale of 10, because sscanf() returns 1. Is this ok?

A: Yes, you can trust the return value of sscanf() here. It's a little difficult to do better.


Q: What should I put for comments for the little functions in functions.c which is useful but doesn't just echo the code?

A: Nothing, maybe. You could describe them in the prologue comment (in which you could put a few words (very few!) about their use, rather than which functions they are). Probably only odd() is odd enough to deserve a comment of its own, and that might just explain the -999.


[main course page]