쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 10-3
11.
#include <stdio.h>
int location[] = {
1, 1, 2, 5, 3, 3, 1, 1, 2, 4
}
;
int main(void) {
int product;
printf("상품 번호를 입력하시요:");
scanf("%d", &product);
if (product < 0 || product >= 10)
printf("잘못된 상품 번호입니다.\n");
else
printf("상품 번호 %d의 위치는 %d입니다.\n", product, location[product]);
return 0;
}
12.
(a)transpose(int a[][3], int b[][3])
#include <stdio.h>
void transpose(int a[][3], int b[][3]) {
int r, c;
for (r = 0; r < 3; r++)
for (c = 0; c < 3; c++)
b[r][c] = a[c][r];
}
void print(int a[][3]) {
int r, c;
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++)
printf("%d ", a[r][c]);
printf("\n");
}
}
int main(void) {
int A[3][3] = { {
1, 2, 3
}
, {
4, 5, 6
}
, {
7, 8, 9
}
}
;
int B[3][3];
transpose(A, B);
print(B);
return 0;
}
(b)mult(int a[][3], int b[][3], int c[][3])
#include <stdio.h>
void mult(int A[][3], int B[][3], int C[][3]) {
int r, c, k;
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++) {
int sum = 0;
for (k = 0; k < 3; k++)
sum += A[r][k] * B[k][c];
C[r][c] = sum;
}
}
}
void print(int A[][3]) {
int r, c;
for (r = 0; r < 3; r++) {
for (c = 0; c < 3; c++)
printf("%d ", A[r][c]);
printf("\n");
}
}
int main(void) {
int A[3][3] = { {
1, 2, 3
}
, {
4, 5, 6
}
, {
7, 8, 9
}
}
;
int B[3][3] = { {
1, 2, 3
}
, {
4, 5, 6
}
, {
7, 8, 9
}
}
;
int C[3][3];
mult(A, B, C);
print(C);
return 0;
}
13.
#include <stdio.h>
void show_digit(int n) {
int i;
int binary[32] = {
0
}
;
for (i = 0; i < 32 && n > 0; i++) {
binary[i] = n % 2;
n = n / 2;
}
for (i--; i >= 0; i--) {
printf("%d", binary[i]);
}
printf("\n");
}
int main(void) {
show_digit(32);
return 0;
}
14.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define ROWS 20
#define COLS 20
int grid[ROWS][COLS];
int ro, co;
void mark_grid(int y, int x) {
if (x >= 0 && y >= 0 && y < ROWS && x < COLS) {
ro = y;
co = x;
if (grid[y][x] == '.')
grid[y][x] = ' *';
}
}
void print_grid() {
int r, c;
for (r = 0; r < ROWS; r++) {
for (c = 0; c < COLS; c++) {
printf("%c ", grid[r][c]);
}
printf("\n");
}
}
int main() {
int r, c;
int i;
char letters = '*';
for (r = 0; r < ROWS; r++) {
for (c = 0; c < COLS; c++) {
grid[r][c] = '.';
}
}
srand((unsigned)time(NULL));
ro = ROWS / 2;
co = COLS / 2;
grid[ro][co] = letters;
for (i = 0; i < 100000; i++) {
int move = rand() % 8;
switch (move) {
case 0:
mark_grid(ro + 1, co);
break;
case 1:
mark_grid(ro + 1, co + 1);
break;
case 2:
mark_grid(ro, co + 1);
break;
case 3:
mark_grid(ro - 1, co + 1);
break;
case 4:
mark_grid(ro - 1, co);
break;
case 5:
mark_grid(ro - 1, co - 1);
break;
case 6:
mark_grid(ro, co - 1);
break;
case 7:
mark_grid(ro + 1, co - 1);
break;
}
print_grid();
if (getch() == 'q')
break;
}
return 0;
}
15.
#include <stdio.h>
#include <stdlib.h>
void sieve(int s[], int size);
int main(void) {
int top_value = 100;
int count = top_value - 1;
int my_array[101];
int i, prime, multiple;
for (i = 2; i <= top_value; ++i)
my_array[i] = 1;
for (prime = 2; prime <= top_value; ++prime) {
if (my_array[prime])
for (multiple = 2 * prime; multiple <= top_value; multiple +=prime)
my_array[multiple] = 0;
}
for (i = 2; i <= top_value; ++i) {
if (my_array[i])
printf("%d ", i);
}
return 0;
}
댓글
댓글 쓰기