Concept explainers
What is the base case of each of the recursive functions listed in Questions 12, 13, and 14?
Base case:
When the problem is solved without performing recursion is called as a base case.
- In base case, a problem gets solved during its first attempt without getting reiterated to obtain a solution or a problem’s solution could be obtained at the initial stage.
- When a recursive function solves and returns the result without performing any recursive operation is called as Base case of a problem.
Explanation of Solution
Given code:
12.
//include the necessary headers
#include <iostream>
using namespace std ;
//function prototype
int function(int);
//main method
int main()
{
//variable declaration and definition
int x = 10;
//function call to compute the sum
cout << function(x) << endl;
//return the value to be zero
return 0;
}
//function definition
int function(int num)
{
//validates the number is less than or equal to zero
if (num <= 0)
//return the value to be zero
return 0;
else
//call the function recursively
return function(num-1) + num;
}
Explanation:
The above highlighted code is the base case in the given problem, because the value of number is validated to be zero or less than zero, the function will immediately return zero after computing the result.
13.
//include the necessary headers
#include <iostream>
using namespace std;
//function prototype
void function(int);
//main method
int main()
{
//variable declaration
int x = 10 ;
//function call
function(x);
//return the value to be zero
return 0 ;
}
//method definition that displays the given character
void function(int num)
{
//validates the number is greater than zero
if (num > 0)
{
//loop that iterates for the number of times the number that is given
for (int x = 0 ; x < num; x++)
//display the character
cout << '*';
//new line
cout << endl ;
//function call that calls iteratively to display the character in the decremented order
function(num- 1);
}
}
Explanation:
The above highlighted code is the base case for the given code because the code gets executed only when the value of the number that is passed is greater than the zero or else the code will return the zero.
14.
//include the necessary headers
#include <iostream>
#include <string>
using namespace std;
//function protytype that performs the string operations
void function(string, int, int);
//main method
int main()
{
//varible declaration and definition
string mystr = "Hello";
//display the original string
cout << mystr << endl;
//function call that performs string reverse operation
function(mystr , 0 , mystr.size());
//return the value to be zero
return 0;
}
/* function definition that performs the reverse of the string */
void function(string str , int pos, int size)
{
//compares the position of the string with its size
if (pos < size)
{
//function call that performs the string reverse
function(str, pos+ 1, size);
//display the string in reverse order
cout << str[pos];
}
}
Explanation:
The above highlighted code is the base case for the given code, because the function will perform string operation only when the size and position gets validated and if the condition fails, the control will be returned to the called function.
Want to see more full solutions like this?
Chapter 20 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Additional Engineering Textbook Solutions
HEAT+MASS TRANSFER:FUND.+APPL.
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Mechanics of Materials (10th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
- How can I resolve the following issue?arrow_forwardI need help to resolve, thank you.arrow_forwardLet the user choose encryption or decryption. For encryption, let user input the key in Hexadecimal number, the plain text in Hexadecimal number, output the ciphertext (in hexadecimal numbers). For decryption, let user input the key in Hexadecimal number, the ciphertext (in hexadecimal numbers), output the decrypted message (Hexadecimal number). Both encryption and decryption should output the different operation results for each round like the following: For example: Round 1: E(R0) = ...... (Hex or Binary) K1 = …… E(Ro) xor K1 = S-box outputs = …… f(Ro1, K1) = ….. L2 =R1 =……. La = Ra Round 2: .....• No Encryption/Decryption libraries or functions provided by the third party are allowed. Submit your program codes to Moodle with the notes of how to compile and run your program.arrow_forward
- When the given integer variable numberOfPackages is: greater than 12, output "Needs more than one box". between 5 inclusive and 12 inclusive, output "Large box". between 0 exclusive and 4 inclusive, output "Small box". less than or equal to 0, output "Invalid input". End with a newline.arrow_forwardsummarize in a short paragraph how to Advance Incident Response and Automation in ML home based security systemsarrow_forward1.[30 pts] Computers generate color pictures on a video screen or liquid crystal display by mixing three different colors of light: red, green, and blue. Imagine a simple scheme, with three different lights, each of which can be turned on or off, projecting onto a glass screen: We can create eight different colors based on the absence (0) or presence (1) of light sources R,G and B: R G B Color 0 0 0 Black 0 0 1 Blue 0 1 0 Green 0 1 1 Cyan 1 0 0 Red 1 0 1 Magenta 1 1 1 0 Yellow 1 White 1 Each of these colors can be represented as a bit vector of length 3, and we can apply Boolean operations to them. a. The complement of a color is formed by turning off the lights that are on and turning on the lights that are off. What would be the complement of each of the eight colors listed above? b. Describe the effect of applying Boolean operations on the following colors: Λ 1. Red(100) ^ Magenta(101)= Blue(001) 2. Bue(001) | Green(010)= 3. Yellow(100) & Cyan(011)= 2.[30 pts] Perform the following…arrow_forward
- D. S. Malik, Data Structures Using C++, 2nd Edition, 2010arrow_forwardMethods (Ch6) - Review 1. (The MyRoot method) Below is a manual implementation of the Math.sqrt() method in Java. There are two methods, method #1 which calculates the square root for positive integers, and method #2, which calculates the square root of positive doubles (also works for integers). public class SquareRoot { public static void main(String[] args) { } // implement a loop of your choice here // Method that calculates the square root of integer variables public static double myRoot(int number) { double root; root=number/2; double root old; do { root old root; root (root_old+number/root_old)/2; } while (Math.abs(root_old-root)>1.8E-6); return root; } // Method that calculates the square root of double variables public static double myRoot(double number) { double root; root number/2; double root_old; do { root old root; root (root_old+number/root_old)/2; while (Math.abs (root_old-root)>1.0E-6); return root; } } Program-it-Yourself: In the main method, create a program that…arrow_forwardI would like to know the main features about the following 3 key concepts:1. Backup Domain Controller (BDC)2. Access Control List (ACL)3. Dynamic Memoryarrow_forward
- In cell C21, enter a formula to calculate the number of miles you expect to drive each month. Divide the value of number of miles (cell A5 from the Data sheet) by the average MPG for the vehicle multiplied by the price of a gallon of gas (cell A6 from the Data sheet).arrow_forwardMicrosoft Excelarrow_forwardIn cell C16, enter a formula to calculate the price of the vehicle minus your available cash (from cell A3 in the Data worksheet). Use absolute references where appropriate—you will be copying this formula across the row what fomula would i use and how do i solve itarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage Learning