분류 전체보기
-
class_lab10.cpp 포인터의 개념C++/class 2022. 5. 25. 17:47
포인터의 주 사용 목적 1. 용량이 큰 데이터를 전달하는 경우 -> 비용이 큼 데이터 전달이 아니라 데이터 주소만 전달하기 때문에 시간, 메모리 등 비용적인 측면에서 효율적 2. call by value 함수의 매개변수로 어떤 값을 전달할 때 함수 내에서 매개변수를 수정하는 경우 변경되지 않음 (main함수 내에서는 변경O) ex) main 함수 내에서의 x,y -> 함수에서 x++, y++을 해도 돌아왔을 때 변화X 그동안은 전역변수를 선언해서 사용하는 편법(?)을 이용했지만 포인터로 해결! #include #include using namespace std; /* 1. 포인터 변수란? 주소를 가지고 있는 변수 변수, 베열은 RAM(주기억장치)에 저장된다. int i의 메모리를 어디에 할당할지는(주소)..
-
[C++] find_max_n_min.cpp 최댓값, 최솟값 구하는 프로그램(배열 활용)C++/clone code 2022. 5. 23. 15:40
[프로그램] 1. 사용자로부터 배열의 크기를 입력받는다. 2. 앞서 입력받은 배열의 크기만큼 정수를 입력받는다. 3. 값들 중에서 최댓값, 최솟값을 구한다. ** 일반적인 방식, 함수를 사용한 방식 두 가지로 구현하기 [일반 방식] #include using namespace std; const int SIZE = 50; //배열의 크기를 기호상수로 선언 int main(){ int array[SIZE]; //50 int max; int min; int size; cout > size; //배열의 크기 입력 // Input array elements cout max) max = array[i]; if (array[i] < min) min = array[i]; } cout
-
[C++] 2차원 배열의 이해(two dimensional array)C++/clone code 2022. 5. 23. 14:21
[목표] 2차원 배열을 함수의 매개변수로 사용한다. #include using namespace std; //기호상수 선언 const int YEARS = 3; const int PRODUCTS = 5; // 함수의 매개변수로 2차원 배열 // 맨 앞 []인덱스는 공백으로 놔야한다! (포인터 개념) int sum(int grade[][PRODUCTS]); int main() { int sales[YEARS][PRODUCTS] = { {1,2,3}, {4,5,6}, {7,8,9}, }; //배열의 원소 중괄호 끝에는 세미콜론을 꼭 붙여야 한다. int total_sale = 0; total_sale = sum(sales); cout
-
[C++] average.cpp 배열을 활용한 평균 계산C++/clone code 2022. 5. 22. 22:25
Power C++ p.218 학생들의 성적을 저장하고 있는 정수 배열을 만들고 평균 계산하는 프로그램 // // main.cpp // average // 학생들의 성적을 저장하고 있는 정수 배열을 만들고 평균을 계산하는 함수 제작 // Created by yeonsu on 2022/05/22. // #include using namespace std; //함수 원형 선언 int average(int score[], int n); int main(){ const int STUDENTS = 5; //배열의 크기 int grade[STUDENTS] = {1,2,3,4,5}; //배열 선언 int avg; avg = average(grade, STUDENTS); cout
-
[C++] frequency.cpp 데이터의 빈도수 계산C++/clone code 2022. 5. 22. 21:43
Power C++ p.215 데이터의 빈도수 계산하는 프로그램 작성 // // main.cpp // frequency // 데이터 빈도를 계산하는 프로그램 // Created by yeonsu on 2022/05/22. // #include using namespace std; int main(){ const int SIZE = 5; //0~4까지의 범위 int freq[SIZE]; //배열 선언 int score; for (int i = 0; i score; if (score < 0) break; //사용자가 음수를 입력할 경우 종료 fre..
-
[C++] combination.cpp 조합값 계산하기C++/clone code 2022. 5. 22. 21:09
Power C++ 교재 151p. 수학적인 조합 값을 구하는 예제 [조건] 사용자 정의 함수가 다른 사용자 정의 함수를 호출 // // main.cpp // function // // Created by yeonsu on 2022/05/22. // #include using namespace std; /*함수 원형 정의*/ int get_integer(void); //사용자로부터 정수를 입력받는 함수 int combination (int n, int r); //조합 계산 함수 int factorial(int n); //팩토리얼 계산 함수 int main(){ /*정수를 입력받는 함수의 반환값을 변수 a,b에 저장*/ int a, b; a = get_integer(); b = get_integer(); c..