Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 35

(Computers in Education) Computers are playing an increasing role in education. Write a program that helps an elementary school student learn multiplication. Use rand to produce two positive one-digit integers. It should then type a question such as How much is 6 times \(7 ?\) The student then types the answer. Your program checks the student's answer. If it is correct, print "very good!", then ask another multiplication question. If the answer is wrong. print "No. Please try again.", then let the student try the same question repeatedly until the student finally gets it right.

Problem 38

(Guess the Number Game) Write a program that plays the game of "guess the number" as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000 . The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type your first guess. The player then types a first guess. The program responds with one of the following: 1\. Excellent! You guessed the number! Would you like to play again (y or n)? 2\. Too low. Try again. 3\. Too high. Try again. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player too high or Too low to help the player "zero in" on the correct answer.

Problem 40

For example, power \((3,4)=3 * 3 * 3 * 3 .\) Assume that exponent is an integer greater than or equal to \(1 .\) Hint: The recursion step would use the relationship base exponent \(=\) base \(\cdot\) base exponent -1 and the terminating condition occurs when exponent is equal to \(1,\) because base \(^{1}=\) base

Problem 43

Any program that can be implemented recursively can be implemented iteratively. although sometimes with more difficulty and less clarity. Try writing an iterative version of the Towers of Hanoi. If you succeed, compare your iterative version with the recursive version developed in Exercise 6.42 . Investigate issues of performance, clarity and your ability to demonstrate the correctness of the programs.

Problem 45

(Recursive Greatest Common Divisor) The greatest common divisor of integers \(x\) and \(y\) is the largest integer that evenly divides both \(x\) and \(y .\) Write a recursive function gcd that returns the greatest common divisor of \(x\) and \(y,\) defined recursively as follows: If y is equal to \(0,\) then \(\operatorname{gcd}(x, y)\) is \(x ;\) otherwise, gcd \((x, y)\) is gcd \((y, x \% y),\) where g is the modulus operator. [ Note: For this algorithm, x must be larger than y.]

Problem 46

Can main be called recursively on your system? Write a program containing a function main. Include static local variable count and initialize it to 1\. Postincrement and print the value of count each time main is called. Compile your program. What happens?

Problem 48

Write function distance that calculates the distance between two points \((x 1, y 1)\) and \((x 2,\) \(y 2\) ). All numbers and return values should be of type double.

Problem 49

include 4 using std::cin; 5 using std::cout… # What is wrong with the following program? 1 // Exercise 6.49: ex06_49.cpp 2 // What is wrong with this program? 3 #include 4 using std::cin; 5 using std::cout; 6 7 int main() 8 { 9 int c; 10 11 if ( ( c = cin.get() ) != EOF ) 12 { 13 main(); 14 cout << c; 15 } // end if 16 17 return 0; // indicates successful termination 18 } // end main

Problem 50

include 4 using std::cout; 5 using std::cin; 6 using st… # What does the following program do? 1 // Exercise 6.50: ex06_50.cpp 2 // What does this program do? 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 int mystery( int, int ); // function prototype 9 10 int main() 11 { 12 int x, y; 13 14 cout << "Enter two integers: "; 15 cin >> x >> y; 16 cout << "The result is " << mystery( x, y ) << endl; 17 18 return 0; // indicates successful termination 19 } // end main 20 21 // Parameter b must be a positive integer to prevent infinite recursion 22 int mystery( int a, int b ) 23 { 24 if ( b == 1 ) // base case 25 return a; 26 else // recursion step 27 return a + mystery( a, b - 1 ); 28 } // end function mystery

Problem 53

Find the error in each of the following program segments and explain how to correct it: a. float cube( float ); // function prototype double cube( float number ) // function definition { return number * number * number; } b. register auto int x = 7; c. int randomNumber = srand(); d. float y = 123.45678; int x; x = y; cout << static_cast < float > ( x ) << endl; e. double square( double number ) { double number; return number * number; } f.int sum( int n ) { if ( n == 0 ) return 0; else return n + sum( n ); }

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks