CSC151.02, Class 10: Conditionals
Admin:
* Did anyone go to either of the Wednesday talks? Email me.
* No reading this weekend (amazingly enough). Feel free to think about problems, work on the homework, and reread the conditionals reading.
* I've posted a formal description of the writeup assignment.
Overview:
* A problem: Assigning letter grades.
* Something that might help: Scheme's conditionals.
* Solving the problem with conditionals.
* Solving the problem without conditionals.
Questions about procedures?
* How do you get both roots? (See roots.scm for code.)
; Problem: There are two roots of the quadratic equation (or at least
; there usually are. How do I get them both?
; (1) Does Scheme provide a way to represent more than one value?
; Yes, put them in a list
(define roots
(lambda (a b c)
(list
(/ (+ (- b) (sqrt (- (* b b) (* 4 a c))))
(* 2 a))
(/ (- (- b) (sqrt (- (* b b) (* 4 a c))))
(* 2 a)))))
----------------------------------------
A problem for the class to solve (not in Scheme, yet)
* (+ (* .35 homework-grade) (* .25 exam-grade) (* .40 convo-grade))a
* Given a numeric grade (call it numgrade), figure out a corresponding letter grade
"round up" using (ceiling)
if numgrade is >= 90 then give the person an A
if numgrade is <= 89 and >= 80.0 then give the person a B
if numgrade is <= 79 and >= 70 then give the person a C
if numgrade is <= 69 and >= 60 then give the person a D
if numgrade is <= 59 then give the person an F
Problems:
* Might assign multiple grades. Fixed by using ranges.
Right now, if someone is smart enough to get over 100 (yes, it has happened), they get no grade. Fixed by eliminating cap.
* No F's. Fixed by giving 59 or below an F.
* What about 79.5? What letter grade do we give someone with that grade?
* What range does numgrade have? It's at least 0 and generally in the range 70-90.
Observation: In order to describe this to someone, we've used the word "if". If is a word we use to tell someone to make choices. We'll need to tell the computer to make choices.
In Scheme, you tell the computer to make choices with "if".
(if TEST-EXPRESSION TRUE-PART FALSE-PART)
Meaning of this expression
1. Evaluate TEST-EXPRESSION
2. If the value is false, evaluate FALSE-PART and use its value.
3. If the value of TEST-EXPRESSION is anything other than false, it uses the value of TRUE-PART
Many tests will be comparisons, e.g.,
(>= numgrade 90)
">=" is a single operator. We'll learn others. It goes first.
Using only the Scheme you knew before today, write a procedure that assigns a letter grade using the following scale:
4: A
3: B
2: C
1: D
0: F
Q: What do I know about the input?
A: A number between 0 and 4, inclusive.
Q: Is it an integer?
A: Sure, if that makes your life easier.
Q: What should the outcome be?
A: The procedure should return a symbol that corresponds to the appropriate letter grade as specified in the table above.
Q: Know what the computer knows how to do. Decide which things might apply.
A: define
A: List operations: list, cons, car, cdr, append, length, cddr, cadr, reverse, list-ref
A: Numeric operations: +, -, *, /, modulo, max, min, abs, gcd, lcm, complex?, exact?, exact->inexact, inexact?, integer?, number?, rational?, and real?.
Idea: Use list-ref. Given a list and an integer, extracts the element at the appropriate position.
Idea: (define grades ('f 'd 'c 'b 'a))
Observation: (list-ref grades 0) -> f
(list-ref grades 1) -> d
(list-ref grades 2) -> c
Moral: There are often different ways to solve a problem.
Submoral: It's okay if you have a different solution than someone else.
Moral: When you're stuck, think about what you know already and how it might help.
Have a great weekend!
----------------------------------------
(define assign-grade
(lambda (numgrade)
; Is numgrade at least 90, inclusive
(if (> numgrade 89) 'A 'B)))