CSCI 151 Fall 2013 TEST 4 PREP Date: November 22, 2013 NO CELL PHONES, NO BOOKS, ONE PAGE OF NOTES ONLY and COMPUTER TEST 4 consists of Two Parts o Part I - 30 points – 1 short question o Part II - 70 points - 2 program writing question PART I Sample Question 1 a. What is the output of the following program if the input is: 234 453 679 0? Give short explanation. NO TRACING is required. (25 points) b. Explain, what this program is calculating (3 points) c. Determine the type of repetitions, count-controlled or condition-controlled, this program has and explain your answer. (2 points) SIZE = 3 def who(num): a=0 for i in range(SIZE): k = num % 10 a = a+ k num = num / 10 return a def main(): n = int(input(“”)) b=0 c=0 while (n != 0): result = who(n) print(result) b = b + result c=c+1 n = int(input(“”)) d = b/c print(d) main() PART I Sample Question 2: a. What is the output of the following program? Give short explanation. NO TRACING is required. (25 points) b. Explain, what this program is calculating (3 points) c. Determine the type of repetition, counter-controlled or sentinel-controlled, this program has and explain your answer. (3 points) SIZE = 6 def who(num): c=0 d=0 for i in range(SIZE): k = num % 10 if (k % 3 == 0): d=d+k c=c+1 num = num // 10 if(c >0): f=d/c print (c, d, f) else: print(“error”) def main(): who(248155) who(356793) main() Part I Sample Question 3 a. Trace your program and write an output for the following input sequence: -2 3 5 -6 -7 0 b. Explain what this program is calculating. c. Determine the type of repetition, counter-controlled or sentinel-controlled, this program has and explain your answer. num = int(input(“enter first number “)) a= 0 b= 0 while(num!=0): if(num > 0): a = a+1 b = b + num num = int(input(“enter next number “)) if(a > 0): c= b/a print(a,b) else: print(“error”) Part II Sample Questions – we will do some of these questions in the lab on Monday and Wed Question 1 Write a program that reads 10 positive three digit numbers and prints the last digits of all these numbers on one line. You may assume that input consists of 10 positive integers between 100 and 999. YOU MUST USE LOOP FOR. Question 2 Write a function sumDivisor that has one parameter – positive integer. The function finds and prints the sum of all divisors of the parameter (use loop FOR in this function). Write a program that reads a sequence of positive integers. First negative or zero terminates the input. For each number, program uses function sumDivisor to find the sum of the divisors Question 3 Write a program that reads a sequence of positive integers greater than 10. The first negative integer terminates the input. The program finds the average of the last digits of the even numbers and the first digits of the odd numbers. You can assume that the input is valid and not empty – there is at least one positive integer in the input. Use whatever floating point format your language provides by default for the output. For example, if the user enters the positive numbers 23, 455, 668 and 24, the program will compute the average of the first digits of the odd numbers (2 and 4) and the last digits of the even numbers (8 and 4): (2 + 4 + 8 + 4) / 4 = 4.5 Question 4 Write a function that has 3 parameters: deposit, interest and number of years. The function finds and prints the amount in the account after all these years. Write a program that asks 5 users for information about the bank account and for each user calculates the amount of money in the account at the end of the designated period. Question 4 a(Wed group did in the lab last week) Write a function that has 2 parameters, deposit and interest. The function finds and prints the number of years it will take to receive $1,000,000. Test your function on 5 users. Question 5 Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds the number, the sum, and the average of the POSITIVE EVEN numbers. Write everything in main function. No additional function is required. Example: Input: 2 -6 6 7 8 -9 0 Output: there are 3 POSITIVE EVEN numbers, their sum is 16 and their average is 5.333333 Question 5 a(very similar to 5) Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds the number, the sum, and the average of the NEVATIVE ODD numbers. Write everything in main function. No additional function is required. Example: Input: 2 -6 6 -7 8 -9 0 Output: there are 2 NEGATIVE ODD numbers, their sum is -16 and their average is -8.0 Question 6: A positive divisor of some number K, which is different from K, is called a proper divisor of K. For example, 1, 2, and 3 are proper divisors of 6. In number theory, an abundant number is a number that is less than the sum of its proper divisors. For example, 12 is an abundant number since it is less than 1+2+3+4+6 = 16. The number 15 is NOT an abundant number since the sum of the proper divisors is 1+3+5 = 9. The number 6 is also NOT an abundant number since 1+2+3 = 6. Write a program that reads one positive integer N and prints all ODD ABUNDANT numbers between 1 and N. If the number N is ODD and ABUNDANT, the program will print N as well. Example 1: Input 1000 Output: 945 Example 2: Input: 2000 Output: 945 1575 Example 3: Input: 2205 Output: 945 1575 2205 Question 7 (bonus question) A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). If n is not happy, then its sequence does not go to 1. What happens instead is that it ends up in a cycle such as: 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89… For example if n = 44, we get Step 1: 42 + 42= 32 Step 2: 32 + 22= 13 Step 3: 12 + 32= 10 Step 4: 12 + 02 = 1 – terminates the process and the output is: YES, 4 Write a program that reads one integer and outputs the following: if the number is happy, it outputs YES and the number of steps it took to get from the input to 1 (including the step that resulted in 1), otherwise, the program outputs NO and the number of steps it took to get to the beginning of the cycle (including the step that resulted in 4) For example, if n = 43, we get Step 1: 42 + 32 = 25 Step 2: 22 +52 = 29 Step 3: 22 + 92 = 85 Step 4: 82 + 52 = 89 Step 5: 82 + 92 = 145 Step 6: 12 +42 +52 = 42 Step 7: 42 +22 = 20 Step 8: 22 +02 = 4 The output NO, 8