C++ Programming Ch.3 Exercise 6 Solution
Problem: λ¬Έμ 5λ²μ μ°Έκ³ νμ¬ μ§μ μ μλ§ λλ€νκ² λ°μμν€λ EvenRandom ν΄λμ€λ₯Ό μμ±νκ³ EvenRandom ν΄λμ€λ₯Ό μ΄μ©νμ¬ 10κ°μ μ§μλ₯Ό λλ€νκ² μΆλ ₯νλ νλ‘κ·Έλ¨μ μμ±νλΌ. 0λ μ§μλ‘ μ²λ¦¬νλ€. Objective & Hints: ν΄λμ€ μμ± λ° ν΄λμ€ νμ© Execution Result: 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include #include #include using namespace std; class EvenRandom{ public: EvenRandom(); int next(); int nextInRange(int a, int b); }; EvenRandom::EvenRandom(){ srand((unsigned)time(0)); } int EvenRandom::next() { int ran = rand(); if(ran%2 == 0) return ran; else return --ran; } int EvenRandom::nextInRange(int a, int b){ int ran = rand()%(b-a+1) + a; if(ran%2 == 0) return ran; else return --ran; } int main(){ EvenRandom r; cout << "-- 0μμ " << RAND_MAX << "κΉμ§μ λλ€ μ μ 10 κ°--" << endl; for(int i=0; i<10; i++) { int n = r.next(); // 0μμ RAND_MAX(32767) μ¬μ΄μ λλ€ν μ μ cout << n << ' '; } cout << endl << endl << "-- 2μμ " << "10 κΉμ§μ λλ€ μ μ 10 κ° --" << endl; for(int i=0; i<10; i++) { int n = r.nextInRange(2,10); // 2μμ 10 μ¬μ΄μ λλ€ν μ μ cout << n << ' '; } cout << endl; } Explanation: ...