|
CMPT 120: Assignment 2
Graphing Equations
For the second asignment you will write a program that will read in two quadratic equations from the user. It will then give the user the option to evaluate the equations at a given value for x or graph the equations. The user will also be able to change the equations without restarting the program. Quadratic equations are of the form A*x^2 + B*x + C. The user will simply enter the values for the coefficients A, B, C. The output from your program should match that from the example runs.
Here's an example run.
and another.
and another.
and another.
Lists
You will use a data type called a list to store the coefficients for each equation. We will cover these in class well before the assignment is due, but here is what you'll need to konw to do the assignment.
A list is like a string, but instead of storing a character in each index, you can store anything, in this case we will be storing floats. For this program you will use lists containing three numbers. To create a list and store it in the variable eq1, use the following code. eq1=[0,0,0] . Now eq1 is storing three values, all 0. To change a value you use a statement like eq1[0] = 3 . This will set the first value to 3. To access a value you use the list like a string. eq1[0] , eq1[1] , and eq1[2] retrieve the first, second, and third values respectively. Just like other data types, lists can be used as arguments for a function or the return value from a function. Here are a few lines of code that use a list.
eq1 = [0,0,0]     #create a list with 3 values
eq1[0] = 1        #set the first value to 1
eq1[1] = 2        #the second to 2
eq1[2] = 3        #the third to 3
print eq1[0],eq1[1],eq1[2]      #print the 3 values
User Input
You must make sure that all input from the user is numeric. If the user doesn't enter a number, print the message "Please enter a number." and prompt them again for the input. If they enter an invalid number at the menu print "Please make a valid selection." and prompt them with the menu again.
----------------------------------
Select an option:
Please enter a number
Select an option: five
Please enter a number
Select an option: 11
Please make a valid selection.
Equation-1: 1.0*x^2 + 1.0*x + 1.0
Equation-2: 1.0*x^2 + 1.0*x + 1.0
1. Re-enter Equation-1 Coefficients
2. Re-enter Equation-2 Coefficients
Functions
Using functions is going to be an important part of this program. Below are definitions for some functions that you need to write and use in your program. If you don't follow the definitions below, you will lose points even if your program works correctly.
readEquation()
readEquation will take no arguments as input and will read in the coefficients for an equation from the user. It will then return a list containing the coefficients for the equation.
>>> readEquation()
A*x^2 + B*x + C
A=1
B=2
C=3
[1.0, 2.0, 3.0]
Here is a start for your function definition.
def readEquation():
    """ Enter a docstring here """
    eq = [0,0,0]
    #Add code to read in the values for eq here
    return eq
equationText(coefs)
Write a function named equationText that takes one argument as input. This argument will be a list containing the coefficients for an equation. It will return a string containing the equation fully written out.
>>> equationText([.5, 2.0, 0.0])
'0.5*x^2 + 2.0*x + 0.0'
>>> equationText([-1.0, -2.0, -3.0])
'-1.0*x^2 - 2.0*x - 3.0'
Notice that if B or C is negative, the plus changes to a minus. You should not see something like '-1.0*x^2 + -2.0*x + -3.0'.
evaluateEquation(coefs, x)
The function named evaluateEquation will take two arguments, the first is the list of coefficients for an equation and the second is a value for x. The function will evaluate the equation at x and return the value.
>>> evaluateEquation([1,1,1], 2)
7
>>> evaluateEquation([0,-5,2], -2)
12
printGraph(coefs1,coefs2)
printGraph will take two arguments, lists containing the coefficients for two equations. It will print out a graph of the two equations. You will only use normal text to create the graph. The characters you will use are spaces " ", pipes "|", dashes "-", the digits 1 and 2, and the @ symbol.
>>> printGraph([0,1,-2], [1,0,-4])
10                          |
9                           |
8                    2      |      2            1
7                           |                 1
6                           |               1
5                     2     |     2       1
4                           |           1
3                           |         1
2                      2    |    2  1
1                           |     1
0       ----------------2---|---@----------------
-1                          | 1
-2                       2  1  2
-3                        @ | 2
-4                      1  222
-5                    1     |
-6                  1       |
-7                1         |
-8              1           |
-9            1             |
-10         1               |
           -8  -6  -4  -2   0   2   4   6   8
The graph has 21 rows not including the bottom row of x values, "-8 -6...". Each row corresponds to an integer value of y from -10 to 10. The graph has 41 columns for the x values from -10 to 10 stepping by .5. So there is a column for -10, -9.5, -9.... ,9.5, 10. The vertical axis where x=0 is made up of pipes, "|"s. The horizontal axis where y=0 is made up of dashes, "-"s. If one of the equations goes through a point on the graph, we mark it with a "1", a "2", or a "@" for both.
Use the following method to decide where to plot the equations. If for a given value x, an equation evaluates to a y value within .5 of the point on the graph, mark the equation as going through that point.
Let's look at a specific point on our graph above, (0,-2). Equation-1 is 0*x^2 + 1*x - 2. Evaluating the equation at x=0, we get the value -2. We need to mark equation-1 as going through this point. We would still mark it if the equations value was greater than -2.5 and less than -1.5. Equation-2 evaluates to -4 when x=0, so we will not mark equation-2 at (0,-2).
One more example. Let's think about the equation 1.0*x^2 + 0.0*x + .25 at x = 2. The equation evaluates to 4.25 so we will add a mark for it at (2,4.0) since it is wthin .5 of that point on the graph.
Along with these functions, you must define and use 2 other functions in your program. You will lose points if the functions aren't useful.
Notes
Do not try to write the entire program at once. Write a function at a time and once you have each function working, move on. I'd reccomend writing the functions in the order I have them written above.
Coding style will be an important part of the grade. Functions should have docstrings, there should be a helpful comments when it seems appropriate (NOT everywhere), use good variable names, use proper spacing, and any other style issues mentioned in class.
Chris Schmidt, last updated June 12th, 2007 |