2015-10-02 HW08

posted Oct 1, 2015, 6:11 AM by Samuel Konstantinovich   [ updated Oct 5, 2015, 6:24 AM ]
Exam Next Friday October 9th. 
Do not miss exams there are no make up exams unless you have a doctor's note or other similar level emergency.
-I will drop your lowest exam ONLY if you take every exam. 
-I will drop your lowest quiz ONLY if you take every quiz.

HW08 Due Thursday Morning on the HW server. (Oct 8th, 6am) [see bottom of this post for the assignment]
Notes/Classwork:

Goal: Using Conditional Statements!!!!11one!1eleven

Do now:
Write the following on paper (You don't have to copy the questions, but should use complete sentences.)
1. How do and and or work in boolean logic?
2. Write out the truth tables for and and or
3. Copy the function below, and the function calls, then evaluate the results:
(define what (λ (a b)
              (and
                 (or a b)
                 (or
                     (not (and a b))
                     (and (not a) b)))))
;evaluate
(what #t #t)
(what #t #f)
(what #f #t)
(what #f #f)

Conditionals:

Unlike logical if-then statements, in computer science if statements do not have a truth value. Instead an if-then statement allows the computer to choose between two options.

(if  booleanExpression result1 result2)
the if statement evaluates to:
result1 when booleanExpression is true
result2 when booleanExpression is false

(if (< 3 10) 10 20) 
(if #true 10 20)
10
(if (< 2 0) 10 20) 
(if #false 10 20)
20

Lets try this in a function:

(define bar (λ (n)
   (if (< n 10)       
            31
            55) ) )
(bar 3)
(bar 10)
(bar 11)


We can use this to make functions like
(myAbs n)   evaluates to the absolute value of n.
(myMin a b) evaluates to the smaller of the values a and b.

Try writing these functions now! 


Try this:
(define foo (λ (n)
   (if (= (remainder n 2) 0)       
            (bar n)
            (* 3 n) ) )
evaluate:
(foo 2)
(foo 3)








HW08 Due Thursday Morning on the HW server. (Oct 8th, 6am)
Problem Difficulty:
1 and 4 you have seen all the tools for already, 
2 and 3 you could do if you are very insightful, but we will learn more Monday
5 we will do more practice, but you CAN do it already with and/or/not )


0. Don't forget your header. 
1. Write a function (makeEven x) , assume x is an integer.
When x is even it evaluates to x
When x is odd, it evaluates to x+1
(makeEven -1) -> 0
(makeEven 4) -> 4
(makeEven 3) -> 4


2. Write a function (checkSign x) , assume x is a real number.
When x is positive return 1 
When x is negative return 2
When x is neutral return 3
examples:
(checkSign 4) -> 1
(checkSign -3) -> 2
(checkSign 0) -> 3
 
3. Write a function (howManyRoots a b c) that tells us how many roots a quadratic in the form ax^2 + bx + c = 0. The possible values are 0 1 or 2.

The discriminant can help us determine the nature of the roots of a quadratic as follows:
discriminant is negative : there are no real solutions
discriminant is zero : there is exactly one real solution
discriminant is positive : there are two real solutions
examples:
(howManyRoots 2 6 3) returns 2
(howManyRoots 1 -8 16) returns 1
(howManyRoots 4 5 3) returns 0

4. Create a (myQuotient a b) function that does the same thing as  (quotient a b). 
-You may use any tools EXCEPT for the quotient function. 
-Assume a and b are integers. This means you don't have to test it on decimals.
(myQuotient 10 3 ) -> 3
(myQuotient 10 4 ) -> 2
(myQuotient 9 3 ) -> 3

5a. (Easier than 5b)
Write a function (isPyTriple a b c) that returns #true when x y and z are valid side lengths of a right triangle, #false otherwise.  Note: It should work no matter which of the sides is the longest. (Do not assume c is the longest)

5b. Write a function (leapyear year) that returns #true when a year is a leap year, #false when it is not. 
NOTE: This problem does not require an if statement. 
You must figure it out with just boolean expressions, and , or , not , and comparisons.

You may google leap years for clarification.
The rules of leap years are:   
-Multiples of 400 are leap years.
-Multiples of 100 that are not multiples of 400 are not leap years
-Multiples of 4 that are not multiples of 100 are leap years
-All other years are are not.
OR: (rephrase)
Years that are multiples of 4 are leap years, except that every year that is a multiple of 100 is not a leap year , unless it is also a multiple of 400 then it is.
outputs:
(leapyear 2000) #t  ;because 400
(leapyear 1900) #f  ;because 100 but not 400
(leapyear 1904) #t  ;because 4
(leapyear 2103) #f  ;because not even close...
(leapyear 2104) #t  ;because 4
Comments