# In the game zone sections of chapters 6 and 8 you

• Homework Help
• 37
• 93% (61) 57 out of 61 people found this document helpful

This preview shows page 33 - 36 out of 37 pages.

17. In the Game Zone sections of Chapters 6 and 8, you designed the logic for a quiz that contains questions about a topic of your choice. Now, modify the program so it contains an array of five multiple-choice quiz questions related to the topic of your choice. Each question contains four answer choices. Also create a parallel array that holds the correct answer to each question—A, B, C, or D. In turn, pass each question to a method that displays the question and accepts the player’s answer. If the player does not enter a valid answer choice, force the player to reenter the choice. Return theuser’s valid (but not necessarily correct) answer to the main program. After the user’s answer is returned to the main program, pass it and the correct answer to a method that determines whether the values are equal and displays an appropriate message. After the user answers all five questions, display the number of correct and incorrect answers that the user chose.
33
Programming Logic and Design, 7eSolutions 9-string QUESTIONS[SIZE] = “What is the closest star to Earth?”, “What is the name of Earth’s moon?”, “How many miles (in trillions) are in a light year?”, “How many moons does Pluto have?”, “What planet is the biggest?”string CHOICES[SIZE] = “A. Scopper, B. Dipper, C. Sun, D. Moon”, “A. Luna, B. Europa, C. Sol, D. Terra”, “A. 6, B. 93, C. 10, D. 35”,“A. 6, B. 2, C. 3, D. 1”, “A. Saturn, B. Jupiter, C. Earth, D. Mars”string ANSWERS[SIZE] = “C”, “A”, “A”, “C”, “B”x = 0while x < SIZEguesses[x] = getAnswer(QUESTIONS[x], CHOICES[x])correct = correct + displayAnswer(guesses[x], ANSWERS[x])x = x + 1endwhileoutput “Number of correct=”, correctoutput “Number of incorrect=”, (SIZE – correct)stopstring getAnswer(string question, string choices)Declarationsstring answeroutput questionoutput choicesinput answerwhile (answer not equal to “A” OR answer not equal to “B” ORanswer not equal to “C” ORanswer not equal to “D”)output questionoutput choicesinput answerendwhilereturn answernum displayAnswer(string guess, string answer)Declarationsnum correct = 0if guess = answer thenoutput “Correct!”correct = 1elseoutput “The correct answer is: ”, answerendifreturn correct18. In the Game Zone section of Chapter 6, you designed the logic for the game Hangman, in which the user guesses letters in a hidden word. Improve the game to store an array of 10 words. One at a time, pass each word to a method that allows the 34
Programming Logic and Design, 7eSolutions 9-user to guess letters continuously until the game is solved. The method returns the number of guesses it took to complete the word. Store the number in an array before returning to the method for the next word. After all 10 words have been guessed, display a summary of the number of guesses required for each word as well as the average number of guesses per word.Answer: A sample solution followsPseudocode:startDeclarationsnum xnum numCorrectnum sumnum avgnum LENGTH = 10string WORDS = “COMPUTER”, “PROGRAM”, “LOGIC”, “TEST”, “LOOP”“CODE”, “MOUSE”, “DISK”, “CONTROL”, “ARRAY”num guesses[LENGTH] = 0x = 0while x < LENGTHguesses[x] = guessTheWord(WORDS[x])
• • • 