9 Replies - 43161 Views - Last Post: 18 February 2010 - 10:27 AM Rate Topic: -----

#1 rttlsnake  Icon User is offline

  • New D.I.C Head

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

Print a diamond pattern using recursion

Post icon  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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • 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 :D
Was This Post Helpful? 0
  • +
  • -

#3 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: Print a diamond pattern using recursion

Posted 15 July 2008 - 08:43 AM

View Postpolymath, on 15 Jul, 2008 - 04:58 AM, said:

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


:P
Was This Post Helpful? 0
  • +
  • -

#4 rttlsnake  Icon User is offline

  • New D.I.C Head

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

Re: Print a diamond pattern using recursion

Posted 17 July 2008 - 08:06 PM

View Postpolymath, 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 :D



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

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Thomsoningreen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
	}


Was This Post Helpful? 1

#7 renegade_314  Icon User is offline

  • New D.I.C Head

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

Re: Print a diamond pattern using recursion

Posted 14 November 2009 - 07:06 AM

View Postpolymath, 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 :D

do you still have this code i would like to look at it
Was This Post Helpful? 0
  • +
  • -

#8 Guest_mark king*


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.
Was This Post Helpful? 0

#9 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • 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
    return to caller


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

Was This Post Helpful? 0
  • +
  • -

#10 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1