notes/education/software development/ECE1400/Chapter 5 Exercises.md

103 lines
2.7 KiB
Markdown
Raw Normal View History

2024-09-18 16:52:12 +00:00
> 2. The following program fragments illustrate the logical operators. Show the output produced by each, assuming that `i`, `j`, and `k` are `int` variables.
a. `i = 10; j = 5;`
2024-09-18 17:12:12 +00:00
```c
2024-09-18 16:57:12 +00:00
printf("%d", !i < j);
2024-09-18 17:12:12 +00:00
// Expected output: `1`, because `!i` evaluates to 0, and 0 is less than 5, so that expression evaluates to true, or 1.
```
2024-09-18 17:17:12 +00:00
b. `i = 2; j = 1;`
```c
printf("%d", !!i + !j);
// Expected output: `1`, because !!2 evaluates to 1, and !j evaluates to 0
```
c. `i = 5; j = 0; k = -5;`
```c
printf("%d", i && j || k);
2024-09-18 17:22:12 +00:00
// Expected output: `1`, because i && j should evaluate to 0, but `0 || 1` should evalulate to true.
2024-09-18 17:17:12 +00:00
```
2024-09-18 17:22:12 +00:00
d. `i = 1; j = 2; k = 3;`
```c
printf("%d", i < j || k);
2024-09-18 17:27:12 +00:00
// Expected output: `1`
```
> 4. Write a single expression whose value is either `-1`, `0`, or `1` depending on whether `i` is less than, equal to, or greater than `j`, respectively.
```c
2024-09-18 17:32:12 +00:00
/*
If i < j, the output should be -1.
If i == j, the output should be zero
If i > j, the output should be 1.
*/
(i > j) - (i < j)
```
2024-09-18 17:37:12 +00:00
> 6. Is the following `if` statement legal?
```c
2024-09-18 17:42:12 +00:00
if (n == 1-10)
2024-09-18 17:37:12 +00:00
printf("n is between 1 and 10\n");
```
2024-09-18 17:42:12 +00:00
Yes the statement is *legal*, but it does not produce the intended effect. It would not produce an output when `n = 5`, because `1-10` evaluates to `-9`, and `-9 != 5`.
> 10. What output does the following program fragment produce? (Assume that `i` is an integer variable.)
```c
int i = 1;
switch (i % 3) {
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
```
The program would print `onetwo` because each case is missing a `break` statement.
2024-09-18 17:47:12 +00:00
> 11. The following table shows the telephone area codes in the state of Georgia along with the largest city in each area:
| Area code | Major city |
| --------- | ---------- |
| 229 | Albany |
| 404 | Atlanta |
| 470 | Atlanta |
| 478 | Macon |
| 678 | Atlanta |
| 706 | Columbus |
| 762 | Columbus |
| 770 | Atlanta |
| 912 | Savannah |
> Write a switch statement whose controlling expression is the variable `area_code`. If the value of `area_code` is not in the table, the `switch` statement will print the corresponding city name. Otherwise, the `switch` statement will display the message `"Area code not recognized."` Use the techniques discussed in section 5.3 to make the `switch` as simple as possible.
```c
2024-09-18 17:52:12 +00:00
int area_code;
2024-09-18 17:47:12 +00:00
switch (area_code) {
case 404:
case 470:
case 678:
case 770:
printf("Atlanta");
break;
2024-09-18 17:52:12 +00:00
case 706:
case 762:
printf("Columbus");
break;
case 229:
printf("Albany");
break;
case 478:
printf("Macon");
break;
case 912:
printf("Savannah");
break;
default:
printf("Area code not recognized.");
break;
2024-09-18 17:47:12 +00:00
}
```