Homework 7 solution UCR CS10: Introduction to Computer Science Spring Quarter 2004, Lecturer: Kris Miller Due Wednesday, May 26 before 8:00pm by web turnin. ------------------------------------------------------------------------------- Turnin: Turnin this assignment to the folder, hw7, using the electronic turnin linked to from the class webpage. For homeworks only, you may turnin .doc (Microsoft Word) or .txt (Notepad) files. You may also turnin the homework as a .cc or .cpp (C++ source) file. Be sure to turn it into the correct folder. At best, you will be penalized 20%. At worst, your homework may not be graded at all. ------------------------------------------------------------------------------- Note: Collaboration policy for this homework assignment Collaboration on this homework is strongly ENCOURAGED. The homework is intended for practice, not assessment -- most people who turn in decent work should get most if not all points for this assignment. Forming study groups is strongly encouraged. You shouldn't simply copy answers from any source -- each other, the book, the web, other books, previous solutions, etc. But you can certainly discuss how to solve the problems, look over each others solutions and help "debug" them, compare answers and redo problems if you determine your answer is wrong, and most importantly, explain concepts related to the problems and solutions. ------------------------------------------------------------------------------- 0. For 1 point bonus (20%), enter the names (first and last name) of the CS10 students you collaborated on this homework with. 1. Self-test Exercise #6-8 (Chapter 6, Exercise #8) 2. Self-test Exercise #7-1 (Chapter 7, Exercise #1) 3. Self-test Exercise #7-4 4. Self-test Exercise #7-6 5. Self-test Exercise #7-8 6. Self-test Exercise #7-9 7. Self-test Exercise #7-28 8. Self-test Exercise #7-29 9. Self-test Exercise #7-33 ***Answers to self-test exercises are in the back of each chapter.*** 10. CodeMate Programming Project #1 (Chapter 7) ***Solution*** // -------------------------------- // ----- ENTER YOUR CODE HERE ----- // -------------------------------- // The outer if structure takes care of all player1Choice possibilities. // The nested if structures take care of all player2Choice possibilities // for each of player1Choice possibilities. // The getPlayerChoice function ensures that the characters 'P', 'p', // 'R', 'r', 'S', and 's' are the only possible values of player1Choice // and player2Choice. if (player1Choice == 'P' || player1Choice == 'p') { if (player2Choice == 'P' || player2Choice == 'p') { cout << "Nobody wins\n"; } else if (player2Choice == 'R' || player2Choice == 'r') { cout << "Player 1 wins: paper covers rock\n"; } else // player2Choice is 'S' or 's' { cout << "Player 2 wins: scissors cut paper\n"; } } else if (player1Choice == 'R' || player1Choice == 'r') { if (player2Choice == 'P' || player2Choice == 'p') { cout << "Player 2 wins: paper covers rock\n"; } else if (player2Choice == 'R' || player2Choice == 'r') { cout << "Nobody wins\n"; } else // player2Choice is 'S' or 's' { cout << "Player 1 wins: rock breaks scissors\n"; } } else // player1Choice is 'S' or 's' { if (player2Choice == 'P' || player2Choice == 'p') { cout << "Player 1 wins: scissors cut paper\n"; } else if (player2Choice == 'R' || player2Choice == 'r') { cout << "Player 2 wins: rock breaks scissors\n"; } else // player2Choice is 'S' or 's' { cout << "Nobody wins\n"; } } // -------------------------------- // --------- END USER CODE -------- // --------------------------------