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

69 lines
2.5 KiB
Markdown
Raw Permalink Normal View History

2024-10-09 02:43:52 +00:00
> 1. We discussed using the expression `sizeof(a) / sizeof(a[0]` to calculate the number of elements in an array. The expression `sizeof(a) / sizeof(t)` where `t` is the type of `a`'s elements would also work, but it's considered an inferior technique. Why?
2024-10-09 03:05:01 +00:00
Answer:
Using the type of the array's first element means that if you change the type of an array, it won't break the code that calculates the number of elements.
2024-10-09 02:48:52 +00:00
> 3. Write a declaration of an array named weekend containing seven `bool` values. Include an initialize that makes the first and last values `true`; all other values should be `false`.
2024-10-09 03:20:19 +00:00
Answer:
```c
bool weekend[] = {true, [1 ... 5] = false, true};
```
> 5. Calculators, watches, and other electronic devices often rely on 7 segment displays for numerical output. To form a digit, such devices turn on some of the seven segments while leaving others off.
2024-10-09 02:48:52 +00:00
>
> Here's what the array might look like, with each row representing one digit:
2024-10-09 03:20:19 +00:00
```c
2024-10-09 02:48:52 +00:00
const int segments[10][7] = {{1, 1, 1, 1, 1, 1, 0}, ...};
```
> I've given you the first row of the initializer, fill in the rest.
2024-10-09 04:01:51 +00:00
Answer:
2024-10-09 03:20:19 +00:00
```c
const int segments[10][7] = {
2024-10-09 03:30:42 +00:00
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
2024-10-09 03:41:22 +00:00
{1, 0, 1, 1, 1, 1, 1}, // 6
2024-10-09 03:46:22 +00:00
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
2024-10-09 03:20:19 +00:00
};
```
2024-10-09 03:00:01 +00:00
>10. Write a declaration for an 8x8 `char` array named `chess_board`. Include an initializer that puts the following data into the array, one character per array element:
\[omitted]
2024-10-09 04:01:51 +00:00
```c
2024-10-09 04:06:51 +00:00
char chess_board[8][8] = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{' ', '.', ' ', '.', ' ', '.', ' ', '.'},
{'.', ' ', '.', ' ', '.', ' ', '.', ' '},
{' ', '.', ' ', '.', ' ', '.', ' ', '.'},
{'.', ' ', '.', ' ', '.', ' ', '.', ' '},
{' ', '.', ' ', '.', ' ', '.', ' ', '.'},
{'.', ' ', '.', ' ', '.', ' ', '.', ' '},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R'},
};
2024-10-09 04:01:51 +00:00
```
2024-10-09 04:11:53 +00:00
> 11. Write a program fragment that declares an 8x8 `char` array named `checker_board` and then uses a loop to store the following data into the array (one character per array element).
```c
char checker_board[8][8];
for (int row = 0; row < 8; row++) {
for (int column = 0; column < 8; column++) {
if ((column + row) % 2 == 0) {
checker_board[row][column] = 'B';
} else {
checker_board[row][column] = 'R';
}
}
}
```