Write a C++ program, no need to be object-oriented, to test the following three functions. Each functionreceives one parameter n, which is a positive integer.int F1(int n){if (nif (n % 2int result0) return 1;0) {F1(n / 2);return result * result;else1);return 2 * F1(n}int F2(int n){if (nreturn 2 * F2(n - 1);0) return 1;int F3(int n){if (n0) return 1;return F3(n - 1) + F3(n - 1);}Test the functions by calling them with the following values for n: 1, 3, 5, 16, 24, 26, 30.Submit your test program and in your report answer the following questions:a. What does each function return as a function of input parameter n?b. Which function is the worst in terms of time efficiency? Support your answer by stating, withoutproof, the running time growth rate, as a function of n, of each of the above functions.

Question
Asked Feb 7, 2020
66 views

Please help me with question b to find the running time. Thank you.

Write a C++ program, no need to be object-oriented, to test the following three functions. Each function
receives one parameter n, which is a positive integer.
int F1(int n)
{
if (n
if (n % 2
int result
0) return 1;
0) {
F1(n / 2);
return result * result;
else
1);
return 2 * F1(n
}
int F2(int n)
{
if (n
return 2 * F2(n - 1);
0) return 1;
int F3(int n)
{
if (n
0) return 1;
return F3(n - 1) + F3(n - 1);
}
Test the functions by calling them with the following values for n: 1, 3, 5, 16, 24, 26, 30.
Submit your test program and in your report answer the following questions:
a. What does each function return as a function of input parameter n?
b. Which function is the worst in terms of time efficiency? Support your answer by stating, without
proof, the running time growth rate, as a function of n, of each of the above functions.
help_outline

Image Transcriptionclose

Write a C++ program, no need to be object-oriented, to test the following three functions. Each function receives one parameter n, which is a positive integer. int F1(int n) { if (n if (n % 2 int result 0) return 1; 0) { F1(n / 2); return result * result; else 1); return 2 * F1(n } int F2(int n) { if (n return 2 * F2(n - 1); 0) return 1; int F3(int n) { if (n 0) return 1; return F3(n - 1) + F3(n - 1); } Test the functions by calling them with the following values for n: 1, 3, 5, 16, 24, 26, 30. Submit your test program and in your report answer the following questions: a. What does each function return as a function of input parameter n? b. Which function is the worst in terms of time efficiency? Support your answer by stating, without proof, the running time growth rate, as a function of n, of each of the above functions.

fullscreen
check_circle

Expert Answer

Function F1

For function F1 the number of steps assuming only if branch:

T(1) = 1

T(n) = 1 + T(n/2)

Hence by repeated substitution we get

T(n) = log(n) + 1 i.e. O(logn)

For function F1 the number of steps assuming only the else branch:

T(1) = 1

T(n) = 1 + T(n-1)

Hence by repeated substitution we get

T(n) = k + T(n-k) = … = n -1 + 1 = O(n)

Hence the worst case time complexity is O(n) as O(n) is tighter than O(logn)

Function F2

For function F2 doing similar analysis as for F1 we get a worst case time complexity de...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: In a computer instruction format, the instruction length is 10 bits and the size of an addressfield ...

A:  Total number of instructions =210=1024Size of address field=3 bitsAnd the instruction length is 10 ...

question_answer

Q: - For all the employees supervised by KING, give them a salary increase, which is equal to the 2% of...

A: Programming Instructions:Create two database tables ‘EMP’ and ‘DEPT’ and declare all required attrib...

question_answer

Q: (1) Prompt the user for a title for data. Output the title. Ex: Enter a title for the data: Number o...

A: Answer:1.Program: #include <iostream>#include <string> using namespace std; int main(){ ...

question_answer

Q: Please see attachment.

A: Given expression:

question_answer

Q: #include <stdio.h> int main(){printf(" enter two integers and ill add them \n"); int integer1;...

A: Program Instructions: In the given C code at the time of taking input from the user by using 'scanf(...

question_answer

Q: 13. The reciprocal Fibonacci constant y is defined by the infinite sum: ν- ΣΕ where F, are the Fibon...

A: Program:n = [10 50 100];for j = 1:3a = 1;b = 1;reci = 1/a+1/b;for i = 1:n(j) - 2c = a + b;reci = rec...

question_answer

Q: Do the following program in Java Eclipse. Java Class that can be used to create a Fruit object as de...

A: The java program and approach are given below:Note: Run the same code on Eclipse.Approach: Creating ...

question_answer

Q: Convert the following decimals numbers to binary. 187 20486

A: For converting the given decimal number into the binary number, keep dividing the available number w...

question_answer

Q: Draw the logic diagram for the following Boolean expressions. The diagram should correspond exactly ...

A: OR gate is used where + is provided in the boolean expression.AND gate is used where .(or multiplica...