Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,611 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 927 people online right now. Registration is fast and FREE... Join Now!




Print a diamond pattern using recursion

 
Reply to this topicStart new topic

Print a diamond pattern using recursion

rttlsnake
post 14 Jul, 2008 - 08:56 PM
Post #1


New D.I.C Head

*
Joined: 8 Feb, 2008
Posts: 10

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:
CODE

#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
//     *
//    * *
//   * * *
//  * * * *
//   * * *
//    * *
//     *
User is offlineProfile CardPM

Go to the top of the page

polymath
post 15 Jul, 2008 - 03:58 AM
Post #2


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


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 smile.gif . Just type int* a=new int; and see what happens biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 15 Jul, 2008 - 07:43 AM
Post #3


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


QUOTE(polymath @ 15 Jul, 2008 - 04:58 AM) *



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


tongue.gif
User is offlineProfile CardPM

Go to the top of the page

rttlsnake
post 17 Jul, 2008 - 07:06 PM
Post #4


New D.I.C Head

*
Joined: 8 Feb, 2008
Posts: 10

QUOTE(polymath @ 15 Jul, 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 smile.gif . Just type int* a=new int; and see what happens biggrin.gif



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

Thanks!
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 4 Sep, 2008 - 07:07 AM
Post #5


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,503



Thanked 14 times

Dream Kudos: 125
My Contributions


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

This post has been edited by OliveOyl3471: 4 Sep, 2008 - 07:09 AM
User is offlineProfile CardPM

Go to the top of the page

Thomsoningreen
post 28 Sep, 2008 - 09:43 AM
Post #6


New D.I.C Head

*
Joined: 28 Sep, 2008
Posts: 5


My Contributions


smile.gif Try this one it will make a diamond but you will have to make it work with a user input. wink2.gif
CODE
// 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;
    }
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month