본문 바로가기
IT/프로그래밍

쉽게 풀어 쓴 C언어 Express 10장 프로그래밍 문제 해설

by nutrient 2020. 12. 10.
728x90
728x170

 

쉽게 풀어 쓴 C언어 Express 10장 프로그래밍 문제 해설

쉽게 풀어 쓴 C언어 Express 10장 프로그래밍 문제 해설

1.

#include <stdio.h> 

int days[] = { 

31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 

} 

; 

int main(void) { 

int i; 

for (i=0; i<12; i++) 

printf("%d월은 %d일까지 있습니다.\n", i+1, days[i]); 

return 0; 

} 



2.

#include <stdio.h> 

int main(void) { 

int list[10]; 

int i, max, min; 

for (i=0;i<10;i++) 

list[i] = rand(); 

max =min=list[0]; 

for (i=1;i<10;i++) { 

if( list[i] < min ) 

min=list[i]; 

if( list[i] > max ) 

max=list[i]; 

} 

printf("최대값은 %d\n", max); 

printf("최소값은 %d\n", min); 

return 0; 

} 

 



3.

#include <stdio.h> 

#define N_DATA 10 

int array_equal(int a[], int b[], int size); 

int main(void) { 

int A[N_DATA] = { 
1, 2, 3 } ; 

int B[N_DATA] = { 0 }; 

printf("%d\n",array_equal(A, B, N_DATA)); 

return 0; 

} 

int array_equal(int a[], int b[], int size) { 

int i; 

for (i=0; i<size; i++) { 

if( b[i] != a[i] ) 

return 0; 

} 

return 1; 

} 



4.

#include <stdio.h> 

#define N_DATA 10 

void array_copy(int a[], int b[], int size); 

void array_print(int a[], int size); 

int main(void) { 

int A[N_DATA] = { 

1, 2, 3 

} 

; 

int B[N_DATA] = { 

0 

} 

; 

array_copy(A, B, N_DATA); 

array_print(B, N_DATA); 

return 0; 

} 

void array_copy(int a[], int b[], int size) { 

int i; 

for (i=0; i<size; i++) { 

b[i] = a[i]; 

} 

} 

void array_print(int a[], int size) { 

int i; 

for (i=0; i<size; i++) { 

printf("%d ", a[i]); 

} 

printf("\n"); 

} 

 

 



5.

#include <stdio.h> 

int main(void) { 

int freq[10]= { 

0 

} 

; 

int i, max; 

for (i=0;i<100;i++) { 

++freq[rand()%10]; 

} 

max = 0; 

for (i=1;i<10;i++) { 

if( freq[i] > max ) 

max = i; 

} 

printf("%d\n", max); 

return 0; 

} 

 



6.

#include <stdio.h> 

int main(void) { 

int i, j, sum; 

int data[3][5] = { { 

12, 56, 32, 16, 98 

} 

, { 

99, 56, 34, 41, 3 

} 

, { 

65, 3, 87, 78, 21 

} 

} 

; 

for (i=0; i<3; i++) { 

sum = 0; 

for (j=0; j<5; j++) 

sum += data[i][j]; 

printf("%d행의 합계: %d\n", i, sum); 

} 

for (j=0; j<5; j++) { 

sum = 0; 

for (i=0; i<3; i++) 

sum += data[i][j]; 

printf("%d열의 합계: %d\n", j, sum); 

} 

return 0; 

} 




7.

#include <stdio.h> 

int main(void) { 

int list[3][10]= { 

0 

} 

; 

int value, i; 

for (i=0;i<10;i++) { 

list[0][i] = i; 

list[1][i] = i*i; 

list[2][i] = i*i*i; 

} 

printf("정수를 입력하시오:"); 

scanf("%d", &value); 

for (i=0;i<10;i++) { 

if( value == list[2][i] ) 

printf("%d의 세제곱근은 %d\n", value, i); 

} 

return 0; 

} 

 



8.

#include <stdio.h> 

#define N_DATA 10 

void get_data(double data[]); 

double get_mean(double data[]); 

double get_std_dev(double data[], double mean); 

int main(void) { 

double data[20]; 

double mean; 

get_data(data); 

mean = get_mean(data); 

printf("평균값은 %f\n", mean); 

printf("표준편차값은 %f\n", get_std_dev(data, mean)); 

return 0; 

} 

void get_data(double data[]) { 

int i; 

for (i=0; i<N_DATA; i++) { 

printf("데이터를 입력하시오:"); 

scanf("%lf", &data[i]); 

} 

} 

double get_mean(double data[]) { 

int i; 

double sum=0.0; 

for (i=0; i<N_DATA; i++) { 

sum += data[i]; 

} 

return sum/N_DATA; 

} 

double get_std_dev(double data[], double mean) { 

int i; 

double sum=0.0; 

for (i=0; i<N_DATA; i++) { 

sum += (data[i]-mean)*(data[i]-mean); 

} 

return sum/N_DATA; 

} 

 


9.

#include <stdio.h> 

void get_stat_per_test(int score[][3]) { 

int i, k, min, max, sum; 

for (k=0;k<3;k++) { 

min=max=score[0][k]; 

sum=0; 

for (i=0;i<10;i++) { 

if(score[i][k]<min) min=score[i][k]; 

if(score[i][k]>min) max=score[i][k]; 

sum += score[i][k]; 

} 

printf("최대점수=%d\n", max); 

printf("최저점수=%d\n", min); 

} 

} 

int main(void) { 

int i, k; 

int score[10][3]; 

for (i=0;i<10;i++) { 

for (k=0;k<3;k++) { 

score[i][k]=rand()%100+1; 

} 

} 

get_stat_per_test(score); 

return 0; 

} 

 



10.

(a)

#include <stdio.h> 

void vector_add(double x[], double y[], double z[]); 

int main(void) { 

double x[3]= { 

1.0, 2.0, 3.0 

} 

; 

double y[3]= { 

1.0, 2.0, 3.0 

} 

; 

double z[3]; 

vector_add(x, y, z); 

return 0; 

} 

void vector_add(double x[], double y[], double z[]) { 

z[0] =x[0]+y[0]; 

z[1] =x[1]+y[1]; 

z[2] =x[2]+y[2]; 

} 



(b)

#include <stdio.h> 

double vector_dotp(double x[], double y[]); 

int main(void) { 

double x[3]= { 

1.0, 2.0, 3.0 

} 

; 

double y[3]= { 

1.0, 2.0, 3.0 

} 

; 

double value; 

value = vector_dotp(x, y); 

printf("%f\n", value); 

return 0; 

} 

double vector_dotp(double x[], double y[]) { 

int i; 

double result; 

result = 0.0; 

for (i=0;i<3;i++) 

result += x[i]*y[i]; 

return result; 

} 



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; 

}

728x90
그리드형

댓글