C++ Programming Ch.10 Exercise 3 Solution

Problem: λ°°μ—΄μ˜ μ›μ†Œλ₯Ό λ°˜λŒ€ μˆœμ„œλ‘œ λ’€μ§‘λŠ” reverseArray() ν•¨μˆ˜λ₯Ό ν…œν”Œλ¦ΏμœΌλ‘œ μž‘μ„±ν•˜λΌ. reverseArray()의 첫 번째 맀개 λ³€μˆ˜λŠ” 배열에 λŒ€ν•œ 포인터이며 두 번째 맀개 λ³€μˆ˜λŠ” λ°°μ—΄μ˜ κ°œμˆ˜μ΄λ‹€. reverseArray()의 호좜 μ‚¬λ‘€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 int x[] = { 1, 10, 100, 5, 4}; reverseArray(x, 5); for(int i=0; i<5; i++) cout << x[i] << ' '; // 4 5 100 10 1이 좜λ ₯λœλ‹€. Execution Result: Objective & Hints: ν…œν”Œλ¦Ώ ν•¨μˆ˜ λ§Œλ“€κΈ° ...

March 9, 2020 Β· 1 min Β· Sobamemil

C++ Programming Ch.10 Exercise 2 Solution

Problem: 두 개의 배열을 λΉ„κ΅ν•˜μ—¬ κ°™μœΌλ©΄ trueλ₯Ό, μ•„λ‹ˆλ©΄ falseλ₯Ό λ¦¬ν„΄ν•˜λŠ” μ œλ„€λ¦­ ν•¨μˆ˜ equalArrays()λ₯Ό μž‘μ„±ν•˜λΌ. λ˜ν•œ main() ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬ equalArrays()λ₯Ό ν˜ΈμΆœν•˜λŠ” λͺ‡ κ°€μ§€ 사둀λ₯Ό 보여라. equalArrays()λ₯Ό ν˜ΈμΆœν•˜λŠ” μ½”λ“œ μ‚¬λ‘€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 int x[] = {1, 10, 100, 5, 4}; int y[] = {1, 10, 100, 5, 4}; if(equalArray(x, y, 5)) cout << "κ°™λ‹€"; // λ°°μ—΄ x, yκ°€ κ°™μœΌλ―€λ‘œ "κ°™λ‹€" 좜λ ₯ else cout << "λ‹€λ₯΄λ‹€"; Execution Result: ...

March 9, 2020 Β· 1 min Β· Sobamemil

C++ Programming Ch.10 Exercise 1 Solution

Problem: 배열을 λ°›μ•„ κ°€μž₯ 큰 값을 λ¦¬ν„΄ν•˜λŠ” μ œλ„€λ¦­ ν•¨μˆ˜ biggest()λ₯Ό μž‘μ„±ν•˜λΌ. λ˜ν•œ main() ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬ biggest()λ₯Ό ν˜ΈμΆœν•˜λŠ” λͺ‡ κ°€μ§€ 사둀λ₯Ό 보여라. Execution Result: Objective & Hints: ν…œν”Œλ¦Ώ ν•¨μˆ˜ λ§Œλ“€κΈ° biggest()λ₯Ό ν˜ΈμΆœν•˜λŠ” μ½”λ“œ μ‚¬λ‘€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 int x[] = {1, 10, 100, 5, 4}; cout << biggest(x, 5) << endl; // 5λŠ” λ°°μ—΄ x의 크기. 100이 좜λ ₯λœλ‹€. Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include using namespace std; template T biggest(T a[], T n){ T big = a[0]; // λ°°μ—΄ a의 첫번째 μ›μ†Œλ₯Ό big에 μ‚½μž… for(T i=1; i<n; i++) big = big < a[i] ? a[i] : big; // big보닀 a[i]의 값이 더 크면 big에 a[i]의 값을 μ‚½μž… return big; } int main() { int big = 0; int x[] = {1, 10, 100, 5, 4}; cout << biggest(x, 5) << endl; } Explanation: ...

March 9, 2020 Β· 1 min Β· Sobamemil

C++ Programming Ch.8 Exercise 9 Solution

