How can i print it recursively with one helper function?

[img]http://img2.imageshack.us/img2/1690/recursive.th.jpg[/img]

Here, n will always be a power of 2 and c will be indicating the column number from
which to start the output. The above pattern has been produced with the initial function call RecursivePattern(8,1)


It prototype should be like void Recursive pattern(int n, int c)
where n number represent Asterisk, and c represent column.
Above output is function of RecursivePattern(8,1). As you can see the longest line of the pattern has 8 asterisks beginning in column 1. In general, the function call RecursivePattern(n,c) will produce the output in which the longest line has n asterisks beginning in the column c. Note that there is a space after each asterisk.

mvmalderen commented: No double posting here :( (http://www.daniweb.com/forums/thread191375.html) -1

Recommended Answers

All 11 Replies

How can i print it recursively with one helper function?

[img]http://img2.imageshack.us/img2/1690/recursive.th.jpg[/img]

Here, n will always be a power of 2 and c will be indicating the column number from
which to start the output. The above pattern has been produced with the initial function call RecursivePattern(8,1)


It prototype should be like void Recursive pattern(int n, int c)
where n number represent Asterisk, and c represent column.
Above output is function of RecursivePattern(8,1). As you can see the longest line of the pattern has 8 asterisks beginning in column 1. In general, the function call RecursivePattern(n,c) will produce the output in which the longest line has n asterisks beginning in the column c. Note that there is a space after each asterisk.

That's a star? I doesn't look like any star I've ever seen. Post the code you have so far along with a description of exactly what the problem is and what the actual output is. Does it compile? Does it run to completion? You can at least post a main function and the recursive function, even if that function currently does nothing.

That's a star? I doesn't look like any star I've ever seen.

It isn't a star but a 'star pattern' (or asterisk pattern)

Following is a text-based fractal pattern of asterisks and spaces. my task is to write a function that can generate patterns like this.

[img]http://img5.uploadhouse.com/fileuploads/3910/3910325-holder-5fc85321bbfdce289fc7e5d339238de6.jpg[/img]

its function prototype should be exactly like this:
void RecursivePattern (int n, int c)

Here, n will always be a power of 2 and c will be indicating the column number from which to start the output. The above pattern has been produced with the initial function call RecursivePattern(8,1)
As you can see the longest line of the pattern has 8 asterisks beginning in column 1. In general, the function call RecursivePattern(n,c) will produce the output in which the longest line has n asterisks beginning in the column c. Note that there is a space after each asterisk.

Assuming that the base case for the function is when n equals 1, you just need to figure out what exactly the function does on each run and code accordingly. Write out on a piece of paper if it helps.

Solution Of above Question

#include<conio.h>
#include<iostream.h>

void recursivepattern(int n,int c)
{
	if(n==1)
	{

		for(int i=0;i<c;i++)
		cout<<" ";
		cout<<"*";
	}
	else
	{
		recursivepattern(n/2,c);
		cout<<endl;
		for(int k=0;k<c;k++)
	cout<<" ";
	for(int p=0;p<n;p++)
	cout<<"* ";
	cout<<endl;
	recursivepattern(n/2,c+n);
	}
}

void main()
{
clrscr();

	recursivepattern(8,4);

getch();
}
commented: void main, non-standard code, Giving code to noob. There are multiple reason for a bad reputation -1
#include<conio.h>
#include<iostream.h>

void recursivepattern(int n,int c)
{
	if(n==1)
	{

		for(int i=0;i<c;i++)
		cout<<" ";
		cout<<"*";
	}
	else
	{
		recursivepattern(n/2,c);
		cout<<endl;
		for(int k=0;k<c;k++)
	cout<<" ";
	for(int p=0;p<n;p++)
	cout<<"* ";
	cout<<endl;
	recursivepattern(n/2,c+n);
	}
}

void main()
{
clrscr();

	recursivepattern(8,4);

getch();
}
commented: Are you still using a dinosour compiler? This is just unportable rubbish !!! -1

What if I want to display this pattern without the space in between the stars?

commented: Don't bump old threads -1

What if I want to display this pattern without the space in between the stars?

Then you will create new thread and show what you did.

Thread closed

HI, How to print 2016 as pattern using with stars in java pattern

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.