problem Draw Pattern with recursive function on C. help please

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 2915 Views - Last Post: 02 December 2012 - 10:35 PM Rate Topic: ***-- 3 Votes

#1 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 03:37 PM

Any help of any king would be really really appriciated (please read before you judge. I am not asking you to do my homework, i just ask help).

So I have this homework and i have been strungling a lot and I came here to get some help since deadline is in 2 days.
The main thing is to input the level of the pattern and get the following patterns using a recursive function (naming it voin drawPattern(long level, long ident); )
Level____|0___|1___|2____|3________|4________________and so on...
________|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pattern__|*___|*___|*____|*________|*
________|____|**__|**___|**_______|**
_____________| *__|_*___|_*_______|_*
_____________|____|****_|****_____|****
__________________|__*__|__*______|__*
__________________|__**_|__**_____|__**
__________________|___*_|___*_____|___*
__________________|_____|********_|********
________________________|____*____|____*
________________________|____**___|____**
________________________|_____*___|_____*
________________________|____****_|____****
________________________|______*__|______*
________________________|______**_|______**
________________________|_______*_|_______*
________________________|_________|****************
__________________________________|________*
__________________________________|________**
__________________________________|_________*
__________________________________|________****
__________________________________|__________*
__________________________________|__________**
__________________________________|___________*
__________________________________|________********
__________________________________|____________*
__________________________________|____________**
__________________________________|_____________*
__________________________________|____________****
__________________________________|______________*
__________________________________|______________**
__________________________________|_______________*
__________________________________|

(the |'s, _'s and ~'s are not included in the pattern)

I get the main idea(the stars at the middle are 2^level
and that the pattern above middle is the same as bellow but have 2^(level-1) spaces before the * and stuff)
and recursive of the pattern but i have problems creating the function. I have created a fuction to aid the printing(void printNchars(long n, char c)):

void printNchars(long n, char c)
{
long i;
for(i=0;i<n;++i)
{
printf("%c",c);
}
}
I think the ident on drawPattern should be the number of spaces. I really wouldnt ask for code if i really really didnt have the need to but I am really desperate an I need your help por favor.

Any help of any king would be really really appriciated.

Is This A Good Question/Topic? 0
  • +

Replies To: problem Draw Pattern with recursive function on C. help please

#2 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 03:58 PM

BUMP
Was This Post Helpful? -1
  • +
  • -

#3 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 04:30 PM

BUMP
Was This Post Helpful? -1
  • +
  • -

#4 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 04:43 PM

kk. i am bumping my head on the wall
Was This Post Helpful? -2
  • +
  • -

#5 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 05:18 PM

BUMP
Was This Post Helpful? -2
  • +
  • -

#6 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 05:24 PM

BUMP
Was This Post Helpful? -2
  • +
  • -

#7 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 07:19 PM

BUMP
Was This Post Helpful? -2
  • +
  • -

#8 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 07:52 PM

BUMP!!!
Was This Post Helpful? -2
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 07:53 PM

Does drawPattern have to be the recursive function?
Was This Post Helpful? 1
  • +
  • -

#10 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 08:09 PM

View Post#define, on 30 November 2012 - 07:53 PM, said:

Does drawPattern have to be the recursive function?

These functions are just suggestions given to me. the task is: Create a recursive function that recreates the recursive pattern.

thanks for reply, i was getting really desperate
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 09:07 PM

The column is a fixed width at any level, the position of the star depends on the row.

So a function could be created for that.

So something like so :-

void print_column(int level, int row);




With the recursive function it will be useful to pass a current level and perhaps other parameters.

I'll have a look at that.
Was This Post Helpful? 1
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 10:00 PM

If a parameter is used for the current level, several levels can be drawn.

// current starts at 0
void drawPattern1(long current, long level)
{
   if(current <= level) {
     cout << " level " << current << endl;
     drawPattern1(current+1, level);
   }
}





With another (variable) parameter columns can be added.

// col initially starts at 0
void drawPattern2(long current, long col, long level)
{
  if(current > level)
    return;

   if(col <= level)
   {
     cout << " col " << col;
     drawPattern2(current, col+1, level);
   }
   else
   {
     cout << " level " << current << endl;
     drawPattern2(current+1, 0, level);
   }
}




Within each level there are a number of rows so another parameter could be used for that.
Was This Post Helpful? 1
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • Posts: 10,903
  • Joined: 08-August 08

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 10:09 PM

With 7/9 of the OPs posts being BUMPS I think you're being too kind #define.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: problem Draw Pattern with recursive function on C. help please

Posted 30 November 2012 - 11:18 PM

It's a tricky pattern, but it looks like there should be an easier answer.

Anyway I answered, that stopped them bumping for now.
Was This Post Helpful? 0
  • +
  • -

#15 PROistorikos  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 30-November 12

Re: problem Draw Pattern with recursive function on C. help please

Posted 01 December 2012 - 08:12 AM

View PostCTphpnwb, on 30 November 2012 - 10:09 PM, said:

With 7/9 of the OPs posts being BUMPS I think you're being too kind #define.

I am sorry...

Thanks #define, I think i can start trying things again. :)

I am still a beginer with C and recursive functions (first time did recursive functions was 1 week before
and that example was quite simple). Is there a way to set a parameter that starts from a number and counts
down or up without making it an input? Cause i tried some ways within the recursive function but i can stop
it going back to the initial price it was set every time the the recursion runs.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3