Problem: λΉ„ν–‰κΈ° μ˜ˆμ•½ ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜λΌ. 이 λ¬Έμ œλŠ” μ—¬λŸ¬ 개의 ν΄λž˜μŠ€μ™€ 객체듀을 λ‹€λ£¨λŠ” μ—°μŠ΅μ„ μœ„ν•œ 것이닀. 클래슀 μ‚¬μ΄μ˜ 상속 κ΄€κ³„λŠ” μ—†λ‹€. 항곡사 이름은 'ν•œμ„±ν•­κ³΅'이고, 8개의 μ’Œμ„μ„ κ°€μ§„ 3λŒ€μ˜ λΉ„ν–‰κΈ°λ‘œ μ„œμšΈ λΆ€μ‚° κ°„ μš΄ν•­ 사업을 ν•œλ‹€. 각 λΉ„ν–‰κΈ°λŠ” ν•˜λ£¨μ— ν•œ 번만 μš΄ν•­ν•˜λ©° λΉ„ν–‰μ‹œκ°„μ€ 07μ‹œ, 12μ‹œ, 17μ‹œμ΄λ‹€. λΉ„ν–‰κΈ° μ˜ˆμ•½ ν”„λ‘œκ·Έλž¨μ€ λ‹€μŒμ˜ κΈ°λŠ₯을 κ°€μ§„λ‹€. ● μ˜ˆμ•½ : λΉ„ν–‰ μ‹œκ°„, μ‚¬μš©μžμ˜ 이름, μ’Œμ„ 번호λ₯Ό μž…λ ₯λ°›μ•„ μ˜ˆμ•½ν•œλ‹€. ● μ·¨μ†Œ : λΉ„ν–‰ μ‹œκ°„, μ‚¬μš©μžμ˜ 이름, μ’Œμ„ 번호λ₯Ό μž…λ ₯λ°›κ³  μ˜ˆμ•½μ„ μ·¨μ†Œν•œλ‹€. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 8 Solution

Problem: λ‹€μŒ κ·Έλ¦Όκ³Ό 같은 상속 ꡬ쑰λ₯Ό κ°–λŠ” 클래슀λ₯Ό μ„€κ³„ν•œλ‹€. λͺ¨λ“  ν”„λ¦°ν„°λŠ” λͺ¨λΈλͺ…(model), μ œμ‘°μ‚¬(manufacturer), 인쇄 맀수(printedCount), 인쇄 쒅이 μž”λŸ‰(availableCount)을 λ‚˜νƒ€λ‚΄λŠ” 정보와 print(int pages) 멀버 ν•¨μˆ˜λ₯Ό κ°€μ§€λ©°, print()κ°€ ν˜ΈμΆœν•  λ•Œλ§ˆλ‹€ pages 맀의 μš©μ§€λ₯Ό μ‚¬μš©ν•œλ‹€. μž‰ν¬μ ― ν”„λ¦°ν„°λŠ” μž‰ν¬ μž”λŸ‰(availableInk) 정보와 printInkJet(int pages) 멀버 ν•¨μˆ˜λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§€λ©°, λ ˆμ΄μ € ν”„λ¦°ν„°λŠ” ν† λ„ˆ μž”λŸ‰(availableToner) 정보와 μ—­μ‹œ printLaser(int pages) 멀버 ν•¨μˆ˜λ₯Ό μΆ”κ°€μ μœΌλ‘œ κ°€μ§„λ‹€. 각 ν΄λž˜μŠ€μ— μ μ ˆν•œ μ ‘κ·Ό μ§€μ •μœΌλ‘œ 멀버 λ³€μˆ˜μ™€ ν•¨μˆ˜, μƒμ„±μž, μ†Œλ©Έμžλ₯Ό μž‘μ„±ν•˜κ³ , λ‹€μŒκ³Ό 같이 μ‹€ν–‰λ˜λ„λ‘ 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 7 Solution

