111年地方特考四等程式設計概要
三、針對以下 C 程式,其輸出為下列表格,說明 compress 函式的功能,並完成程式碼(I)、(II)、(III)、(IV),使之執行正確。(25分)
|
答:
(一)compress函式的功能
對二維陣列進行壓縮,將陣列分成大小為 n×n 的小方塊,並且計算每個小方塊內所有元素的平均值,將其作為新陣列中對應位置的值。以下是這個程式的步驟:
1.計算新陣列的大小 cSize = size / n。
2.依次對每個大小為 n×n 的小方塊,計算其內所有元素的平均值,將其設為新陣列中對應位置的值。
3.將新陣列印出。
(二)程式碼(I)、(II)、(III)、(IV)
(I)target
(II)size / 2
(III)[x][y]
(IV)print
(三)完整程式碼
#include <stdio.h> #define SIZE 10 int op(int data[ ][SIZE], int x, int y, int n) { int value = 0; for (int i = x; i < x + n; i++) for (int j = y; j < y + n; j++) value = value + data[i][j]; return value / (n * n); } void print(int target[ ][SIZE], int size) { for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) printf("%d ", target[x][y]); printf("\n"); } } void compress(int data[ ][SIZE], int target[][SIZE], int size, int n) { int cSize = size / n; for (int x = 0; x < cSize; x++) for (int y = 0; y < cSize; y++) target[x][y] = op(data, x * n, y * n, n); print(target, cSize); } int main( ) { int data[ ][SIZE] = { {1, 2, 3, 4, 5, 6}, {3, 4, 5, 6, 7, 8}, {5, 6, 1, 2, 3, 4}, {7, 7, 9, 9, 5, 5}, {2, 2, 4, 4, 8, 8}, {1, 1, 5, 5, 9, 9} }; int target[SIZE][SIZE]; compress(data, target, 4, 2); compress(data, target, 6, 2); compress(data, target, 6, 3); return 0; } |
執行結果:
2 4
6 5
2 4 6
6 5 4
1 4 8
3 5
4 6
執行過程:
compress(data, target, 4, 2)
void compress(int data[ ][SIZE], int target[ ][SIZE], int size, int n) {
int cSize = size / n = 4 / 2 = 2;
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++)
target[x][y] = op(data, x * 2, y * 2, 2);
print(target, 2);
}
compress(data, target, 4, 2)
x = 0
y = 0
op(data, 0, 0, 2)
x = 0
y = 1
op(data, 0, 2, 2)
x = 1
y = 0
op(data, 2, 0, 2)
x = 1
y = 1
op(data, 2, 2, 2)
|
0 |
1 |
2 |
3 |
4 |
5 |
0 |
(0, 0) |
(0, 1) |
|
|
|
|
1 |
(1, 0) |
(1, 1) |
|
|
|
|
2 |
|
|
|
|
|
|
3 |
|
|
|
|
|
|
4 |
|
|
|
|
|
|
5 |
|
|
|
|
|
|
compress(data, target, 6, 2)
void compress(int data[ ][SIZE], int target[ ][SIZE], int size, int n) {
int cSize = size / n = 6 / 2 = 3;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
target[x][y] = op(data, x * 2, y * 2, 2);
print(target, 3);
}
x = 0
y = 0
op(data, 0, 0, 2)
x = 0
y = 1
op(data, 0, 2, 2)
x = 0
y = 2
op(data, 0, 4, 2)
x = 1
y = 0
op(data, 2, 0, 2)
x = 1
y = 1
op(data, 2, 2, 2)
x = 1
y = 2
op(data, 2, 4, 2)
x = 2
y = 0
op(data, 4, 0, 2)
x = 2
y = 1
op(data, 4, 2, 2)
x = 2
y = 2
op(data, 4, 4, 2)
|
0 |
1 |
2 |
3 |
4 |
5 |
0 |
(0, 0) |
(0, 1) |
(0, 2) |
|
|
|
1 |
(1, 0) |
(1, 1) |
(1, 2) |
|
|
|
2 |
(2, 0) |
(2, 1) |
(2, 2) |
|
|
|
3 |
|
|
|
|
|
|
4 |
|
|
|
|
|
|
5 |
|
|
|
|
|
|
compress(data, target, 6, 3)
void compress(int data[ ][SIZE], int target[ ][SIZE], int size, int n) {
int cSize = size / n = 6 / 3 = 2;
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++)
target[x][y] = op(data, x * 3, y * 3, 3);
print(target, 2);
}
x = 0
y = 0
op(data, 0, 0, 3)
x = 0
y = 1
op(data, 0, 3, 3)
x = 1
y = 0
op(data, 3, 0, 3)
x = 1
y = 1
op(data, 3, 3, 3)
|
0 |
1 |
2 |
3 |
4 |
5 |
0 |
(0, 0) |
(0, 1) |
|
|
|
|
1 |
(1, 0) |
(1, 1) |
|
|
|
|
2 |
|
|
|
|
|
|
3 |
|
|
|
|
|
|
4 |
|
|
|
|
|
|
5 |
|
|
|
|
|
|