1 / 12

서울호서전문학교 김 욱

배열. 서울호서전문학교 김 욱. 1. 1 차원 배열의 정의. #include &lt;stdio.h&gt;    main()   { int arr[3]; arr[0] = 100; arr[1] = 200; arr[2] = 500; prinf(“ 배열 요소 array[0] 의 값 : %d<br>”, arr[0] ); prinf(“ 배열 요소 array[1] 의 값 : %d<br>”, arr[1] );

dinah
Download Presentation

서울호서전문학교 김 욱

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 배열 서울호서전문학교 김 욱

  2. 1. 1차원 배열의 정의 #include <stdio.h>    main()   { int arr[3]; arr[0] = 100; arr[1] = 200; arr[2] = 500; prinf(“배열 요소 array[0]의 값: %d\n”, arr[0]); prinf(“배열 요소 array[1]의 값: %d\n”, arr[1]); prinf(“배열 요소 array[2]의 값: %d\n”, arr[2]); } ① ② 프로그램 설명 ① 정수형 자료 3개를 저장하기 위한 배열을 arr라는 이름으로 정의 ② 배열의 첫번째 요소에 100, 두번째 요소에 200 세번째 요소에 500을 저장

  3. 2. 1차원 배열의 초기화 #include <stdio.h>    main()   { int i; float arrf[3] = {100.0, 200.0, 300.0}; for(i = 0; i<3; i++) printf(“배열 요소 arrf[%d]의 초기 값: %f\n”, i, arrf[i]); } ① 프로그램 설명 ①실수형 자료3개를 저장하기 위한 배열 arrf를 초기화 arrf[0] = 100.0 arrf[1] = 200.0 arrf[2] = 300.0

  4. 3. 초기값의 개수가 배열 요소의 개수보다 적은 경우 #include <stdio.h> main() { int ai[3] = { 100, 200 };① float af[3] = { 1.0, 2.0 };② int i = 0; for(i=0; i<3; i++) printf(" 배열 요소 ai[%d]의 초기값 : %d“, i, ai[i]); for(i=0; i<3; i++) printf("배열 요소 af[%d]의 초기값 : %f“, i, af[i]); } 프로그램 설명 ① 초기값이 지정되지 않은 정수형 배열 요소 ai[2]에는정수 0이 초기값으로 내정 ② 초기값이 지정되지 않은 실수형 배열 요소 af[2]에는 실수 0.0이 내정

  5. 4. 크기가 생략된 배열의 정의 #include <stdio.h> main() { int arr[] = { 100, 200, 300 }; ① int i = 0; for(i=0; i<3; i++) printf("배열 요소 arr[%d]의 값 : %d\n ", i, arr[i]); } 프로그램 설명 ① 배열의 크기를 지정하는 첨자를 생략하면, 초기값의 개수를 배열의 크기로 간주함

  6. 5. 문자형 배열의 정의 및 초기화 #include <stdio.h> main() { int i ; char c[4] = { 'A','B','C','D' }; for(i=0; i<4; i++) printf("배열 요소 c[%d]의 초기값 : %c\n", i, c[i]); printf("문자형 배열 c의 내용 : %s\n", c); } ① ② 프로그램 설명 ① 문자형 배열에서는 내부의 문자를 1자씩 나열하는 방식으로 초기값을 지정 ② 문자열 배열에 저장된 문자열을 %s 형식으로 출력시 뒤에 null 문자가 추가되지 않았으므로 예측할수 없는 결과가 표시됨

  7. 6. 문자형 배열의 null문자의 사용 예 #include <stdio.h> main() { int i ; char c[5] = { 'A','B','C','D‘,’\0’ }; for(i=0; i<4; i++) printf("배열 요소 c[%d]의 초기값 : %c\n", i, c[i]); printf("문자형 배열 c의 내용 : %s\n", c); } ① ② 프로그램 설명 ① 문자형 배열에서 초기값을 지정하면서 마지막에 null문자를 추가시킴 ② 마지막에 null문자를 추가시킴으로 문자열이 정확하게 출력됨

  8. 7. 문자열에 의한 문자형 배열의 초기화 #include <stdio.h> main() { int i ; char c[5] = “ABCD”; for(i=0; i<5; i++) printf("c[%d]의 초기값 : %c\n", i, c[i]); printf("문자형 배열 c의 내용 : %s\n", c); } ① 프로그램 설명 ① 이중부호 내에서 “ABCD”와 같이 문자열을 초기화하면, 문자열의 마지막에 null 문자를자동으로 추가

  9. 8. 2차원 배열의 정의와 사용 방법 #include <stdio.h> main() { int a[2][3]; int i, j; a[0][0] = 11; a[0][1] = 12; a[0][2] = 13; a[1][0] = 21; a[1][1] = 22; a[1][2] = 23; for(i=0; i<2; i++) { for(j=0; j<3; j++) { printf(" 배열a[%d][%d]의값: %d", i, j, a[i][j]); } printf("\n"); } } ① ② 프로그램 설명 ① 2차원 배열 a는 2행의 3열의 구조를 가짐 ② 2차원 배열의 각 요소에 값을 할당

  10. 9. 2차원 배열의 초기화 #include <stdio.h> main() { //a[0][0] ………………. a[2][2] int a[3][3] = { 11, 12, 13, 21, 22, 23, 31, 32, 33}; int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf(" 배열a[%d][%d]의값: %d", i, j, a[i][j]); } printf("\n"); } } ① 프로그램 설명 ① {} 사이에 나열된 초기값 리스트를 이용하여 초기화함

  11. 10. 2차원 배열의 행 단위 초기화 #include <stdio.h> main() { int a[3][3] = { {11, 12, 13}, //a[0][0], a[0][1], a[0][2] {21, 22, 23}, //a[1][0], a[1][1], a[1][2] {31, 32, 33} //a[2][0], a[2][1], a[2][2] }; int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf(“ 배열a[%d][%d]의값: %d ", i, j, a[i][j]); } printf("\n"); } } ① 프로그램 설명 ① 2차원 배열을 행단위로 나누어서 초기화함, 2차원 구조를 보다 쉽게 구분및 이해

  12. 실습(#1) 정수형 자료를 5개 저장할수 있는 배열을 만든다 키보드를 통해 5개의 숫자를 입력받은후 배열에 저장한다. 배열의 이름을 인수로 전달하면, 각 배열 요소의 값을 화면에 출력하는 함수를 작성하시오. void show_array(int a[]) 위의 함수 선언 및 정의하여 main함수에서 호출하시오. 호출예) int a[5]; show_array(a);

More Related