CW1: Function Grapher for Hackers
Read the whole document before you start. Particularly the slide regarding plagiarism.
Coursework Objective
You will write a c program that produces the function graph of , for any in the terminal window of VSCode.
A complete program should produce something like:
Function graph for sin(2.00x+1.57):
|** **** ** | * ** ** ** |** ** * |**** |**** X---------------X---------------X---------------X-------------- |****
| * ** * * |** *** |**** *** | **** *****
Why Terminal Grapher?
Although many software enable you to generate function graphs easily, it is not always easy to include graphs in traditional text based editors.
You want to post a function graph on reddit, which does not allow comments with images.
You want to attach a function graph in the email, but your editor only accepts text messages.
Part I
-
Download the CW file, and unzip it to your labpack.
-
Open grapher.c .
-
Create a function plot with two inputs a and b and no output.
-
In lecture 4, we talked about how to write code to produce the following output:
****
****
****
****
4. Copy and paste relevant code to plot , so now you can see the above output when you call plot in main .
Part II
0. Now, remove the printf statement in your code.
1. Modify your loops, so it prints out the following coordinates .
1.00, 0.00
1.00, 0.10 //the second coordinate increases by .1
...
1.00, 6.20 //the second coordinate stops at 6.20
0.80, 0.00 //the first coordinate decreases by .2
0.80, 0.10
...
0.80, 6.20
...
-1.00, 0.00
-1.00, 0.10
...
-1.00, 6.20 // the first coordinate stops at -1.00
Part II
Hint, what output will the following code produce?
for(double i = 0.0; i <= 1.0; i = i + 0.1){ printf("%.2f\n", i);
}
Make sure you can produce the correct output before you continue.
Part III
Now, remove the printf statement in your code. Write code according to the following description:
For each coordinate ,
If , print out *
otherwise, print out an empty space
.
Hint: The absolute value function is fabs(a) .
With some additional tricks you have learned from the lectures/labs, you should be able to produce:
Part III
**** **
** **
*** ***
** **
*********
** ** *
** **
*** ***
** **
** ****
*********
At this moment, there should be about 10 lines of code in your plot function. Do not over engineer your code.
Part IV (Challenging)
Modify your code, so plot(a,b) produces the graph for function .
Add a title to your graph:
Function graph for sin(ax+b) , where a and b will be
replaced with the actual values of a and b .
Part V (Challenging)
Modify your code, so it produces a horizontal axis like this:
Function graph for sin(2.00x+1.57):
*** **** **
* ** ** **
** ** * ****
**** ---------------------------------------------------------------
**** * ** * * ** *** ** ** * ** **** *****
Part V (Challenging)
Modify your code, so it marks with X .
Function graph for sin(2.00x+1.57):
*** **** **
* ** ** **
** ** * ****
**** X---------------X---------------X---------------X--------------
**** * ** * * ** *** ** ** * ** **** *****
Part V (Challenging)
Now, add the verticle axis
Function graph for sin(2.00x+1.57):
|** **** ** | * ** ** ** |** ** * |**** |**** X---------------X---------------X---------------X-------------- |****
| * ** * * |** *** |**** *** | **** *****
You should be able to do all 5 parts with less than 50 lines of code (my answer uses 34 lines of code). Do not over engineer your code (such as adding user interface).
Marking Criteria
5% for submitting a C file with the correct file name, i.e., ab1234.c where ab1234 is your email address before @ .
10% for submitting a C file that compiles.
15% for submitting a C file that produce the correct output for part I.
30% for submitting a C file that produce the correct output for part II.
50% for submitting a C file that produce the correct output for part III.
60% for submitting a C file that produce the correct output for part IV.
Marking Criteria
80% for submitting a C file that produce the correct output for part V.
20% additional points for good coding practice:
Good code format.
Apt use of comments.
Good variable/function naming scheme.
Plagiarism
You can discuss about the general strategy of your code with your coursemates.
You must write the code independently! Do not write code together!
Academic collusion is a serious offense. Certainly do not copy other people's code.
You can use code you found on internet, but you MUST provide the source in the comments. If we found identical source code from the internet without any mentioning or citation, it will be regarded as plagiarism.