Step 1: In this experiment you will learn how to declare functions. Enter, save,
ID: 3763034 • Letter: S
Question
Step 1: In this experiment you will learn how to declare functions.
Enter, save, compile and execute the following program in MSVS. Call the new directory “recursionExp1” and the program “recursion1.cpp”. Answer the questions below:
#include <iostream>
using namespace std;
void recursive_countdown(int count)
{
if (count == 0)
cout<<"count="<<count<<endl;
else
{
cout<<"count="<<count<<endl;
recursive_countdown(--count);
}
}
int main(void)
{
int count = 10;
recursive_countdown(count);
return 0;
}
Question 1:What does the program do?
Question 2:Please write the base case?
Question 3:How many times is “recursive_countdown” called recursively?
Question 4:Please rewrite the program in Step 1 using an iterative function to countdown? Call the function “iterative_countdown”.
Question 5:Which type of function (recursive or iterative) executes the fastest? Please explain?
Step 2: In this experiment you will learn how to declare functions.
Enter, save, compile and execute the following program in MSVS. Call the new directory
“recursionExp2” and the program “recursion2.cpp”. Answer the question below:
#include <iostream>
using namespace std;
int R_power(int count, const int & base)
{
if (count == 0)
return 1;
else
return base * R_power(count-1,base);
}
int I_power(int count, const int & base)
{
int multiend = 1;
while (count > 0)
{
multiend *= base;
count--;
}
return multiend;
}
int main()
{
int count = 10;
int base = 2;
cout<<R_power(count,base)<<endl;
cout<<I_power(count,base)<<endl;
return 0;
}
Question 6:Please explain the operation of the functions “R_power” and “I_Power”?
Explanation / Answer
STEP 1:
Q1) Program would print 0 to 10 in reverse order (descending order)
Q2) No Classes are used here
Q3) Once from main and 10 times from recursive function/method
Q4) Code:
void iterative_countdown(int count)
{
while (count>=0)
{
cout<<"count="<<count<<endl;
count--;
}
}
q5) Itertive is much faster since the value/objects/classes need not be created
and destroyed; also the thread is simple no depth in thread pid/thread chain need
to be created.
STEP 2:
q6) R_Power and I_Power functions calculate the base^count value. Here it is 2^10 = 1024.
R_power follows recursive method - calling the function itself untill the value is 0. Every time it calls it self the power (count is reduced by 1).
Steps:
2 * 512 = 1024
2 * 256
2 * 128
2 * 64
2 * 32
2 * 16
2 * 8
2 * 4
2 * 2 - Last Recursion
I_Power follows Iteration method - Repeating given steps till the condition is met. I this case the condition is count should be greater than 0 to do multiplication.
Steps
Multiend initial value is 1
Base Count Multiend
2 10 2
2 9 4
2 8 8
2 7 16
2 6 32
2 5 64
2 4 128
2 3 256
2 2 512
2 1 1024 - Stops here since count is >0
2 0 - Does not execute since count is <=0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.