Question

subject : basic programming with C++

Notes: please give me detailed answer so I could understand and learn from this, thank you very much

PL03; CO-5: Students have knowledge of modular programming / subprograms and can implement them in computer programs.; mark:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

The code for (a) part is as under:

#include <iostream>

using namespace std;

int e[10] = {6,7,4,5,-1,4,-1,3,1,9};

int compute(int a, int b)
{
if(a == b)
{
return 1;
}
  
else if(e[a] == -1)
{
return 0;
}
  
else
{
return 2*compute(e[a],b);
}
}

int main()
{
cout<<compute(8,4); //declaration of the question statement for execution

return 0;
}

Step by Step explanation:

> The values of a and b will be checked i.e. a=8 and b=4;

> As a != b, the first if statement will be skipped and moved to the next step.

> Now, the else if statement will be checked and as e[8] != -1 it will be skipped as well.

> Now, it will move to the else statement and will be executed.

Now, as we know that the question is using recursion therefore, the explanation of the recursion is as follows:

> compute(a,b) we will represent it as c(a,b) where a=8 and b=4

> Now, for the first recursion c(8,4) is as such that it will go to the else statement(already discussed above) i.e. 2*compute(e[a],b)

> There, the value it ill have will become c(e[8],4) which is equal to c(1,4) as the number at location e[8]=1. Now, a=1, b=4

> As we see again it will go to else statement 2*compute(e[a],b); where the new values for c(a,b) will be, c(7,4).

>As we see again it will go to else statement 2*compute(e[a],b); where the new values for c(a,b) will be, c(3,4)

>As we see again it will go to else statement 2*compute(e[a],b); where the new values for c(a,b) will be, c(5,4)

>As we see again it will go to else statement 2*compute(e[a],b); where the new values for c(a,b) will be, c(4,4) and now it fulfills the if statement and thus, returns 1, so now the result for the last step i.e. c(4,4) is 1 now the recursion will happen.

> Now, c(4,4) =1

c(5,4) = 2*c(4,4)=2

c(3,4)=2*c(5,4)=2*2=4

c(7,4)=2*c(3,4)=2*4=8

c(1,4)=2*c(7,4)=2*8=16

c(8,4)=2*c(1,4)=2*16=32 which is the desired result.

The output of this code is 32. Below is the screenshot attached of executed code with output.

Llı Result $g++ -o main *.cpp $main 32 == 9. Execute > Share main.cpp STDIN 1 #include <iostream> 2 3 using namespace std; 4

b)

The code for proper execution of (b) is as under:

#include <iostream>

using namespace std;

int e[10] = {6,7,4,5,1,4,2,6,-1,8};

int compute(int a, int b)
{
if(a == b)
{
return 1;
}
  
else if(e[a] == -1)
{
return 0;
}
  
else
{
return 2*compute(e[a],b);
}
}

int main()
{
cout<<compute(1,4); //The code for the execution of the second part

return 0;
}

Breaking it to step by step explanation.

We will write compute(a,b) as c(a,b) for easy understanding.

Now, as we know that c(a,b) is not fulfilling either the if statement or the else if, so it will directly go to the else stataement. This is a question of recursion, so it will be solved using the same technique.

Let us break the recursion steps for better understanding:

> c(1,4) will directly go to else statement which is 2*compute(e[a],b), so it will become c(7,4).

> c(7,4) will directly go to else statement which is 2*compute(e[a],b), so it will become c(6,4).

> c(6,4) will directly go to else statement which is 2*compute(e[a],b), so it will become c(2,4).

> c(2,4) will directly go to else statement which is 2*compute(e[a],b), so it will become c(4,4).

> c(4,4) will go to if statement and will return 1.

> Therefore: c(4,4)=1

c(2,4)=2*c(4,4)=2*1=2

c(6,4)=2*c(2,4)=2*2=4

c(7,4)=2*c(6,4)=2*4=8

c(1,4)=2*c(7,4)=2*8=16, which is the desired output.

The output for this is 16. Below is the screenshot attached of executed code with output.

STDIN Llı Result Execute > Share main.cpp 1 #include <iostream> $g++ -o main *.cpp $main 16 3 using namespace std; 4 5 int e[

Add a comment
Know the answer?
Add Answer to:
subject : basic programming with C++ Notes: please give me detailed answer so I could understand...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible...

    Subject: Object Oriented Programming (OOP) Please kindly solve the above two questions as soon as possible would be really grateful to a quick solution. would give a thumbs up. Thank you! Q3: Question # 3 [20] Will the following code compile? If it does not, state the errors. If it does compile, write the output. //Function.cpp #include <iostream> using namespace std; void printData (long i) cout<<"In long print Data "«<i<<endl; } void printData(int i) cout<<"In int printData "<<i<<endl; ) void...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • 2. What is the value of x alter the following code is executed # include<iostream> using...

    2. What is the value of x alter the following code is executed # include<iostream> using namespace std; int main int t-0, c- 0,x-3; while (c < 4) t=t+x; cout << x return 0; a) 3 b) 21 c) 24 d) 48 3. What is the output of the following code? # include<iostream> using namespace std; int main0 int a- 3; for (int i 5; i >0; i) a a+i cout << a << “ “ return 0; d) 8...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • 1. What is wrong with the following C++ program? #include <iostream> int main() { a =...

    1. What is wrong with the following C++ program? #include <iostream> int main() { a = 4; b = 6; cout << a << "+" << b << "=" << a+b; return 0; 2. What is wrong with the following C++ program? What was its intended output? #include <iostream> using namespace std; int main() { cout << "What is larger? e pi or pi e?" << endl; double ans1 = exp(pi); double ans2 = pi exp(1.); cout << "epi is...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • 1.20 LAB: Warm up: Basic output with variables This zyLab activity prepares a student for a...

    1.20 LAB: Warm up: Basic output with variables This zyLab activity prepares a student for a full programming assignment. Warm up exercises are typically simpler and worth fewer points than a full programming assignment, and are well-suited for an in-person scheduled lab meeting or as self-practice. A variable like userNum can store a value like an integer. Extend the given program as indicated. Output the user's input. (2 pts) Output the input squared and cubed. Hint: Compute squared as userNum...

  • CHALLENGE ACTIVITY 13.1.2: Basic function call. Complete the function definition to output the hours given minutes....

    CHALLENGE ACTIVITY 13.1.2: Basic function call. Complete the function definition to output the hours given minutes. Output for sample program: 3.5 1 test passed All tests passed 1 #include <iostream> 2 using namespace std; 3 4 void OutputMinutesAsHours (double origMinutes) { 5 6 /* Your solution goes here */ 7 8} 9 10 int main() { 11 double minutes; 12 13 cin >> minutes; 14 15 OutputMinutesAsHours (minutes); // Will be run with 210.0, 3600.0, and 0.0. 16 cout <<...

  • What is the output of this program? #include<iostream> using namespace std; int main() { int x=4,...

    What is the output of this program? #include<iostream> using namespace std; int main() { int x=4, y=4, z=4; x += 2; y = z++; z = ++x + y; cout<<x<<" "<<y<<" "<<z<<endl; return 0; } Group of answer choices 4 4 4 7 4 11 7 5 12 6 5 11

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT