CSC 15 Lab : Loops, Arrays and Functions Due BEFORE next week's lab This week's lab will ask you to write a series of programs using loops and arrays, and will also introduce you to writing functions. The first part is to be done directly in interactive Python. The second part should be written as a separate program. Only the second part should be submitted. REVIEW OF FUNCTIONS. Be sure to read the notes on writing functions. Here's a quick review and example of functions. def area(r): # compute area of a circle with radius r pi = 3.1415927 # or, from math import pi at top ax = pi*r*r # area value stored in ax return ax # value returned #end of area definition. # examples of calling the function: print(area(10)) # should print 314.15927, area of radius 10 circle. x = area(100) # should assign 31415.927 to variable x Things to note: 1. The function has an "argument" or "parameter" called r. A function can take multiple parameters. 2. The function returns a value before ending. 3. Defining the function is not the same as calling the function. 4. When a function is called, the parameter variable (in this case r) is first assigne the value that was "passed into" the function. So calling area(10), will first do the assignment r=10. 5. The function call is used in the same way as its return value is used. In this, case, the function call is used as a floating point numerical value. 6. The argument variable (r) and any variables you assign to inside the function, are local to the function. This means that, for the area functions above, the variables r, pi and ax are not available (unless they're redefined) outside the indentation scope of the function. PART I: Preliminary Drills on Loops and Arrays: The following problems should be completed inside the Python interactive interpreter (it will not be collected, and are only provided for your benefit). a. Write a while loop that prints every even number from 0 to 100, inclusive. I.E. the first value should be 0, then 2, then 4, etc... and the last number should be 100. Think carefully about how the loop should start and when it should end. b. Write a while loop that prints every odd number between 0 and 100 BACKWARDS, i.e., 99, 97 ... c. Write a while loop that constructs an array of all odd numbers between 0 and 100 backwards. That is, an array with [99, 97, 95, ... 1] Hint: start with an empty array A = [] then use A.append(...) in your loop. d. Rewrite the code for part b as a function: def printodds(): ... So that each time it's called it will print every odd number between 0 and 100 backwards. e. Rewrite the function in part d. with def printoddsto(n): ... So that each time it's called it will print all odd numbers between 0 and n backwards. printoddds(10) # should print 9 7 5 3 1 f. Rewrite the function in part e so that it also returns the sum of all the numbers that were printed. x = printoddsum(10) # should print 9 7 5 3 1, then return 25 and assign it to x You will need a return statement at the end. g. Write a function that take as a parameter n, and returns an array of all odd numbers from 1 to n. If n is 0 or negative, then the function should return [] That, is complete the following: def oddsUpTo(n): ... So that, for example, calling print(oddsUpTo(8)) will print [1,3,5,7] (i.e., the function should return [1,3,5,7]) The following is an example of a function that returns an array (of binary factors of n) def binfactors(n): F = [] # array to be returned power = 0 while n>0: if (n%2==1): F.append(2**power) #add to end of array n = n/2 power += 1 return F # return array that was constructed. #binfactors print binfactors(147) # prints [1, 2, 16, 128] Even if you don't quite understand the algorithm, you should still observe how the array F was created (and returned) by the function. REMEMBER that you functions must work for any argument value, and do not use input/print statements inside the function. All I/O must be done outside the functions. ----------------------------------------- PART II: Write the following functions in Python. Everything should be in the same file. Test each function with multiple calls. 0. Write a function that reverses an array. For example, reverse([2,4,5,7]) should return an array [7,5,4,2]. Hint: do not try to change the existing array. Contruct a new array starting from []. 1. Write a function largesti that returns the INDEX of the largest element in an array (which is passed in as a parameter). If there are multiple occurrences of the largest element, the function should return -1. For example, largesti([2,4,1,7,3]) should return 3, since the largest element, 7, is at position 3. largest([4,1,6,2,6]) should return -1 because there's a tie. Devise several function calls to make sure the code is working correctly. e.g. largesti([2,4,1,7,3]) As an example of a similar function, the following returns the index of x in A, or -1 if x is not in A: def indexof(x,A): answer = -1; // -1 means x not found in A i = 0 # indexes A while answer==-1 and i