Reputation: 31
I want to print an X shape using n
number of rows but without any loop. I have to use a recursive function to print the X.
I have called the functions along with space function but it didn't print the X. It printed:
* *
* *
* *
I have to solve this problem using recursion. No for
loop or while
loop is allowed to solve this one.
// C++ implementation to print the given
// pattern recursively
#include <bits/stdc++.h>
using namespace std;
// function to print the 'n-th' row of the
// pattern recursively
int g;
void printPatternRowRecur(int n) {
if (n < 1)
return;
if (n = 1) {
cout << "*";
}
// print the remnaining stars of the n-th row
// recursively
else {
return;
}
printPatternRowRecur(n - 1);
}
void print_space(int space) {
// base case
if (space == 0)
return;
cout << " ";
// recursively calling print_space()
print_space(space - 1);
}
int s;
void Rhombus(int n) {
// base condition
if (s >= n)
return;
else {
print_space(s);
printPatternRowRecur(n);
print_space(n - s);
printPatternRowRecur(n);
}
// print the stars of the n-th row
s++;
// move to next line
cout << endl;
// print stars of the remaining rows recursively
Rhombus(n);
}
// Driver program to test above
int main() {
int n = 3;
//cout << "Enter the number of lines you want to print" << endl;
//cin >> n;
//cout << endl << "Rhombus" << endl;
Rhombus(n);
return 0;
}
Upvotes: 3
Views: 1409
Reputation: 144949
if (n = 1)
should be if (n == 1)
. As coded, the if
expression is always true.
Here is a simplified version:
// C++ implementation to print the given pattern recursively
#include <iostream>
using namespace std;
void print_spaces(int n) {
if (n > 0) {
cout << ' ';
print_spaces(n - 1);
}
}
void rhombus(int s, int n) {
if (s < n) {
int left = min(s, n - s - 1);
int middle = n - 2 * left - 2;
print_spaces(left);
cout << '*';
if (middle >= 0) {
print_spaces(middle);
cout << '*';
}
cout << endl;
// print stars of the remaining rows recursively
rhombus(s + 1, n);
}
}
void rhombi(int s, int n) {
if (s <= n) {
cout << endl << "Rhombus " << s << endl;
rhombus(0, s);
rhombi(s + 1, n);
}
}
int main() {
//int n = 3;
//cout << "Enter the number of lines you want to print" << endl;
//cin >> n;
//cout << endl << "Rhombus" << endl;
//rhombus(0, n);
rhombi(0, 7);
return 0;
}
Upvotes: 3
Reputation: 23945
Here's a recursion with one function, one given parameter, and two default parameters:
#include <iostream>
using namespace std;
void f(int y, int x=1, int w=0){
if (y < 1)
return;
if (x > 0){
f(y, -(y + 2 * w), w);
f(y - 2, x, w + 1);
if (y > 1)
f(y, -(y + 2 * w), w);
return;
}
if (x == -w){
cout << endl;
return;
}
if (x == -(w + y) || x == -(w + 1))
cout << '*';
else
cout << ' ';
f(y, x + 1, w);
}
int main(){
f(5);
cout << endl;
f(6);
return 0;
}
Upvotes: 1
Reputation: 406
#include <iostream>
using namespace std;
// * *
// * *
// *
// * *
// * *
//printStarHorizontally will recursively print '*' on a line using x coordinates
void printStarHorizontally(int xCoordinate1, int xCoordinate2, int currentXCoordinate, int maxCoordinate){
if(currentXCoordinate >= maxCoordinate) {
cout<<endl;
return;
}
if(currentXCoordinate == xCoordinate1 || currentXCoordinate == xCoordinate2) {
cout<<"*";
} else {
cout<<" ";
}
printStarHorizontally(xCoordinate1, xCoordinate2, currentXCoordinate + 1, maxCoordinate);
}
//PrintCross will go to each height and then use printStarHorizontally func. to print star on that particular height
void PrintCross(int heightOfCross, int currentHeight) {
if(currentHeight >= heightOfCross) return;
printStarHorizontally(currentHeight, heightOfCross-currentHeight-1, 0, heightOfCross);
PrintCross(heightOfCross,currentHeight+1);
}
int main() {
// heightOfCross should be odd integer
int heightOfCross = 13;
PrintCross(heightOfCross, 0);
return 0;
}
Upvotes: 1
Reputation: 149085
I would use two recursive functions: first to write a character after a number of spaces, second to write the lines.
#include <iostream>
/*************************************
* display on character (c) on out after x spaces recursively
* **********************************/
void display_char(int x, std::ostream& out = std::cout, char c='*') {
if (x == 0) out << c;
else {
out << ' ';
display_char(x-1, out, c);
}
}
/*********************************
* displays lines to draw an X pattern
* The pattern is composed of c characters on out
* x decreases to 0 while y increases and a c
* is printed at positions x and y
* ******************************/
void display_line(int x, int y=0, std::ostream& out=std::cout, char c='*') {
int oldx=x, oldy=y;
if (x < y) {
int t = x;
x = y;
y = t;
}
display_char(y, out, c);
if (x != y) display_char(x - y, out, c);
out << '\n';
if (oldx > 0) display_line(oldx-1, oldy+1, out, c);
}
int main() {
int x;
std::cout << "X size (int): ";
std::cin >> x;
display_line(x-1);
return 0;
}
Upvotes: 0