1 p 256 O quiz0 1 printfd quiz1

  • Slides: 72
Download presentation

1차원 배열 원소 참조 p. 256 O 예) § quiz[0] = 1; § printf("%d",

1차원 배열 원소 참조 p. 256 O 예) § quiz[0] = 1; § printf("%d", quiz[1]); § scanf("%d", &quiz[0]); § quiz[4]++; 6

Program 7 -2 배열의 최솟값 구하기 p. 262 2 #define N 5 3 4

Program 7 -2 배열의 최솟값 구하기 p. 262 2 #define N 5 3 4 int main() 5 { 6 int f[N] = {3, 0, -30, -20, -1}; // 배열 선언 7 int i, min; 8 9 // 최솟값 구하기 10 min = f[0]; 11 for (i=1; i<N; i++) 12 { 13 if (f[i] < min) 14 min = f[i]; 15 } 16 18 printf("어는 점 목록: "); 19 for (i=0; i<N; i++) 20 printf("%4 d", f[i]); 21 23 printf("n가장 낮은 어는 점: %d n", min); 14

Program 7 -4 가장 단순한 버블 정렬 p. 267 2 #define SIZE 5 3

Program 7 -4 가장 단순한 버블 정렬 p. 267 2 #define SIZE 5 3 4 int main() 5 { 6 int i, repeat, temp, a[SIZE] = {5, 4, 3, 2, 1}; 7 9 for (repeat=1; repeat<SIZE; repeat++) //(배열 원소수-1)번 반 복 10 11 12 13 15 16 17 18 19 20 21 22 { // 두 원소간 정렬을 차례대로 반복하기 for (i=0; i<SIZE-1; i++) { if (a[i] > a[i+1]) { temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } } // 바깥 for 끝 26

버블 정렬: [프로그램 7 -4]의 개선 repeat가 repeat가 1 2 3 4 → →

버블 정렬: [프로그램 7 -4]의 개선 repeat가 repeat가 1 2 3 4 → → p. 268 (a[0], a[1]), (a[1], a[2]), (a[2], a[3]), (a[3], a[4]) (a[0], a[1]), (a[1], a[2]), (a[2], a[3]) (a[0], a[1]), (a[1], a[2]) (a[0], a[1]) → → i가 i가 0 0 ~ ~ 3까지 a[i]와 a[i+1] 비교 2까지 “ 1까지 “ 0까지 “ 14 for (repeat=1; repeat<SIZE; repeat++) 15 { 16 for (i=0; i<SIZE-repeat; i++) 17 { 18 if (a[i] > a[i+1]) : 28

Program 7 -5 정렬이 완료된 원소 간 비교를 제거 p. 269 2 #define SIZE

Program 7 -5 정렬이 완료된 원소 간 비교를 제거 p. 269 2 #define SIZE 5 3 4 int main() 5 { 6 int i, repeat, temp, a[SIZE] = {5, 4, 3, 2, 1}; 7 13 // 버블 정렬 시작 14 for (repeat=1; repeat<SIZE; repeat++) 15 { 16 for (i=0; i<SIZE-repeat; i++) 17 { 18 if (a[i] > a[i+1]) 19 { 20 temp = a[i]; 21 a[i] = a[i+1]; 22 a[i+1] = temp; 23 } 24 } 25 } 29

Program 7 -8 학생의 정보를 문자열로 입력받아 출력하기 p. 286 1 #include <stdio. h>

Program 7 -8 학생의 정보를 문자열로 입력받아 출력하기 p. 286 1 #include <stdio. h> 3 int main() 4 { 5 char university[11], std_no[9], name[10]; 6 int i, j; // 문자열 저장 배열 7 9 printf("학교명은? "); scanf("%s", university); 11 printf("학번은? "); 13 fflush(stdin); 14 printf("이름은? "); 15 gets(name); scanf("%s", std_no); //std_no 입력후 남은 엔터키가 15행에서 사용되는 것을 방지 // 빈 칸 포함 이름 입력받도록 gets 이용 16 17 printf("=============n"); 18 printf("%s %c%c학번 %s n", 19 20 university, std_no[2], std_no[3], name); printf("=============n"); 57

Program 7 -10 3차원 배열의 출력 p. 291 2 #define L 2 // 면

Program 7 -10 3차원 배열의 출력 p. 291 2 #define L 2 // 면 개수 3 #define M 3 // 행 개수 4 #define N 4 // 열 개수 5 6 int main() 7 { 8 int n[L][M][N] = {{{20, 22, 30, 35}, {12, 19, 35, 22}, {10, 9, 15, 20}}, 9 {{20, 22, 30, 35}, {12, 19, 35, 22}, {12, 15, 9, 25}}}; 10 int i, j, k; 11 12 for (i=0; i<L; i++) // 면 개수만큼 반복 13 { 14 printf("n %d면 nn", i+1); 15 for (j=0; j<M; j++) // 행 개수만큼 반복 16 { 17 for (k=0; k<N; k++) // 열 개수만큼 반복 18 printf("%3 d ", n[i][j][k]); 19 printf("n"); 20 } 21 } 67