Recursive function to print a shape

I have to write a program that prints out a shape where the first line has n stars, second has n-2, ..., and the last line has 2 stars, using a recursive function.
The picture should look like
******
****
**

This is what I have so far - I was able to print out the picture for when n was 8 but I cant get it for any number as n


#include <iostream>
#include <string>
using namespace std;

void makespaces (int total, int current) {
for (int c = 0; c <((total-current)/2); c++){
cout << " ";
}
}

void pr (int n, int total) {
if (n < 1) return;
makespaces (total, n);
for (int x = n; x > 0; x--){
cout << "*";
}
makesapces (total, n);
cout << endl;
pr (n-2, total);
}

int main () {
int n = 0;
while (n%2!=0 || n <=0){
cout << "Enter a positive integer ";
cin >> n;
}
pr (8,8);
}

Last edited on
1) Use code tags please.

2) There's a spelling error in your code. Look in your function pr().

3) The reason you're not getting it for any number (aka odd numbers) is due to your while loop in main.

4) Just replace pr(8,8) with pr(n,n).

5) Think of what the compiler is doing in the command prompt with your program. You created a function that would print spaces. So it gives you spaces instead of a downwards facing right triangle.
Topic archived. No new replies allowed.