======C++ 스터디 #5: 배열====== | 시간 | 2021년 5월 13일 목요일 20:30 ~ 22:10 | | 장소 | Google Meet | | 참가자 | - | {{youtube>BkfRhejM7So?medium}} 출처: https://dojang.io/course/view.php?id=2 (코딩도장)\\ https://boycoding.tistory.com/category (소년코딩) =====1. 배열을 선언하고 요소에 접근하기===== 배열은 같은 자료형의 변수를 일렬로 늘어 놓은 형태이며 반복문과 결합하면 연속적이고 반복되는 값을 손쉽게 처리할 수 있습니다. 학생 30명의 성적을 기록하는 경우를 생각해봅시다. 배열이 없다면 30개의 변수를 선언하고 할당해야 합니다. 하지만 배열을 사용하면 이러한 작업을 훨씬 쉽게 수행할 수 있습니다. ====1.1. 배열 선언 및 요소 접근(1)==== #include using namespace std; int main() { int numArr[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 }; cout << numArr[0] << " " << numArr[2] << " " << numArr[9] << endl; cout << numArr[-1] << " " << numArr[10] << endl; return 0; } ====1.2. 배열 선언 및 요소 접근(2)==== #include using namespace std; int main() { int numArr[5]; numArr[0] = 11; numArr[1] = 22; numArr[2] = 33; numArr[3] = 44; cout << numArr[2] << endl; cout << numArr[4] << endl; return 0; } ====1.3. 배열 선언 및 cin 사용하기==== #include using namespace std; int main() { int numArr[5]; cin >> numArr[0] >> numArr[1] >> numArr[2] >> numArr[3]; cout <<"numArr[2]: "< =====2. 배열을 0으로 초기화하기===== ====2.1. 배열을 0으로 초기화하기 예시(1)==== #include using namespace std; int main() { int numArr[10] = { 11, 22, 33 }; cout << numArr[0] << " " << numArr[2] << " " << numArr[9] << endl; return 0; } ====2.2. 배열을 0으로 초기화하기 예시(2)==== #include using namespace std; int main() { int numArr[10] = { }; cout << numArr[0] << " " << numArr[2] << " " << numArr[9] << endl; return 0; } =====3. 배열의 크기 구하기===== 코딩을 할 때 코드를 다 작성했는데 배열의 크기를 줄여야 하는 상황일 때, 배열의 크기는 줄였지만 크기와 연관된 코드를 전부 수정하지 않으면 버그가 발생하게 됩니다. 이러한 실수를 방지하기 위해서는 배열의 크기가 바뀌었을 때 배열의 크기를 알아서 계산하도록 만들면 됩니다. ====3.1 sizeof 사용하기==== sizeof는 자료형이나 배열 같은 표현식의 크기를 바이트 단위로 구하는 연산자입니다. #include using namespace std; int main() { int num1 = 1; int array[] = {1, 2, 3, 4, 5, 6, 7, 8}; cout << "sizeof(int): " << sizeof(int) << endl; cout << "sizeof(num1): " << sizeof(num1) << endl; cout <<"sizeof(array): "< ====3.2 sizeof로 배열 크기 구하기==== #include using namespace std; int main() { int array[] = {1, 2, 3, 4, 5, 6, 7, 8}; cout <<"sizeof(array): "< =====4. 반복문으로 배열의 요소 출력하기===== 반복문을 사용하여 배열의 요소를 쉽게 출력할 수 있습니다. ====4.1 순서대로 출력하기==== #include using namespace std; int main() { int numArr[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 }; for (int i = 0; i < sizeof(numArr) / sizeof(int); i++) { cout< ====4.2 역순으로 출력하기==== #include using namespace std; int main() { int numArr[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 }; for (int i = sizeof(numArr) / sizeof(int)-1; i >=0 ; i--) { cout< ====4.3 반복문으로 배열의 요소 합계 구하기==== #include using namespace std; int main() { int numArr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int sum = 0; for (int i = 0; i < sizeof(numArr) / sizeof(int); i++) { sum += numArr[i]; } cout << sum << endl; return 0; } =====5. 2차원 배열을 선언하고 요소에 접근하기===== 2차원 배열은 다음과 같이 가로 X 세로 형태로 이루어져 있으며 행과 열 모두 0부터 시작합니다. {{activity:public:2021:cpp:2차원_배열_개념.png?500}} ====5.1. 2차원 배열 선언 및 요소 접근(1)==== #include using namespace std; int main() { int numArr[3][4] = { { 11, 22, 33, 44 }, { 55, 66, 77, 88 }, { 99, 110, 121, 132 } }; cout <<"numArr[0][0]: "<< numArr[0][0] << endl; cout << "numArr[1][2]: " << numArr[1][2] << endl; cout << "numArr[2][0]: " << numArr[2][0] << endl; cout << "numArr[2][3]: " << numArr[2][3] << endl; return 0; } ====5.2. 2차원 배열 선언 및 요소 접근(2)==== #include using namespace std; int main() { int numArr[3][4]; numArr[0][0] = 11; numArr[0][1] = 22; numArr[0][2] = 33; numArr[0][3] = 44; numArr[1][0] = 55; numArr[1][1] = 66; numArr[1][2] = 77; numArr[1][3] = 88; numArr[2][0] = 99; numArr[2][1] = 110; numArr[2][2] = 121; numArr[2][3] = 132; cout <<"numArr[0][0]: "<< numArr[0][0] << endl; cout << "numArr[1][2]: " << numArr[1][2] << endl; cout << "numArr[2][0]: " << numArr[2][0] << endl; cout << "numArr[2][3]: " << numArr[2][3] << endl; return 0; } =====6. 2차원 배열을 0으로 초기화하기===== ====6.1. 2차원 배열을 0으로 초기화하기 예시(1)==== #include using namespace std; int main() { int numArr[3][4] = {1}; cout << "numArr[0][0]: " << numArr[0][0] << endl; cout << "numArr[1][2]: " << numArr[1][2] << endl; cout << "numArr[2][0]: " << numArr[2][0] << endl; cout << "numArr[2][3]: " << numArr[2][3] << endl; return 0; } ====6.2. 2차원 배열을 0으로 초기화하기 예시(2)==== #include using namespace std; int main() { int numArr[3][4] = { }; cout << "numArr[0][0]: " << numArr[0][0] << endl; cout << "numArr[1][2]: " << numArr[1][2] << endl; cout << "numArr[2][0]: " << numArr[2][0] << endl; cout << "numArr[2][3]: " << numArr[2][3] << endl; return 0; } =====7. 2차원 배열의 크기 구하기===== ====7.1 sizeof로 2차원 배열 크기 구하기==== #include using namespace std; int main() { int numArr[3][4] = { { 11, 22, 33, 44 }, { 55, 66, 77, 88 }, { 99, 110, 121, 132 } }; cout<<"배열 전체의 크기: "< =====8. 반복문으로 2차원 배열의 요소 출력하기===== 이중 for문을 이용하여 2차원 배열의 요소를 출력할 수 있다. ====8.1 순서대로 출력하기==== #include using namespace std; int main() { int numArr[3][4] = { { 11, 22, 33, 44 }, { 55, 66, 77, 88 }, { 99, 110, 121, 132 } }; int col = sizeof(numArr[0]) / sizeof(int); int row = sizeof(numArr) / sizeof(numArr[0]); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout< ====8.2 역순으로 출력하기==== #include using namespace std; int main() { int numArr[3][4] = { { 11, 22, 33, 44 }, { 55, 66, 77, 88 }, { 99, 110, 121, 132 } }; int col = sizeof(numArr[0]) / sizeof(int); int row = sizeof(numArr) / sizeof(numArr[0]); for (int i = row-1; i >= 0; i--) { for (int j = col-1; j >= 0; j--) { cout< =====9. 예제===== ====9.1. 예제(1): 입력된 점수 평균 구하기==== {{activity:public:2021:cpp:예제1.png?500}} ====9.2. 예제(2): 세 자리 자연수 A, B, C를 입력하고, A × B × C 계산 결과 0부터 9까지의 각 숫자가 얼마나 사용되는지 알아보는 프로그램 작성==== {{activity:public:2021:cpp:예제2.png?400}} ====9.3. 예제(3):배열에서 가장 큰 수 출력하기==== 배열\\ {{activity:public:2021:cpp:예제31.png?300}}\\ 결과\\ {{activity:public:2021:cpp:예제32.png?300}} ====9.4. 예제(4): 배열 요소를 오름차순으로 정렬하기(Bubble sort)==== 배열\\ {{activity:public:2021:cpp:예제41.png?300}}\\ 결과\\ {{activity:public:2021:cpp:예제42.png?200}} ====9.5. 예제(5): 전치행렬 출력하기==== {{activity:public:2021:cpp:전치행렬.png?500}}