Posts Tagged ‘C++’
Write a statement that assigns x the value k*(k-1)*(k-2)*…*3*2*1 by calling the fact function and multiplying its return value by k.
LANGUAGE: C++ CHALLENGE: Assume the availability of a function called fact. The function receives an int argument and returns an int value . If the argument is one or smaller, it returns the integer value one. Otherwise it returns the product of all the integers from one to its argument . So the value of fact(4) is 1*2*3*4 and…
Read MoreWrite a bool -function named equals that recursively determines whether its two int parameters are equal and returns true if they are and false otherwise.
LANGUAGE: C++ CHALLENGE: Two non-negative integers x and y are equal if either: Both are 0, or x-1 and y-1 are equal Write a bool -function named equals that recursively determines whether its two int parameters are equal and returns true if they are and false otherwise.
Read MoreThe elements of an integer -valued array can be set to 0 (i.e., the array can be cleared) recursively as follows: An array of size 0 is already cleared; Otherwise, set the first element of the array to 0, and clear the rest of the array Write a void function named clear that accepts an integer array, and the number of elements in the array and sets the elements of the array to 0.
LANGUAGE: C++ CHALLENGE: The elements of an integer -valued array can be set to 0 (i.e., the array can be cleared) recursively as follows: An array of size 0 is already cleared; Otherwise, set the first element of the array to 0, and clear the rest of the array Write a void function named clear that…
Read MoreWrite a recursive, string -valued function, reverse, that accepts a string and returns a new string consisting of the original string in reverse.
LANGUAGE: C++ CHALLENGE: Write a recursive, string -valued function, reverse, that accepts a string and returns a new string consisting of the original string in reverse. For example, calling reverse with the string goodbye returns the string eybdoog. Reversing a string involves: No action if the string is empty or has only 1 character (reversing a…
Read MoreWrite a recursive, int -valued function, len, that accepts a string and returns the number of characters in the string.
LANGUAGE: C++ CHALLENGE: Write a recursive, int -valued function, len, that accepts a string and returns the number of characters in the string . The length of a string is: 0 if the string is the empty string (“”). 1 more than the length of the rest of the string beyond the first character.
Read MoreDeclare an array named winarr, consisting of 3 Window objects , where the first element is an 80×20 window, the second a 10×10 window, and the third a 133×40 window.
LANGUAGE: C++ CHALLENGE: Assume a class Window with a constructor that accepts two integer arguments : width and height (in that order). Declare an array named winarr, consisting of 3 Window objects , where the first element is an 80×20 window, the second a 10×10 window, and the third a 133×40 window.
Read MoreModify the program so that the computer plays a more informed game.
LANGUAGE: C++ CHALLENGE: The computer player in Programming Project 15.5 does not play the number guessing game very well, since it makes only random guesses. Modify the program so that the computer plays a more informed game. The specific strategy is up to you, but you must add function(s) to the Player and ComputerPlayer classes…
Read Morewrite a recursive function definition for a function that has one parameter n of type int and that returns the nth Fibonacci number.
LANGUAGE: C++ CHALLENGE: write a recursive function definition for a function that has one parameter n of type int and that returns the nth Fibonacci number.
Read MoreDefine a derived class WindowWithBorder that contains a single additional integer instance variable named borderWidth, and has a constructor that accepts an integer parameter which is used to initialize the instance variable.
LANGUAGE: C++ CHALLENGE: Assume the existence of a Window class with a function getWidth that returns the width of the window. Define a derived class WindowWithBorder that contains a single additional integer instance variable named borderWidth, and has a constructor that accepts an integer parameter which is used to initialize the instance variable . There is…
Read MoreDefine a derived class , ApartmentBuilding that contains four (4) data members: an integer named numFloors, an integer named unitsPerFloor, a boolean named hasElevator, and a boolean named hasCentralAir. There is a constructor containing parameters for the initialization of the above variables (in the same order as they appear above).
LANGUAGE: C++ CHALLENGE: Assume the existence of a Building class . Define a derived class , ApartmentBuilding that contains four (4) data members: an integer named numFloors, an integer named unitsPerFloor, a boolean named hasElevator, and a boolean named hasCentralAir. There is a constructor containing parameters for the initialization of the above variables (in the same order…
Read MoreDefine a derived class , CameraPhone that contains two data members: an integer named , imageSize, representing the size in megapixels of each picture, and an integer named memorySize, representing the number of gigabytes in the camera’s memory.
LANGUAGE: C++ CHALLENGE: Assume the existence of a Phone class . Define a derived class , CameraPhone that contains two data members: an integer named , imageSize, representing the size in megapixels of each picture, and an integer named memorySize, representing the number of gigabytes in the camera’s memory. There is a constructor that accepts two integer parameters …
Read MoreWrite a recursive, bool-valued function, isPalindrome that accepts a string and returns whether the string is a palindrome.
LANGUAGE: C++ CHALLENGE: A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string ). Write a recursive, bool-valued function, isPalindrome that accepts a string and returns whether the string is a palindrome.
Read More