Courses‎ > ‎Introduction to Computer Science 1‎ > ‎Konstantinovich‎ > ‎ML1 9/2012‎ > ‎labs‎ > ‎

Lab 02 - Conditionals

posted Sep 27, 2012, 5:53 AM by Samuel Konstantinovich   [ updated Dec 21, 2012, 5:23 AM ]
You must come up with your own test cases for each problem. 
You must test your solutions.
If you do not complete this in class, finish at home.
You have a test tomorrow.

Cond statements are great when we want more than two possibilities. You can have any number of different results and only 1 will happen. This is better than nested if statements as it looks much cleaner.

Example of a cond statement:
(cond      ;do the 1st statement with a true boolean, or none if they are all false. 
  (b1 s1)  ;boolean and statement
  (b2 s2)  ;boolean and statement
   ...
)

;example 1, three possibilities no chance for them all to be false:
(cond
  ( (< x 1) (+ x 1))  ;notice there are parenthesis around the whole line
  ( (= x 1) (+ x 2)) 
  ( (> x 1) (+ x 3))
)

;example 2, with a default case:
(cond
  ( (< x 10) (...)) 
  ( (< x 20) (...));What if the number is 20 or more???
  ( else   (...) )  ;this will catch any leftover values
)

;example 3: 
(cond
  ( (= x 1) "Hello")
  ( (= x 2) 5)
  ( (= x 3) 3.141592353)
  ( (= x 4) #t )
  ( #t 1)  ; else is the same as #t for a default case
)

LAB Directions:

1. Write a function weeklyPayCalc that calculates your weekly pay based on two parameters hours and wage. This function should account for overtime after 40 hours. Overtime means that any hours past 40 get 1.5x the pay.

(weeklyPayCalc hours wage)
examples:
(weeklyPayCalc 40 10) --> 400 (40 regular hours)
(weeklyPayCalc 42 10) --> 430 (40 regular hours + 2 overtime hours)

2. Write a function that determines how many roots a quadratic equation has as determined by the discriminant. Remember the discriminant is b*b-4*a*c.

Your function should return according to this table:
Discriminant    Roots
Negative           "No Real Roots"
0                       "One Root"
Positive            "Two Roots"

(rootsOfQuadratic a b c)
(rootsOfQuadratic 1 6 9) --> "One Root"
(rootsOfQuadratic 1 0 1) --> "No Real Roots"
(rootsOfQuadratic 1 0 -1) --> "Two Roots"

3. Improve your roots function as follows:
When a discriminant is positive, it can be 2 rational or 2 irrational roots. When the discriminant is a perfect square, the roots are rational but when it is not a perfect square the roots are irrational.

(rootsOfQuadratic2 1 0 1) --> "No Real Roots"
(rootsOfQuadratic2 1 0 -1) --> "Two Rational Roots"
(rootsOfQuadratic2 1 3 1) --> "Two Irrational Roots"

Comments