Question

In: Computer Science

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”.


Solutions

Expert Solution

Code:

#include <iostream>
using namespace std;
void print_asterisk(int asterisk)
{
   // base case
   if (asterisk == 0)
       return;
   cout << "#";
   print_asterisk(asterisk - 1);
}

// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
   // base case
   if (n == 0)
       return;
   print_asterisk(num - n + 1);
   cout << endl;

   // recursively calling pattern_upper()
   pattern_upper(n - 1, num+1);
}

// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
   if (n == 0)
       return;
   print_asterisk(n);
   print_asterisk(n-1);
   cout << endl;

   // recursively calling pattern_lower(
   pattern_lower(n - 1, num-1);
}

// function to print the pattern
void pattern(int n, int num)
{
   pattern_upper(n, num);
   pattern_lower(n , num);
}

// driver function
int main()
{
  
   int n = 9;
   pattern((n/2)+1, (n/2)+1);
   return 0;
}

Output:


Related Solutions

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".
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.
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should...
Write a recursive function that finds the minimum value in an ArrayList. Your function signature should be public static int findMinimum(ArrayList<Integer>) One way to think of finding a minimum recursively is to think “the minimum number is either the last element in the ArrayList, or the minimum value in the rest of the ArrayList”. For example, if you have the ArrayList [1, 3, 2, 567, 23, 45, 9], the minimum value in this ArrayList is either 9 or the minimum...
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 in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT