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.
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 ≥...
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 ????? ?????...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
In the case of c++, how would I store data drom a recursive function into a...
In the case of c++, how would I store data drom a recursive function into a 2D array?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT