{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 배열\n", "\n", "배열에서 기억해야 할 핵심은 첫 번째 요소를 가리키는 인덱스 또는 첨자가 0라는 것이다. 많은 사람들이 첨자와 실제 저장된 값을 혼돈하는데, 이는 프로그램에서 치명적인 오류로 이어지기도 한다. \n", "\n", "또 하나 기억해야 하는 것은 배열을 선언한 형에 따라 배열이 메모리에서 점유하는 크기가 정해진다. 예를 들어 사용자가 배열의 길이를 10이라고 정했다고 하자. 이 배열이 `int`형으로 선언되었다면 그 크기가 $10*sizeofint$가 된다. sizeofint는 코드에서 sizeof(int)로 구할 수 있고, 시스템마다 다르지만 보통 4byte를 쓴다. 계산하면 $40$이다. 만약 `int`형이 아닌 `double`을 썼다고 해보자. `double`은 일반적으로 8byte이기 때문에 $80$가 된다. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "다음의 코드를 읽어 보고 생각을 해보자. 아래의 프로그램에서 사용된 배열은 `int`형인데, 다른 형으로 변경했을 때 각 요소들의 위치가 어떻게 바뀌는지 살펴보자. 요소들의 주소를 살펴보았을 때 위치와 크기 등에 대해서 어떤 결론을 내릴 수 있는가?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int array[5];\n", " for(int i = 0; i < 5; i++)\n", " {\n", " printf(\"address of array[%d] is %u\", i, &array[i]);\n", " }\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "주의: `scanf`는 Jupyter-notebook에서 구현 안됨. 터미널에서 컴파일하고 실행해야 함.\n", "\n", "아래의 프로그램을 실행 전 어떤 결과를 얻을 수 있을 지 예상해보자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int score[3];\n", " int sum = 0;\n", " printf(\"Enter score of three subjects\\n\");\n", " \n", " for(int i = 0; i < 3; i++)\n", " {\n", " scanf(\"%d\", &score[i]);\n", " sum += score[i];\n", " }\n", " printf(\"total: %d\\n\", sum);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "아래의 코드는 배열의 다양한 선언 방식과 초기화에 대한 이해를 돕기 위한 것이다. 5-7번 줄의 코드를 읽고, 어떤 값들이 for 루프에서 출력될지 예상해보고, 실행 결과와 같은지 확인해보자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int array1[4] = {0, 1, 2, 3};\n", " int array2[] = {0, 1, 2, 3};\n", " int array3[4] = {0, 1};\n", " \n", " int i;\n", " \n", " for(i = 0; i < 4; i++)\n", " {\n", " printf(\"array1[%d]: %d \", i, array1[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " for(i = 0; i < 4; i++)\n", " {\n", " printf(\"array2[%d]: %d \", i, array3[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " for(i = 0; i < 4; i++)\n", " {\n", " printf(\"array3[%d]: %d \", i, array3[i]);\n", " }\n", " printf(\"\\n\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "다음의 코드는 문자열에 대한 것이다. 배열에 문자를 한 글자씩 저장을 하려고 하는 데 사용된 방법을 살펴보자. 5-6번 줄에 배열의 길이가 다른 것에 유의해야 한다. 왜 다른지 그리고 결과에 어떤 영향을 주는지 살펴보자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " char ch1[5] = \"yang\";\n", " char ch2[4] = \"king\";\n", " \n", " int i;\n", " \n", " printf(\"First name is \");\n", " for(i = 0; i < 5; i++)\n", " {\n", " printf(\"%c\", ch1[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " printf(\"First name is \");\n", " for(i = 0; i < 5; i++)\n", " {\n", " printf(\"%c\", ch2[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " printf(\"First name is %s\\n\", ch1);\n", " printf(\"First name is %s\", ch2);\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "`arrary1`에 있는 값들을 `array2`에 복사하려고 한다. 첫 번째 걸림돌은 배열의 길이를 선언해야 하는 것이다. 현재 6번 줄은 크기가 지정되지 않았다. 4라고 써도 되겠지만, `array1`의 길이가 변경되면 6번 줄도 바뀌어야 하기 때문에 실수할 여지가 많다. 두 번째는 실제로 배열의 값들을 복사하는 일이다. 아래 코드를 수정하여 원하는 동작을 하도록 해보자. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int array1[] = {5, 10, 15, 20};\n", " int array2[]; // modify this line\n", " \n", " int i;\n", " \n", " printf(\"Contents of array1: \\t\");\n", " for(i = 0; i < 4; i++)\n", " {\n", " printf(\"%d\\t\", array1[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " // your code here \n", " \n", " printf(\"Contents of array2: \\t\");\n", " for(i = 0; i < 4; i++)\n", " {\n", " printf(\"%d\\t\", array2[i]);\n", " }\n", " printf(\"\\n\");\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "다음의 코드는 다차원 배열의 선언과 초기화에 관한 것이다. 배열의 내용을 하나씩 출력하는 코드를 채워 넣어 보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int i, j;\n", " int array1[2][3] = {1, 2, 3, 4, 5, 6};\n", " int array2[2][3] = {1, 2, 3};\n", " int array3[2][3] = {{1, 2, 3}, {4, 5, 6}};\n", " \n", " printf(\"array1 \\n\");\n", " // your code here\n", " \n", " printf(\"\\narray2 \\n\");\n", " // your code here\n", " \n", " printf(\"array3 \\n\");\n", " // your code here\n", " \n", " printf(\"\\n\");\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제 \n", "\n", "2차원 배열에 저장된 글자들에 대한 것으로 앞에서 1차원 배열에 저장된 문자들과 같은 맥락의 코드이다. 코드를 추가해서 2차원 배열에서도 같은 식으로 동작하는지 확인하여 보고, 일반화할 수 있는 지 생각해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int i, j;\n", " char ch1[2][3] = {\"ab\", \"cd\"};\n", " char ch2[2][3] = {\"ab\"};\n", " \n", " printf(\"values in ch1\\n\");\n", " // your code here\n", " \n", " printf(\"values in ch2\\n\");\n", " // your code here\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "이번에는 배열을 응용하여 프로그램을 작성하여 보자. 달성하고자 하는 목표는 배열에 있는 1의 개수가 짝수이면 1을 출력하는 프로그램을 작성하는 것이다. 빈 영역을 채워 넣어 보자.\n", "\n", "\n", "힌트: 짝수인지 홀수인지 판별하기 위해 나머지 연산자를 사용하면 됨" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#define LENGTH(A) (sizeof(A)/sizeof(A[0]))\n", "int main()\n", "{\n", " int values[] = {1, 1, 0, 0, 1, 0, 1};\n", " int cnt = 0, even = 0;\n", "\n", " // your code here\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "다음은 배열의 값이 이전 값과 다른 경우에만 그 값을 새로운 배열 `su`에 저장하고 출력하는 코드의 일부이다. 어떻게 하면 목적을 달성 할 수 있는지 생각해보고 검증해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int values[] = {10, 20, 20, 30, 30, 30};\n", " int su[6];\n", " int cnt = 0;\n", " \n", " su[0] = values[0];\n", " \n", " for(int i = 1; i <= 5; i++)\n", " {\n", " // your code here\n", " }\n", " \n", " for(int i = 0; i <= cnt; i++)\n", " {\n", " printf(\"%d \", su[i]);\n", " }\n", " \n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "배열의 값을 분석하여 서로 다른 경우를 판별하는 문제를 앞에서 다뤘었다. 이 문제에서는 동일한 문자가 연속적으로 출현한 경우 그 빈도를 출력하는 프로그램을 작성하려고 한다. 단, 빈도가 가장 높은 글자의 수만 출력한다. \n", "예로, `hello`였을 때 결과는 2가 된다. \n", "\n", " h를 h와 비교, e와 비교, l과 비교, l과 비교, \n", " 그리고 o와 비교 하여 같은 지 판단하고 같은 경우에만 cnt를 증가함. \n", " h는 1번만 나왔기 때문에 1이 됨\n", " l의 경우 두 번 나타남. 그러므로 2가 됨\n", " \n", "힌트: `for`루프가 두 번 필요하고 cnt는 현재 중복 횟수를 top은 전체의 최대 빈도수를 저장하도록 하자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " char word[] = \"hello\";\n", " int cnt, top = 0;\n", " \n", " // your code\n", " \n", " printf(\"%d\\n\", top);\n", "\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 문제\n", "\n", "두 배열의 값을 서로 비교하여 각 인덱스의 값들 중 더 큰 값을 저장하는 프로그램을 작성하려고 한다. 목적에 맞는 코드를 작성해보자." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "int main()\n", "{\n", " int value1[] = {2, 4, 8, 10, 11};\n", " int value2[] = {1, 5, 9, 11, 12};\n", " int result[5];\n", " \n", " // your code here\n", " \n", " for(int i = 0; i <= 4; i++)\n", " {\n", " printf(\"%d \", result[i]);\n", " }\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "C", "language": "c", "name": "c" }, "language_info": { "file_extension": ".c", "mimetype": "text/plain", "name": "c" } }, "nbformat": 4, "nbformat_minor": 2 }