# Print a diamond pattern using recursion

Page 1 of 1

## 9 Replies - 60589 Views - Last Post: 18 February 2010 - 10:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=57573&amp;s=add5ae3ed4b800122d359e9929dc4845&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rttlsnake

Reputation: 0
• Posts: 22
• Joined: 08-February 08

# Print a diamond pattern using recursion

Posted 14 July 2008 - 09:56 PM

I need some help. here is the assignment description:
Write a program that generates a diamond pattern of stars. The user is prompted for the number of stars for the largest row in the pattern and the program must call a recursive function to generate it.

I can get half of to produce but not in the shape of a diamond. Any pointers on how to make it appear as a diamond would be great.

Here is my code thus far:
```#include <iostream>
#include <string>
using namespace std;
void printStars(int count, int num);
int main() {
cout << "Enter the number for the largest line to print: ";
int x;
cin >> x;
int ind = 1;

printStars(ind, x);

system("PAUSE");
return 0;
}
void printStars(int count, int num)
{
for (int i = 0; i < count; i++) cout << "*";
cout << endl;
if (count < num) printStars(count + 1, num);
for (int i = 0; i < count; i++) cout << "*";
cout << endl;
}

//Output should look similiar depending on the number for the largest row
//	 *
//	* *
//   * * *
//  * * * *
//   * * *
//	* *
//	 *

```

Is This A Good Question/Topic? 0

## Replies To: Print a diamond pattern using recursion

### #2 polymath

Reputation: 53
• Posts: 670
• Joined: 04-April 08

## Re: Print a diamond pattern using recursion

Posted 15 July 2008 - 04:58 AM

add another print star call. I have some code I wrote a while ago to help someone that prints a triangle that looks like this:

Enter Stars at Max Row: 3

*
**
***
**
*

Let me know if this code would help.

HAHA, sure, we can give pointers . Just type int* a=new int; and see what happens

### #3 captainhampton

• Jawsome++;

Reputation: 14
• Posts: 548
• Joined: 17-October 07

## Re: Print a diamond pattern using recursion

Posted 15 July 2008 - 08:43 AM

polymath, on 15 Jul, 2008 - 04:58 AM, said:

HAHA, sure, we can give pointers . Just type int* a=new int; and see what happens

### #4 rttlsnake

Reputation: 0
• Posts: 22
• Joined: 08-February 08

## Re: Print a diamond pattern using recursion

Posted 17 July 2008 - 08:06 PM

polymath, on 15 Jul, 2008 - 04:58 AM, said:

add another print star call. I have some code I wrote a while ago to help someone that prints a triangle that looks like this:

Enter Stars at Max Row: 3

*
**
***
**
*

Let me know if this code would help.

HAHA, sure, we can give pointers . Just type int* a=new int; and see what happens

That would be great, I am still having trouble to make it work.

Thanks!

### #5 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Print a diamond pattern using recursion

Posted 04 September 2008 - 08:07 AM

edit--never mind, my solution doesn't use recursion. I'll post it somewhere else.
Sorry.

This post has been edited by OliveOyl3471: 04 September 2008 - 08:09 AM

### #6 Thomsoningreen

Reputation: 1
• Posts: 6
• Joined: 28-September 08

## Re: Print a diamond pattern using recursion

Posted 28 September 2008 - 10:43 AM

Try this one it will make a diamond but you will have to make it work with a user input.
```// Translate your logic and algorithm to working code!!
int row, space, star;
for(row=1; row<=5; row++){	//top half
for(space=1; space<=5-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl;
}

for(row=4; row>=1; row--){	//bottom half
for(space=1; space<=5-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl;
}

```

Reputation: 0
• Posts: 1
• Joined: 14-November 09

## Re: Print a diamond pattern using recursion

Posted 14 November 2009 - 07:06 AM

polymath, on 15 Jul, 2008 - 03:58 AM, said:

add another print star call. I have some code I wrote a while ago to help someone that prints a triangle that looks like this:

Enter Stars at Max Row: 3

*
**
***
**
*

Let me know if this code would help.

HAHA, sure, we can give pointers . Just type int* a=new int; and see what happens

do you still have this code i would like to look at it

Reputation:

## Re: Print a diamond pattern using recursion

Posted 18 February 2010 - 09:17 AM

Yes this one is easy, well I wouldn't say easy because you've got to find some way to print the diamond is some kind of buffer because
you can't print it line by line. You've got to think recursively i.e. print the outline of a large diamond and then print all the outlines of smaller diamonds until you fill the large diamond in.

I haven't looked at your code so I don't know if you are already done this.

### #9 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

## Re: Print a diamond pattern using recursion

Posted 18 February 2010 - 09:47 AM

Yipes, another old post! Anyway ...

To produce a diamond shape,you need to ensure the user enters an odd number ... cant work with an even number else you end up with a lob-sided diamond. Nobody likes a lob-sided diamond. The recusion part is easy. You don't need a buffer as someone has suggested. Here is my solution in pseudo-code.

```diamond_routine using current_star_count and int max_star_count
if the current_star_count is greater than the max_star_count then return
print max_star_count minus the current_star_count spaces
print (current_star_count*2) - 1 asterisks
call diamond_routine with current_star_count plus one and the max_star_count

```

This post has been edited by Martyn.Rae: 18 February 2010 - 09:50 AM

### #10 Ferencn

• D.I.C Regular

Reputation: 71
• Posts: 322
• Joined: 01-February 10

## Re: Print a diamond pattern using recursion

Posted 18 February 2010 - 10:27 AM

What Martyn describes is a good algorithm.
But... you can make it work for an even number of max stars as well by putting a space between the asterisks:
```   *
* *
* * *
* * * *
* * *
* *
*

```

This post has been edited by Ferencn: 19 February 2010 - 07:12 AM