22
System Software Experiment 1 Lecture 5 - Array Spring 2018 Jinkyu Jeong ( [email protected]) Computer Systems Laboratory Sungyunkwan University http://csl.skku.edu SSE3032: System Software Experiment 1, Spring 2018 1

System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

  • Upload
    trananh

  • View
    237

  • Download
    11

Embed Size (px)

Citation preview

Page 1: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

System Software Experiment 1 Lecture 5 - Array

Spring 2018

Jinkyu Jeong ( [email protected])

Computer Systems Laboratory

Sungyunkwan University

http://csl.skku.edu

SSE3032: System Software Experiment 1, Spring 2018 1

Page 2: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 동일한 타입의 데이터가 여러 개 저장되어 있는 저장 장소

• 배열의 이름은 그 배열의 시작 주소를 의미

배열(Array)

SSE3032: System Software Experiment 1, Spring 2018 2

int main(void) { int i0; int i1; … int i9; }

int main(void) { int i[10]; // i[0] ~ i[9] }

Page 3: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 자료형 이름[크기]; • 배열 번호(인덱스)는 항상 0부터 시작

배열 선언

SSE3032: System Software Experiment 1, Spring 2018 3

int main(void) { int score[10]; // 10개의 int형 값을 가지는 배열 score float height[10]; // 10개의 float형 값을 가지는 배열 height char name[20]; // 20개의 char형 값을 가지는 배열 name }

Page 4: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

배열 접근

SSE3032: System Software Experiment 1, Spring 2018 4

int main(void) { int score[10]; score[2] = 91; }

91 score

score[0] score[1] score[2] score[9]

Page 5: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 배열의 크기가 주어지지 않으면 초기값의 개수만큼 설정

배열 초기화

SSE3032: System Software Experiment 1, Spring 2018 5

int main(void) { int score[5] = {10, 20, 30}; }

score 10 20 30 0 0

score[0] score[1] score[2]

int main(void) { int score[] = {10, 20, 30}; }

score 10 20 30

score[0] score[1] score[2]

Page 6: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

배열 크기 계산

SSE3032: System Software Experiment 1, Spring 2018 6

int main(void) { int score[] = {10, 20, 30, 40, 50}; int i, size; size = sizeof(score) / sizeof(score[0]); // 4 * 5 / 4 = 5 for (i = 0; i < size; i++) { printf(“Score: %d\n”, score[i]); } }

Page 7: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 배열의 시작 주소를 포인터 변수에 저장 가능

배열과 포인터

SSE3032: System Software Experiment 1, Spring 2018 7

int main(void) { int array[3] = {10, 20, 30}; int *pointer = array; printf(“%d %d\n”, array[0], pointer[0]); printf(“%d %d\n”, array[1], *(pointer+1)); printf(“%d %d\n”, array[2], pointer[2]); }

10 10 20 20 30 30

Page 8: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 배열의 시작 주소는 상수 형태의 주소 값으로 변경이 불가

• 포인터 변수는 저장된 주소 값의 변경이 가능 • name2에 문자열 상수 “Seokha”의 시작 주소를 저장

• name2에 문자열 상수 “Gyusun”의 시작 주소를 저장

배열과 포인터

SSE3032: System Software Experiment 1, Spring 2018 8

int main(void) { char name[6] = “Gyusun”; name = “Seokha”; // complie error char *name2 = “Seokha”; name2 = “Gyusun”; }

Page 9: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• Array에 5개의 float형 데이터를 입력받아 평균을 출력하는 프로그램 생성

Example - 배열

SSE3032: System Software Experiment 1, Spring 2018 9

Page 10: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 배열이 인자인 경우, 배열 원본을 젂달

• 배열 원본의 변경을 원치 않는 경우, const 변수 사용

배열과 함수

SSE3032: System Software Experiment 1, Spring 2018 10

int get_average(int[], int); int main(void) { int score[5] = {10, 20, 30, 40, 50}; int average; average = get_average(score, 5); printf(“Average: %d\n”, average); return 0; }

int get_average(int score[], int num) // int get_average(const int score[], int num) { int i; int sum = 0; for (i = 0; i < num; i++) { sum += score[i]; } return sum / n; }

Page 11: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 선, 면, 입체, … → 1차원, 2차원, 3차원, ...

• 선언 및 초기화

다차원 배열

SSE3032: System Software Experiment 1, Spring 2018 11

int main(void) { int score[5][5]; int array[2][3] = {{1, 2, 3}, {4, 5, 6}}; score[2][3] = 91; }

91

score

score[0][0]

score[1][0]

score[2][0]

score[3][0]

score[4][0]

score[2][3]

Page 12: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 배열 인자의 두 번째 인덱스는 반드시 기입

다차원 배열과 함수

SSE3032: System Software Experiment 1, Spring 2018 12

#define CLASS 2 #define STUDENTS 5 int get_average(int[][STUDENTS], int); int main(void) { int score[CLASS][STUDENTS] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int average; average = get_average(score); printf(“Average: %d\n”, average); return 0; }

int get_average(int score[][STUDENTS]) { int I, j; int sum = 0; for (i = 0; i < CLASS; i++) { for ( j = 0; j < STUDENTS; j++) { sum += score[i]; } } return sum / (CLASS * STUDENTS); }

Page 13: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

포인터 배열

SSE3032: System Software Experiment 1, Spring 2018 13

int main(void) { int num1 = 1, num2 = 2; int *array[2]; array[0] = &num1; array[1] = &num2; printf(“%d\n”, *array[0]); printf(“%d\n”, *array[1]); }

1 2

Page 14: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 3*3 행렬 2개를 더한 행렬을 출력 • 행렬 인자 하나당 3칸씩 출력을 할당

Example – 다차원 배열

SSE3032: System Software Experiment 1, Spring 2018 14

1 2 3

4 5 6

7 8 9

1 2 3

4 5 6

7 8 9

+ = ?

Page 15: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• malloc(size) • 요청한 메모리 바이트만큼 할당

• void *형 시작 주소를 return

배열 동적할당

SSE3032: System Software Experiment 1, Spring 2018 15

int main(void) { char *a = (char *)malloc(sizeof(char) * 4); a[0] = 4; // *a = 4; a[1] = 5; // *(a+1) = 5; }

RAM

0x0

a[0]

a[1]

a[2]

a[3]

Page 16: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• calloc(size a, size b) • b 사이즈만큼 a개의 메모리 할당

• 모든 할당받은 메모리를 0으로 초기화

• realloc(void *p, size) • 메모리 공간의 확장 또는 축소

• 할당된 포인터 p에 대해 size로 크기를 변경

배열 동적할당

SSE3032: System Software Experiment 1, Spring 2018 16

int main(void) { char *a = (char *)calloc(sizeof(char) * 4); a = (char *)realloc(p, sizeof(char) * 6); }

RAM

0x0

Page 17: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• calloc(size a, size b) • b 사이즈만큼 a개의 메모리 할당

• 모든 할당받은 메모리를 0으로 초기화

• realloc(void *p, size) • 메모리 공간의 확장 또는 축소

• 할당된 포인터 p에 대해 size로 크기를 변경

배열 동적할당

SSE3032: System Software Experiment 1, Spring 2018 17

int main(void) { char *a = (char *)calloc(sizeof(char) * 4); a = (char *)realloc(p, sizeof(char) * 6); }

RAM

0x0

a[0]

a[1]

a[2]

a[3]

Page 18: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• calloc(size a, size b) • b 사이즈만큼 a개의 메모리 할당

• 모든 할당받은 메모리를 0으로 초기화

• realloc(void *p, size) • 메모리 공간의 확장 또는 축소

• 할당된 포인터 p에 대해 size로 크기를 변경

배열 동적할당

SSE3032: System Software Experiment 1, Spring 2018 18

int main(void) { char *a = (char *)calloc(sizeof(char) * 4); a = (char *)realloc(p, sizeof(char) * 6); }

RAM

0x0

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

Page 19: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

배열 동적할당 - 다차원

SSE3032: System Software Experiment 1, Spring 2018 19

int main(void) { int i = 0; char **a = (char **)malloc(sizeof(char *) * 2); for (i = 0; i < 2; i++) { a[i] = (char *)malloc(sizeof(char) * 4); } a[0][0] = 4; // *a = 4; a[1][2] = 5; // *(a+1) = 5; }

RAM

0x0

a[0][0]

a[0][1]

a[0][2]

a[0][3]

a[1][0]

a[1][1]

a[1][2]

a[1][3]

Page 20: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 10개의 int input에 대해 오름차순, 내림차순으로 출력

Exercise 1 – 오름차순, 내림차순

SSE3032: System Software Experiment 1, Spring 2018 20

Page 21: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 제시되어 있는 지뢰 위치에 따라 지뢰찾기가 완료된 상태를 출력 • malloc으로 배열 할당

• char형으로 출력 • 숫자 0: 48 or „0‟

• 기호 *: 42 or „*‟

Exercise 2 - Minesweeper

SSE3032: System Software Experiment 1, Spring 2018 21

* * * *

* *

*

*

* *

* * *

* * *

* * *

*

9 * 9

Page 22: System Software Experiment 1 Lecture 5 - …Translate this pagecsl.skku.edu/uploads/SSE2032S18/Lecture5.pdf2018-04-05•동일한 타입의 데이터가 여러 개 저장되어 있는

• 제시되어 있는 지뢰 위치에 따라 지뢰찾기가 완료된 상태를 출력 • malloc으로 배열 할당

• char형으로 출력 • 숫자 0: 48 or „0‟

• 기호 *: 42 or „*‟

Exercise 2 - Minesweeper

SSE3032: System Software Experiment 1, Spring 2018 22