πŸ’» Dev & CSC++ Programming

C++ Programming Ch.10 Exercise 5 Solution

Problem: λ‹€μŒ ν•¨μˆ˜λŠ” 맀개 λ³€μˆ˜λ‘œ μ£Όμ–΄μ§„ 두 개의 int 배열을 μ—°κ²°ν•œ μƒˆλ‘œμš΄ int 배열을 동적 ν• λ‹Ήλ°›μ•„ λ¦¬ν„΄ν•œλ‹€. 1 int * concat(int a[], int sizea, int b[], int sizeb); concatκ°€ int 배열뿐 μ•„λ‹ˆλΌ λ‹€λ₯Έ νƒ€μž…μ˜ 배열도 μ²˜λ¦¬ν•  수 μžˆλ„λ‘ μΌλ°˜ν™”ν•˜λΌ. Execution Result: Objective & Hints: ν•¨μˆ˜μ˜ νžλ°˜ν™”μ— λŒ€ν•œ 이해, ν…œν”Œλ¦Ώ ν•¨μˆ˜ λ§Œλ“€κΈ° Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include using namespace std; template T* concat(T a[], int sizea, T b[], int sizeb){ T *rArray = new T[sizea + sizeb]; // return ν•  배열을 동적생성 for(int i=0; i<sizea+sizeb; i++){ if(i<sizea) rArray[i] = a[i]; else rArray[i] = b[i-sizea]; } return rArray; } int main() { int x[] = { 1, 10, 100, 5, 4 }; int y[] = { 7, 6, 10, 9 }; int *a = concat(x, 5, y, 4); int aSize = sizeof(x)/sizeof(x[0]) + sizeof(y)/sizeof(y[0]); // a에 λ“€μ–΄μžˆλŠ” μ›μ†Œμ˜ 개수 for (int i = 0; i<aSize; i++) cout << a[i] << ' '; }

March 9, 2020 Β· 1 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.6 Exercise 3 Solution

Problem: ν•¨μˆ˜ big()을 ν˜ΈμΆœν•˜λŠ” κ²½μš°λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 int main() { int x = big(3, 5); // 3κ³Ό 5쀑 큰 값은 5λŠ” μ΅œλŒ€κ°’ 100보닀 μž‘μœΌλ―€λ‘œ, 5 리턴 int y = big(300, 60); // 300κ³Ό 60 쀑 큰 κ°’ 300이 μ΅œλŒ€κ°’ 100보닀 ν¬λ―€λ‘œ, 100리턴 int z = big(30, 60, 50); // 30κ³Ό 60 쀑 큰 κ°’ 60이 μ΅œλŒ€κ°’ 50보닀 ν¬λ―€λ‘œ, 50리턴 cout << x << ' ' << y << ' ' << z << endl; } (1) big() ν•¨μˆ˜λ₯Ό 2개 μ€‘λ³΅ν•˜μ—¬ μž‘μ„±ν•˜κ³  ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 5, 2020 Β· 2 min Β· Sobamemil
πŸ’» Dev & CSC++ Programming

C++ Programming Ch.3 Exercise 3 Solution

Problem: μ€ν–‰μ—μ„œ μ‚¬μš©ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜κΈ° μœ„ν•΄, 은행 κ³„μ’Œ ν•˜λ‚˜λ₯Ό ν‘œν˜„ν•˜λŠ” 클래슀 Accountκ°€ ν•„μš”ν•˜λ‹€. κ³„μ’Œ μ •λ³΄λŠ” κ³„μ’Œμ˜ 주인, κ³„μ’Œ 번호, μž”μ•‘μ„ λ‚˜νƒ€λ‚΄λŠ” 3 멀버 λ³€μˆ˜λ‘œ 이루어진닀. main() ν•¨μˆ˜μ˜ μ‹€ν–‰κ³Ό κ²°κ³Όκ°€ λ‹€μŒκ³Ό 같도둝 Account 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. 1 2 3 4 5 6 7 8 9 10 #include using namespace std; int main() { Account a("Kitae", 1, 5000); // id 1번, μž”μ•‘ 5000원, 이름이 Kitae인 κ³„μ’Œ μ„± a.deposit(50000); // 50000원 μ €κΈˆ cout << a.getOwner() << "의 μž”μ•‘μ€ " << a.inquiry() << endl; int money = a.withdraw(20000); // 20000원 좜금. withdraw()λŠ” μΆœκΈˆν•œ μ‹€μ œ κΈˆμ•‘ 리턴 cout << a.getOwner() << "의 μž”μ•‘μ€ " << a.inquiry() << endl; } Execution Result: ...

March 2, 2020 Β· 2 min Β· Sobamemil