Question

In: Computer Science

C++ recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX...

C++

recursive function that draws the following shape.

X

XXX

XXXXX

XXXXXXX

XXXXXXXXX

XXXXXXXXX

XXXXXXX

XXXXX

XXX

X

The length of the longest row in the shape and the shape's character are function parameters.

In above shape the longest row is 9 and the pattern's character is "X".

Solutions

Expert Solution

Recurisive Function : This is a function that gets called during the execution. The process of this repeation takes several times during the execution. Once all the process is finished then only we will be getting the output.

PROGRAM SPECIFICATIONS:

In the implemention of the code I have provided some space after X its because for easy counting of the letter as it is mentioned the longest row is "9".It will become the output look more good enough also. Since I the question it is mentioned the code should be in C++. I have meet that criteria also.

PROGRAM CODE ;

#include <iostream> 
using namespace std; 

//Half pyramid recusion

//This function is for printing the half pyramid of X
void printn(int num) 
{ 
    //inital condition
    if (num == 0) 
        return; 
    cout << "X "; 
    printn(num - 1 ); 
} 
  
void pattern(int n, int i) 
{ 
    if (n == 0) 
        return; 
    //checking the condition using if loop    
    if(i%2!=0)
    {
        printn(i);
    }
    cout<<endl;
     pattern(n - 1 , i+1); 
} 
  
/* Here we are doing the inversion of the pyramid */
void printPatternInverted(int n, int i) 
{ 
    // intial  condition 
    if (n < 1) 
        return; 
      
    // to print the pattern of a particular row 
    if (i <= n && n%2!=0) 
    { 
        cout << "X "; 
        // recursively print rest of the stars  
        // of the row 
        printPatternInverted(n, i + 1); 
        
    }     
      
    else
    {   
        cout<<endl;
        // print the pattern of the remaining rows recursively 
        printPatternInverted(n-1, 1); 
    } 
} 

//main function begins here....
int main() 
{ 
    //declaration of variables
    int n = 9; 
    //calling the recurison function
    pattern(n , 1); 
    printPatternInverted(n, 1); 
    return 0; 
} 

PROGRAM SCREENSHOT :

I have done this program using online GDB complier and attaching the screenshot of input and output.

INPUT SCREENSHOT

OUTPUT SCREENSHOT


Related Solutions

it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
Fill in the ??? cells using the information presented. Use $(X,XXX) for credits and $X,XXX for...
Fill in the ??? cells using the information presented. Use $(X,XXX) for credits and $X,XXX for debits MCD (Shares in Millions; $ in Millions) Total Common Stock APIC Retained Earnings AOCI Treasury Stock Beginning balance (shares) 1660.6 -893.5 Ending Balance at Dec. 31, 2018 ????? $17 $7,376 $ 50,487 $(2,610) $(61,529) Comprehensive Income $ 6,152 ????? $ 127 Common stock cash dividends ????? $ (3,582) Treasury stock purchases (shares) -25 Treasury stock purchases $(4,981) $ (4,981) Share-based compensation ????? ?????...
Determine if each of the following recursive definition is a valid recursive definition of a function...
Determine if each of the following recursive definition is a valid recursive definition of a function f from a set of non-negative integers. If f is well defined, find a formula for f(n) where n is non-negative and prove that your formula is valid. f(0) = 1, f(n) = -f(n-1) + 1 for n ≥ 1 f(0) = 0, f(1) = 1, f(n) = 2f(n-1) +1 for n ≥ 1 f(0) =0, f(n) = 2f(n-1) + 2 for n ≥...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string:...
Reversing certain segments of the alphabet, using a recursive function. (C++) 1. Obtain the following string: abcdefghijklmnopqrstuvwxyz (as input or using initialization) 2. Using recursion, write a reverse function that reverses the characters in a string or character array given two indices (starting and ending). The string or the character array should reflect the reversal. 3. Read indices as input 11,18 (i.e. letters 12,19) 4. Call the reverse function to reverse letters: 12-19 5. Read indices as input 4,22 (i.e....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT