CS 135 Lab Assignments Week 1 Professor: Matt B. Pedersen This handout is the assignment that you must finish for the lab portion of the course in week 1. You must finish the assignments yourself; if you need any help ask the TA or the professor. You may not work together with other students in solving the assignments. The weekly assignments must be handed in on a piece of paper, typed neatly (spell checked etc.) and look nice. The assignments are due in the the professor s mailbox labeled Matt Pedersen in TBE-A211 the following Wednesday before the computer science office closes at 5pm. All lab assignments must be handed in if you wish to attend the final exam. Late assignments are not accepted for any reason except sickness or family emergencies, and the former requires a doctors notice. 1 Question 1 Consider a simple expression in everyday arithmetic like 1+2. Using the rules of arithmetic we can calculate its value, which is 3. Let us call expressions that consist of only one operator and two operands for simple. What about a more complicated expressions like 4+2+1 (i.e., one that is not simple)? What is its value? It is well known that parentheses can be placed in such a way that, if evaluated according to the rules of arithmetic, all expressions are simple when they are evaluated. For example, 4+2+1 can be written as 4+(2+1) or if you like (4+2)+1. Either way if we evaluate the expression we get: 4+(2+1) = 4+3 = 7 or (4+2)+1 = 6+1 = 7. When the operand is + the placement of the parentheses does not matter, but what if the expression was 4-2-1? (4-2)-1 is not the same as 4-(2-1). The former evaluates to (4-2)-1 = 2-1 = 1 and the latter to 4-(2-1) = 4-1 = 3. The same problem arises when we use division (/): the placement of the parentheses matter. Which one is the correct one? (4-2)-1 is. Therefore, the correct answer is 1. The arithmetic we use is left associative if we do not use parentheses. Be careful though, because some operators like * and / take 1
precedence over others like + and -. For example, 4-8/2 is not the same as (4-8)/2, because / has higher precedence than -. The correct expression with parentheses is 4-(8/2). Using parentheses, convert the following non-simple expressions into expressions using parentheses such that when we evaluate it, we only evaluate simple expressions. The parentheses should be placed such that the value of the converted expressions is the same as the value of the expressions without the parentheses. For example, 4/2-3 has the value -1, so the correct way to convert this expressions is (4/2)-3. Also state what the value of the expression is. 4-5-7+4-1*8/4 1+2+3-4-5-6 16/4/2+1 2 Question 2 Expressions can also contain variables. You know this from math when we have expressions like x + 2. The value of x + 2 depends on the value of x. If x has the value 5 then x + 2 is equal to 5+2=7. If x has the value 4 and y has the value 5 what is the value of the expressions x + 2 y? Using parentheses, transform x + 2 y like we did in question 1. What should the value of x be for the expressions x/y 2 to be equal to 3 if y is equal to 5? 2
Find at least 2 sets of values for x and y such that the expression x y/2 is equal to 0. 3 Question 3 In math you will have seen expressions like y = 2 x 1 We can determine the value of y if we know the value of x (and if we draw a graph, then we get a straight line with a slope of 2 crossing the y-axis at -1. y gets a new value every time we evaluate the expression on the right (2 x 1). Consider the following more complex equation: y = x 2 + 2 x 3 and fill in the correct values for y in the table below with the given values for x. 4 Question 4 x -5-3 0 2 4 As mentioned earlier, different operators have different precedences, for example in the non-parenthesized expression x y 2 the multiplication has higher precedence than subtraction. Some operators like + and - have the same precedence. y b + b 2 4 a c 2 a in this rather complex expression (which you will probably recognize as the positive solution of the quadratic equation) we have used both + and - as well as * and /; furthermore, we have used a unary minus (in front of the b; this could have been written as (0-b)) as well as exponentiation (b 2, which 3
of course could have been written as (b b)). Write out the expression using parentheses (and only simple expressions) such that when evaluated it produces the same result as in the expression above. Also list the operators used in order of precedence from highest to lowest. 5 Question 5 I am going to invent a new operator which we will call gets the value of, and its use looks like this: x 5 and it reads x gets the value of 5. After evaluating and expressions using this operator, the value of the expression on the right will be placed in the variable on the left. Naturally the following is illegal: and so is 5 6 7 x + 6 as you cannot change the value of a number! The following is legal though: x 5 1 y x + 2 After the first expression has been evaluated, x has the value of 5-1 (i.e., 4), and when we subsequently evaluate x + 2 we get 4-2 which is 2 and that becomes the value of y. Consider this sequence of expressions: x 2 y x 5 x y 2 What is the value of x and the value of y once we have evaluated all three expressions? After the first expressions clearly x has the value 2! At this point we cannot say anything about the value of y. To evaluate the second expression we need to evaluate the expression x 5, which is 10 (as x was 4
2), and we now know that y is 10 and x has not changed (we only used the value of x on the right hand side of the second expression, not changed it!), so it remains 2. But what happens after the third expression? The right hand side of the this expression is y 2 which is 10-2 which is 8, so x get the value of 8 (changing it from its old value of 2). So in the end, y is 10 and x is 8. What is the value of x, y and z after the following expressions have been evaluated (always evaluate expressions in order starting from the top and then move down, one at a time remembering the values of variables already given values): x 4 y 5 x 2 x y 2 z x + y When evaluating expressions with the operator it is important to think of the right hand side as something that evaluates to a value (by using arithmetic and remembering what values were stored in any variables used in the expression) and the left hand side (which is always a variable) as a container that can hold a single value, which can change only when using the operator. The way an expression with the is evaluated is always Compute the value of the right hand side using arithmetic and by using the values of any variables, and once you have a single value, that value now gets given to the variable on the left. With this in mind we can write something like this: x 1 x x + 1 What is the value of x after these two expressions? 5