Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 14.3, Problem 15STE
Explanation of Solution
Recursive function definition for “squares” function:
The recursive function definition for “squares” function is shown below:
//Function definition for "squares" function
int squares(int n)
{
/* If "n" is less than or equal to "1" */
if (n <= 1)
//Returns "1"
return 1;
//Otherwise
else
/* Recursively call the "squares" function with subtracting the value of "n" by "1" and then add and multiplied by "n" */
return (squares(n-1) + n * n);
}
Explanation:
The above function is used to compute the sum of the squares of numbers from “1” to “n”.
- In this function, first check the value of “n”. If the value of “n” is less than or equal to “1”, then returns “1”.
- Otherwise, recursively call the “squares” function with subtracting the value of “n” by “1” and then add and multiplied by “n”...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a recursive function definition for the following function: int squares(int n); //Precondition: n >= 1 //Returns the sum of the squares of numbers 1 through n. For example, squares(3) returns 14 because 12 + 22 + 32 is 14.
Write a recursive function PrintPattern2 that receives two arguments: (i) a character ch; (ii) number of lines and print the pattern.Example:PrintPattern2('*',5) will print the following pattern
Write a recursive function called Mod which takes two positive integers argument (aand b) and returns a % b – the remainder of a dividing by b. You may not use a remainderoperator or built-in function.
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forwardC++ Programming: Write a recursive function, sumDigits, that takes an integer as a parameter and returns the sum of the digits of the integer. Also, write a program to test your function. Your program should prompt the user for an integer and output the results to the console.arrow_forwardWrite a recursive function diff which takes two positive integer arguments (x and y) and returns |x – y|. You may not perform an x - y calculation anywhere in the function! Example calls: diff(3, 7) returns 4; diff(7,3) returns 4 In JAVAarrow_forward
- Write a function prime() to generate series of prime numbers. The function will accept the integer that represent the number of prime terms to be generated between 0 to 100. Input is given by user. Also, Write a function convertToBinary() that will accept a number and will return a binary number. The function should be recursive function. C++ programarrow_forwardC++ write about the iterative function factorial to a recursive function. function shall calculate the faculty for a number, e.g.factorial (3) -> 6 Input: 0 output: 1input: 3 output: 6 The code : int factorial (int n) {int f = 1;for (int i = n; i> 1; i -) {f * = i; }return f;}int main () {// get input from cin // call your function and send results to cout return 0;}arrow_forwardWrite a recursive findSum function which returns the sum of all integers between (and including) two integers given as parametrs. Write a C program that tests function findSum. (Your function must be recursive.) (Examples: findSum(13, 47) returns 1050, findSum(-2, 89) returns 4002)arrow_forward
- Write a recursive C function that returns the uppercase letters of the string named str by storing them in the cap variable. The prototype of the function should be: void F (char * str, * char cap); we use devC ASAParrow_forward1. Write a recursive function that takes a string as an input and returns the reverse of the string. 2.Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function. Method call rec_string(‘abcde’), will produce the following output: *ede cde bcde abcde Method call rec_string(‘abc’), will produce the following output: *cbc abc 3. Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an important problem that arises in many commercial applications. We can efficiently compute the gcd using the…arrow_forwardWrite a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, then the pattern generated is:********************Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the above pattern.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr