EECS 183, Week 5 General 0. At which location do you have to take the exam? 1. Source code vs. object code? 2. What s a library? Variables 3. Name main data types in C++. 4. Is string a native data type in C++? What do you need to do atop the.cpp file to use strings? 5. How are these two lines of code different? int x = 5; x = 6; 6. Any problems with this line of code? int somenumber = 1000 * 1000 * 1000 * 1000; 7. What are the differences between variables and constants? What are naming conventions for each? 8. Which of the following are valid variable declarations? int 1f; int if; int if1; int f+1; 9. What prints? int x = 18 / 5 5 / 2; cout << x << endl; double y = 18 / 5 5 / 2; cout << y << endl; 10. Given two ints, a and b, how to divide a by b and get a double back? 11. What s the best way to compare two variables of type double for equality? I/O 12. cin >> str; vs. getline(cin, str);? When to use each? 13. What do \', \", \n, \t and \\ do? 14. Consider the following code: int x = 2, y = 4; char ch; cin >> x >> ch >> y; cout << x * y << endl; 15. What prints if the user types the following? 2.5 * 2.0<Enter> Maxim Aleksa (maximal@umich.edu) 0<7 http://umich.edu/~maximal/eecs183
EECS 183, Week 5 Functions 16. In the following code, mark declaration, prototype, function call, definition, implementation, signature, header, argument/parameter list, return type, return statement. #include <iostream> using namespace std; /** * Requires: Nothing. * Modifies: Nothing. * Effects: Returns sum of a and b. */ int add(int a, int b); int sum = add(1, 2); cout << sum << endl; int add(int a, int b) { int sum = a + b; return sum; 17. What is the difference between prototype and implementation? 18. Which of the following are valid function prototypes? char void(double x); int maximum(x, y); int calc(void x); void squareroot(double x); 19. If a variable fillcolor is accessible only within function drawrect, then fillcolor is known as... 20. What is RME? 21. How is RME interpreted by the C++ compiler? 22. Implement max, a function that returns the maximum of the three arguments passed in. int max(int a, int b, int c); Maxim Aleksa (maximal@umich.edu) 1<7 http://umich.edu/~maximal/eecs183
Conditions 23. (T/F) if is a function 24. How are these two snippets of pseudocode different? if conditiona: do something if conditionb: do something else if conditionc: do this other thing if conditiona: do something else if conditionb: do something else else if conditionc: do this other thing 25. For each expression in the left column, find its complement in the right column and write the letter of the complement on the blank. price < 50 20 <= price && price < 50 20 > price price > 50 price < 50 && instock!instock price < 50 26. (T/F) Order of conditions in a sequence of if / else if / else statements matters. 27. (T/F) It is possible to write every sequence of if / else if / else statement as a switch statement. 28. (T/F) It is possible to write every switch statement as a sequence of if / else if / else statements. 29. What does this code print? Does the program crash? if ((3 == 4) && (3 / 0 == 4)) { cout << "true" << endl; else { cout << "false" << endl; 30. What prints? int x = 0; if (x = 0) { cout << "zero" << endl; else { cout << "non-zero" << endl; 31. Find (and fix!) errors with in the snippet of code below. int lecture = getlecturenumber(); if (lecture == 1 3) { string instructor = "Bill Arthur"; else if (lecture == 2) { string instructor = "Héctor García"; else if (lecture == 3) { string instructor = "Mary Lou Dorf"; else { string instructor = "Staff"; 32. Rewrite this expression without using &&: A. price < 20 price >= 50 B. price >= 50 && instock C. price >= 50 &&!instock D. price >= 50!inStock E. price >= 50 cout << "Your professor is " << instructor << endl; student && age >= 18 F. price >= 20 && price <= 50 Maxim Aleksa (maximal@umich.edu) 2 < 7 http://umich.edu/~maximal/eecs183
Loops 33. For loops vs. while loops? When to use each? 34. (T/F) It is possible to write every for loop as a while loop. 35. (T/F) It is possible to write every while loop as a for loop. 36. Rewrite this for loop as a while loop: for (int i = 10; i > 0; i--) { cout << i << endl; 37. Write a loop that prints a triangle of stars of height 4: * ** *** **** 38. Write a loop that prints an upside-down triangle of stars of height 4: **** *** ** * 39. What does this snippet of code print? int x = 100; while (x >= 0) { x /= 5; cout << x << " "; 40. What does this loop print? for (int i = 0; i >= 3; i++) { cout << 'X'; 41. Implement countones, a function that counts how many numbers are in the integer passed as an argument and returns the count. Here s its declaration: int countones(int n); Maxim Aleksa (maximal@umich.edu) 3 < 7 http://umich.edu/~maximal/eecs183
ASCII Maps characters to their numerical representation. Don t memorize the table. Remember this though: 'A' is 65 'a' is 97 'A' comes before 'a' 'Z' and 'a' and are not next to each other '0' is not 0 42. Suppose you want to determine whether the user typed in a letter or not. What condition goes inside of the if statement? char character; cin >> character; if ( ) { cout << "letter" << endl; else { cout << "not a letter" << endl; ) 43. Write some code uses loops to print a battleship board. 1 2 3 4 5 6 7 8 9 10 A o o o o o o o o o o B o o o o o o o o o o C o o o o o o o o o o D o o o o o o o o o o E o o o o o o o o o o F o o o o o o o o o o G o o o o o o o o o o H o o o o o o o o o o I o o o o o o o o o o J o o o o o o o o o o Strings String is a sequence of characters. Each character has an index. The index of the last character is length of the string 1. 0 K 1 Let s say we have a string name that we read from the user. string name; cin >> name; a 2 t 3 h 4 e 5 r 6 i 7 n 8 e 44. How can you get the length of name? int numberofcharacters = 45. How can we get the first character of name? char firstcharacter = Maxim Aleksa (maximal@umich.edu) 4 < 7 http://umich.edu/~maximal/eecs183
46. Write a complete program (with main and one other function) that asks the user for a string, and then prints that same string with the first character capitalized (if a letter). The program should behave per the sample output below: Please give me a string: hello Hello Pass-by-Value and Pass-by-Reference 47. What is pass-by-value? 48. What is pass-by-reference? 49. What prints? void increment(int x); int x = 1; increment(x); cout << "x is " << x << endl; void increment(int x) { x += 1; 50. What prints? void foo(int& x, int y); int a = 3; int b = 4; foo(b, a); cout << a << " " << b << endl; void foo(int& x, int y) { y += 2; x *= y; Maxim Aleksa (maximal@umich.edu) 5 < 7 http://umich.edu/~maximal/eecs183
51. Fix the swap function. void swap(int a, int b) { int temp = a; a = b; b = temp; EECS 183, Week 5 52. For each main function, pretend to be a C++ compiler and figure out what prints. Draw a memory diagram for each function to keep track of variables. Common functions main function Memory diagrams Output int foo(int x) { cout << x--; return x; int bar(int y) { cout << y; return y + 2; int baz(int &z) { z++; cout << z; return z; z += 2; int a = 2; cout << a; foo(a); cout << a; int b = 3; cout << b; b = bar(b); cout << b; int c = 4; cout << c; c = baz(c); cout << c; 53. Consider the following C++ function: int triple(int& x) { x = x * 3; return x; int d = 2; int e = 3; d = baz(d + e); cout << d; int f = bar(foo(2)); cout << f; int g = foo(baz(4)); cout << g; What does the following code print? int y = 1; cout << triple(y) + triple(y); Maxim Aleksa (maximal@umich.edu) 6 < 7 http://umich.edu/~maximal/eecs183