CPSC 231: Introduction to Computer Science for
Computer Science Majors I
Assignment 2: Graphing Calculator
Weight: 7%
Collaboration
Discussing the assignment requirements with others is a reasonable thing to do, and an
excellent way to learn. However, the work you hand-in must ultimately be your work. This is
essential for you to benefit from the learning experience, and for the instructors and TAs to
grade you fairly. Handing in work that is not your original work, but is represented as such, is
plagiarism and academic misconduct. Penalties for academic misconduct are outlined in the
university calendar.
Here are some tips to avoid plagiarism in your programming assignments.
1. Cite all sources of code that you hand-in that are not your original work. You can put the citation into
comments in your program. For example, if you find and use code found on a web site, include a comment
that says, for example:
# the following code is from
https:
www.quackit.com/python/tutorial/python_hello_world.cfm.
Use the complete URL so that the marker can check the source.
2. Citing sources avoids accusations of plagiarism and penalties for academic misconduct. However, you may still
get a low grade if you submit code that is not primarily developed by yourself. Cited material should never
e used to complete core assignment specifications. You can and should verify and code you are concerned
with your instructo
TA before submit.
3. Discuss and share ideas with other programmers as much as you like, but make sure that when you write your
code that it is your own. A good rule of thumb is to wait 20 minutes after talking with somebody before
writing your code. If you exchange code with another student, write code while discussing it with a fellow
student, or copy code from another person’s screen, then this code is not yours.
4. Collaborative coding is strictly prohibited. Your assignment submission must be strictly your code. Discussing
anything beyond assignment requirements and ideas is a strictly fo
idden form of collaboration. This includes
sharing code, discussing code itself, or modelling code after another student's algorithm. You can not use
(even with citation) another student’s code.
5. Making your code available, even passively, for others to copy, or potentially copy, is also plagiarism.
6. We will be looking for plagiarism in all code submissions, possibly using automated software designed for the
task. For example, see Measures of Software Similarity (MOSS - https:
theory.stanford.edu/~aiken/moss/).
7. Remember, if you are having trouble with an assignment, it is always better to go to your TA and/or instructor
to get help than it is to plagiarize. A common penalty is an F on a plagiarized assignment.
Late Penalty
Late assignments will not be accepted.
Goal
Writing a program with loops and functions
Technology
Python 3
Submission Instructions
You must submit your assignment electronically. Use the Assignment 2 dropbox in D2L for the
electronic submission. You can submit multiple times over the top of a previous submission. Do
not wait until the last minute to attempt to submit. You are responsible if you attempt this and
time runs out. Your assignment must be completed in Python 3 and be executable with Python
version XXXXXXXXXXFor graphical drawing you must use the Python turtle li
ary.
Description
Charting Expressions (Graphing Calculator)
You will be creating a small graphical Python 3 program. This program will use the turtle li
ary to draw
ased on information taken from the user. You should already have all the experience you need with
this li
ary from Assignment 1.
The assignment requires an additional understanding of converting between Cartesian coordinate
systems, using loops, and completing functions as outlined by someone else. You will be provided with a
starter code that already implements the basic interaction with the user (something you learned in the
previous assignment). This code consists of a completed main function, which sets up the window using
a setup function. There is also a calc expression that takes a string of an expression (Ex. expr=“x**2”)
and returns the y for a given x value (Ex. expr=“x**2” for x=3 will return 9) None of these three
functions should be changed.
The main function makes use of several incomplete functions. Your assignment is to complete these
functions so that the main function can use them to draw what the user requests. All your code should
e written within the incomplete functions. You are allowed to create constants and new functions as
long as they are not duplications of existing ones. There should be no global variables in your code
(except if you are doing the bonus). There is a bonus that involves identify local minima/maxima and
labelling them described at the end of the assignment.
Your program will be drawing in an 800-pixel by 600-pixel window with (0,0) as the bottom left corner
and (800,600) as the top right corner. The starter code has a setup() function, which sets up the
window and returns the turtle drawing object pointer so you can use it. This pointer is passed into the
existing starter code functions for you to use. The included starter code already prompts the user for the
pixel coordinates for where the chart origin should be placed in the window. The code then prompts the
user for a ratio of how many pixels are 1 step of the chart. For example, an origin of (????, ????) =
(400, 300) should be the centre of the screen. If the ratio of pixels per single step is 100 pixels per step.
Then a position one step up from the origin point to (??,??) = (0, 1) in the charting coordinate system is
(???? + ?????????? ∗ ??, ???? + ?????????? ∗ ??)
= XXXXXXXXXX ∗ 0, XXXXXXXXXX ∗ 1)
= (400, XXXXXXXXXX)
= (400, 400).
Below is an image of 3 curves drawn for a chart with an origin at 400,300 and a ratio of 30.
The existing starter code uses the included (but incomplete) functions to draw the x and y axes in black,
and then loops to get expressions from the user. The colour used to draw an expression is determined
ased on the number of previously drawn expressions. So this number is tracked using a counter
variable, that increases by 1 for each new expression entered.
Your task is to complete the included functions, so they function in the way that the comments and
assignment description require. You will likely find the visual examples of operation included in the
assignment description helpful.
In the following diagram. Green functions are complete and provided. Order of parts is
Part 2 Blue – calc_to_screen_coord, calc_minmax_x, calc_minmax_y
Part 3 Grey – draw_x_axis, draw_y_axis, draw_line, draw_x_axis_label, draw_x_axis_tick,
draw_y_axis_label, draw_y_axis_tick
Part 4 Yellow – draw_expression
I recommend approaching the functions in stages:
Part 1: get_color:
First, complete the colour-determination function get_color. This function takes the counter tracked in
the expression input loop and returns one of three different colours: red, green, or blue. You should find
the remainder division operator (also called modulus) helpful here because it allows you to change an
infinitely increasing integer sequence into a range of integers.
Part 2: calc_to_screen_coord, calc_minmax_x, calc_minmax_y:
There are 3 sub-functions to this part. Each does not take a lot of code, but needs to be mathematically
sound.
First, complete the coordinate conversion function calc_to_screen_coord. This function uses the
parameters that the main function obtains user defined pixel coordinate system (x_origin, y_origin,
atio), along with a calculator coordinate system (??,??) to perform the conversion. The (x_origin,
y_origin) is the pixel coordinates of where (0, 0) in the calculator should be located on screen, and ratio
is how many pixels are equal to 1 in the calculator coordinate system. calc_to_screen_coord returns the
(x, y) coordinate as a pixel screen coordinate (screen_x, screen_y) following the math described
previously in the example. Use this function in your code any time you want to convert and draw a
calculator location. You will lose marks if you duplicate the math/code functionality of this function
somewhere else instead of using this function designed for that purpose.
The following image diagrams another example. In it an 800 by 600 window with a 400,300 origin and a
atio of 100. This would mean a (1,1) coordinate system point would fall at (500,400).
Now complete the other two functions. These functions are utility functions useful later in your code to
determine what coordinates in each axis to draw values in between. To simplify things we will be
deciding what axis value is just off the visible screen on each axis. calc_minmax_x is for the x-axis,
calc_minmax_y is for the y-axis. For the x-axis we want to know the calculator location of x=0 and
x=WIDTH as these are the smallest and largest pixel locations we will be drawing. We can invert the
math of calculating a pixel screen coordinate to determine what calculator x values match each of these.
Then we will take the floor of 0-conversion value to be the min, and the ceiling of the WIDTH-conversion
to be the max. We will do the same for the y-axis function, but for y=0 and y=HEIGHT.
Ex. ??_???????????? = 400, ??_???????????? = 300, ?????????? = 100
?????????????? = ?????????????? + ?? ∗ ?????????? XXXXXXXXXXfor ????????????_?? = 0
0 = 400 + ?? ∗ 100
−400/100 = ??
?? = −4.0
???????? = ??????(??????????(−4.0)) = −4
Part 3: draw_line, draw_x_axis, draw_y_axis (tick/label helpers)
Third, complete the functions that draw the x and y axes. These two functions are structurally similar, so
complete one of them first then you can copy and modify it to complete the other function.
When first drawing your axes, you can ignore the tick marks and labels. These can be added in once you
have the lines drawn successfully. Use your part 2 functions to determine the min/max calculator
locations for the x-axis/y-axis. Then draw a line between those two locations. To draw this line complete
and