Question

Write a function with one positive int parameter called n. The function will write 2^n-1 integers...

  1. Write a function with one positive int parameter called n. The function will write 2^n-1 integers (where ^ is the exponentiation operation). Here are the patterns of output for various values of n:
    n=1: Output is: 1
    n=2: Output is: 1 2 1
    n=3: Output is: 1 2 1 3 1 2 1
    n=4: Output is: 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
    And so on. Note that the output for n always consists of the output for n-1, followed by n itself, followed by a second copy of the output for n-1.
  2. What property of fractals lends itself to recursive thinking?
  3. The first step of the maze search algorithm was to step forward and write your name on the ground. What is the importance of writing your name on the ground?
  4. Consider the following function:
        bool dead_end()
        // Postcondition: The return value is true if the direction directly 
        // in front is a dead end (i.e., a direction that cannot contain the 
        // tapestry).
        // Library facilities used: useful.h (from Appendix 1).
        {
            return inquire("Are you facing a wall?")
                   ||
                   inquire("Is your name written in front of you?");
        }
    
    Explain why the function dead_end sometimes asks 2 questions and sometimes asks only 1.
    Short Answers
    Section 9.3
    Reasoning About
    Recursion
  5. What two properties must a variant expression have to guarantee that a recursive function terminates?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please please upvote, and ask your doubts in the comments


Question 1

#include <iostream>

using namespace std;

void print_pattern(int n) {
   if(n == 0) {
       return;
   } else {
       print_pattern(n-1);
       cout << n << " ";
       print_pattern(n-1);
   }
}

int main() {
   for(int i = 1; i < 5; ++i) {
       cout << i << ":\t";
       print_pattern(i);
       cout << endl;
   }
   return 0;
}



Question)
What property of fractals lends itself to recursive thinking?


A fractal is a geometric shape that can be made up of the samepattern repeated at different scales and orientations. Thenature of a fractal lends itself to a recursive definition



Question)
Writing your name on the ground allows the program to determine whether you've been to a particular spot before (thus avoiding a potential infinite recursion.


Question)

Explain why the function dead_end sometimes asks 2 questions and sometimes asks only 1.

Solution:

  return inquire("Are you facing a wall?")
               ||
               inquire("Is your name written in front of you?");

Explanation:

The function dead_end sometimes asks 2 questions and sometimes asks only 1 because of the kind of condition which is used in the code in the function.

this is an or condition in which first inquire("Are you facing a wall?") is asked, if this condition is true and since there is OR (||) operator there is no need to check the second condition. which means only one question is asked.

The second case is when the first condition is false, in this case after the first condition is false the second question

inquire("Is your name written in front of you?"); will be asked, which means 2 questions asked.


Question)

What two properties must a variant expression have to guarantee that a recursive function terminates?

What two properties must a variant expression have to guarantee that a recursive function terminates?

1. Recursive call

2. Base case to stop recursive call termination.

#include <iostream>

using namespace std;

int factorial(int n)
{
//base case
if (n <= 1) {
return 1;
}
//recursive call
else {
return n * factorial(n - 1);
}
}

int main()
{
cout << factorial(10) << endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a function with one positive int parameter called n. The function will write 2^n-1 integers...
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
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