Problem: μ•„λž˜μ™€ 같은 BaseMemory 클래슀λ₯Ό μƒμ†λ°›λŠ” ROM(Read Only Memory), RAM 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. BaseMemory에 ν•„μš”ν•œ μ½”λ“œλ₯Ό μˆ˜μ • μΆ”κ°€ν•˜μ—¬ 적절히 μ™„μ„±ν•˜λΌ. 1 2 3 4 5 class BaseMemory{ char *mem; protected: BaseMemory(int size) { mem = new char [size]; } }; ROM은 읽기 μ „μš© λ©”λͺ¨λ¦¬μ΄λ―€λ‘œ μž‘λ™ 쀑에 값을 μ“Έ μˆ˜κ°€ μ—†κΈ° λ•Œλ¬Έμ—, 곡μž₯μ—μ„œ 생산할 λ•Œ μƒμ‚°μžκ°€ μš”μ²­ν•œ λ°μ΄ν„°λ‘œ μ΄ˆκΈ°ν™” ν•˜λŠ”λ° 이 μž‘μ—…μ„ κ΅½λŠ”λ‹€(burn)라고 ν•œλ‹€. κ·ΈλŸ¬λ―€λ‘œ ROM은 λ°˜λ“œμ‹œ μƒμ„±μžμ—μ„œ burn μž‘μ—…μ΄ μΌμ–΄λ‚˜μ•Ό ν•œλ‹€. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 6 Solution

Problem: 문제 5~6에 μ μš©λ˜λŠ” BaseArray ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // λ°°μ—΄μ˜ 크기 int *mem; // μ •μˆ˜ 배열을 λ§Œλ“€κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬μ˜ 포인터 protected: // μƒμ„±μžκ°€ protected BaseArray(int capacity=100){ this->capacity = capacity; mem = new int [capacity]; } ~BaseArray() { delete [] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; Write a μŠ€νƒμœΌλ‘œ μž‘λ™ν•˜λŠ” MyStack class that inherits from the BaseArray class. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 5 Solution

Problem: 문제 5~6에 μ μš©λ˜λŠ” BaseArray ν΄λž˜μŠ€λŠ” λ‹€μŒκ³Ό κ°™λ‹€. 1 2 3 4 5 6 7 8 9 10 11 12 class BaseArray { int capacity; // λ°°μ—΄μ˜ 크기 int *mem; // μ •μˆ˜ 배열을 λ§Œλ“€κΈ° μœ„ν•œ λ©”λͺ¨λ¦¬μ˜ 포인터 protected: // μƒμ„±μžκ°€ protected BaseArray(int capacity=100){ this->capacity = capacity; mem = new int [capacity]; } ~BaseArray() { delete [] mem; } void put(int index, int val) { mem[index] = val; } int get(int index) { return mem[index]; } int getCapacity() { return capacity; } }; BaseArrayλ₯Ό 상속받아 큐처럼 μž‘λ™ν•˜λŠ” MyQueue 클래슀λ₯Ό μž‘μ„±ν•˜λΌ. ...

March 9, 2020 Β· 3 min Β· Sobamemil

C++ Programming Ch.8 Exercise 4 Solution

Problem: 문제 3~4에 μ μš©λ˜λŠ” 2차원 μƒμ˜ ν•œ 점을 ν‘œν˜„ν•˜λŠ” Point ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Point { int x,y; public: point(int x, int y) { this->x = x; this->y = y; } int getX(){ return x; } int getY(){ return y; } protected: void move(int x, int y) { this->x = x; this->y = y; } }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Point 클래슀λ₯Ό μƒμ†λ°›λŠ” ColorPoint 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 9, 2020 Β· 2 min Β· Sobamemil

C++ Programming Ch.8 Exercise 3 Solution

Problem: 문제 3~4에 μ μš©λ˜λŠ” 2차원 μƒμ˜ ν•œ 점을 ν‘œν˜„ν•˜λŠ” Point ν΄λž˜μŠ€κ°€ μžˆλ‹€. 1 2 3 4 5 6 7 8 9 class Point { int x,y; public: point(int x, int y) { this->x = x; this->y = y; } int getX(){ return x; } int getY(){ return y; } protected: void move(int x, int y) { this->x = x; this->y = y; } }; λ‹€μŒ main() ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ Point 클래슀λ₯Ό 상속받은 ColorPoint 클래슀λ₯Ό μž‘μ„±ν•˜κ³ , 전체 ν”„λ‘œκ·Έλž¨μ„ μ™„μ„±ν•˜λΌ. ...

March 9, 2020 Β· 2 min Β· Sobamemil