Drills: check yourself
Answer yourself first, only then open the answer.
1. What will this function look like after Ctrl+Alt+L? What will you still have to fix by hand?
int f(int x){int y;if(x>5)y=1;else y=2;return y;}
Answer
int f(int x) {
int y;
if (x > 5)
y = 1;
else
y = 2;
return y;
}
The formatter lays out the lines and spaces, but it doesn't add { } to the if and else branches and doesn't rename f, x, y. That is your job.
2. A breakpoint on sum += grade;. The program stops for the first time. What do you expect in the Variables window if the first grade typed is 7?
int sum = 0;
int grade = read_int("Grade: ");
while (grade != 0) {
sum += grade;
grade = read_int("Grade: ");
}
Answer
grade = 7, sum = 0. The line with the breakpoint hasn't run yet, so sum hasn't grown yet. After F8 it becomes 7.
3. The program stopped on int count = read_int("How many? ");. What is the difference between F7 and F8?
Answer
F8 (Step Over) runs the whole line: the program asks, you type a number in the Console tab, and the debugger stops on the next line. F7 (Step Into) goes inside read_int, into input.c. You come back out with Shift+F8.
4. Which commit message is best?
a) fixed
b) Divide average by number of grades entered
c) asdfgh
d) changes in main.c
Answer
b): it says what changed and why. a) and c) say nothing, and d) says only what Git shows anyway.
5. A breakpoint on sum += i;. How many times does the program stop, what is i each time, and what is sum after the loop?
int sum = 0;
for (int i = 0; i <= 3; i++) {
sum += i;
}
Answer
4 times: i = 0, 1, 2, 3 (<=, so 3 as well). After the loop sum = 0 + 1 + 2 + 3 = 6.