CPSC 122 Study Guide: Examination 1 Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays) 1. What is the output? int x, y; x = y = 0; while(x < 5) y += x; ++x; cout << x << << y << endl ; 2. Write the following function: pre: limit is an integer >= 2 post: returns the sum of the integers in the closed interveral [1..limit] using a for loop int add(int limit) 3. Do problem 2 using a while loop 4. What is the output?... cout << whatisit(9, 3, 17) << endl;... int whatisit(int x, int y, int z) if (x > y && x > z) return x; if (y > x && y > z) return y; if (z > x && z > y) return z; return 0;
5. Rewrite problem 5 without using a nested if 6. Rewrite problem 5 without using && and without using <= 7. Suppose we have this line in a program: typedef float mytype; Write a short program that: declares and initializes two variables of type mytype. Inputs values for each from the keyboard Sums the two variables and outputs the sum 8. What is the output: int a = 2; int d = 0; float b = 1.0; float c = 0.0; c = b/a; cout << c << endl; cout << static_cast<int>(b/a) << endl; d = b/a; cout << d << endl; cout << showpoint; c = static_cast<int>(b/a); cout << c << endl; 9. Write the following function: //pre: x, y, z are integers //post: returns the smallest of the arguments. If two or three of the arguments are tied for the smallest, returns one of them. int findsmallest(int x, int y, int z) 10. Write the following function: pre: base and exp are integers with base >= 1 and exp >= 0 post: returns base to the exp power without using a built-in function int myexp(int base, int exp) 11. If you used a for loop in problem 10, rewrite it with a while loop and vice-versa.
12. Write the following function using a loop construction of your choice pre: value is an integer >= 0 post: returns the factorial of num. Example: fact(0) = 1, fact(3) = 3 * 2 * 1 int fact(int num) 13. If you used a for loop in problem 12, rewrite it with a while loop and vice-versa 14. Write the following function: pre: num is a positive integer >=2 //post: returns true if num is prime, false otherwise int isprime(int num) 15. What is the output?. int sum = 0; int value = 1; for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) sum += value++; cout << sum << << value; 16. What is the output? int x = 7; int y = 0; y = 3 * ++x cout << x << << y << endl; x = 7; y = 0; y = 3 * x++; cout << x << << y << endl;
17. Write the following function pre: num is a positive integer post: returns true if num is odd, false otherwise bool isodd(int num) 18. What is the output? int a = 0; int b = 1; int c = 2; int result = 0; if (result == a result == b result == c) ++result; cout << result << endl; 19. What is the output? int a = 0; int b = 1; int c = 2; int result = 0; if (result == a && result == b && result == c) ++result; cout << result << endl;
20. What is the output? This is supposed to print 1 if result is either a or b or c. What does it print? What s wrong? Fix it. int a = 1; int b = 1; int c = 2; int result = 0; if ((result = a) (result = b) (result = c)) cout << true << endl; cout << false << endl; 21. Write the following function pre: num is an integer > 0 post: displays all integers in the closed interval [0..num] but in reverse void disprev(int num) 22. Suppose you are using a 32 bit machine. int x; //how many bytes are allocated for x char y; //how many bytes are allocated for y 23. Write the following function //pre: stuff is an integer array of size howmany. It is filled with integers //post: returns the sum of the integers in the array int add(int stuff[], int howmany) 24. What is the complexity to your solution to problem 23 expressed in big O notation? 25. What is the complexity to problem 19 expressed in big O notation 26. Write the following function: /* pre: nums is an array containing integers, howmany is the size of the array post: returns the product of the integers in the array */ int prod(int nums[], int howmany) 26. If you solved 1 with a for loop, solve it with a while loop and vice-versa. 27. Write a program that stores the first twenty even integers in an array and then displays them in reverse.
29. Write the following function /* Pre: stuff is an array of strings of size num. target is a string. Post: if target is in the array, its position is returned, returns -1 is returned */ int linsrch(string stuff[], int num, string target) 30. What is the big O complexity of linsrch in problem 29? 31. Write the following function. /* Pre: stuff is an array of char of size num. idx is an index in the range 0 <= idx <= num 1 Post: returns the character stored in stuff at position idx */ char getstuff(char stuff[], int idx) 32. What is the big O complexity of getstuff from problem 31 33. Example 18 on the class web site is selection sort. What is the big O complexity? 34. What is the output? void dostuff(int,int); int x = 1; int y = 2; dostuff(x,y): cout << x << << y << endl; void dostuff(int x, int y) int z; z = x x = y y = z
35. What is the output? void dostuff(int[], int, int); int x = 1; int y = 2; int stuff[5]; for (int i = 0; i < 5; i++) stuff[i] = i; dostuff(stuff, x, y): cout << stuff[x] << << stuff[y] << endl; void dostuff(int stuff[], int x, int y) int z = stuff[x]; stuff[x] = stuff[y]; stuff[[y] = z;