please help me... i don't know which line of code did i get wrong... the output is supposed to be:

1
12
123

but what i get is:

1
21
321

...here is my code... i need to use while loop for this. thanks guys.

#include<iostream>
using namespace std;
int triangle(int n);

int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"enter number : ";
cin>>n;
cout<<triangle(n)<<endl;

}

int triangle(int n)
{
if (n < 1)
return 0;
else
{
triangle(n - 1);

while(n > 0)
{
cout<<n;
n--;
}
cout<<endl;

}

Recommended Answers

All 4 Replies

First some remarks on this statement: cout << triangle(n) << endl; (1)
...and your triangle() function (2):

[B]int[/B] triangle(int n)
{
if (n < 1)
  return 0;
else
[B]{[/B] [B]// where is that matching closing brace??[/B]
triangle(n - 1);

while(n > 0)
{
  cout << n;
  n--;
}
cout << endl;
  
  [B]// sure you don't miss a return here?[/B]
}

(1): why do you want to send the return value of that function to cout, in order to be displayed?
The function itself does the displaying of the triangle, right?
So there's no need to pass the return value to cout if you only want to see the triangle.

(2):
Make sure that your function returns an integer under all circumstances, not only when the end of the "recursive chain" is reached.
This means that you'll also have to put a return statement at the end of the triangle() function's body (make sure you actually return a value!).
There's actually even no need to let your function return an integer (unless defined else by your instructor), you could simply have done the same using a function of type void.

And now, to go to your problem:

while(n > 0)
{
   cout << n;
   n--;
}

Do you see what's going wrong here? You are counting down, I know you're not allowed to use for-loops, but I would suggest you to first make it run well using a for-loop, and then magically convert the for-loop into a while-loop approach by using the following "trick":

(3) This is a bonus:
If this is the for-loop:

for([B]part A[/B]; [B]part B[/B]; [B]part C[/B])
{
  /*
      The loop's body
  */
}

Then you can simulate it using a while-loop, as follows:

[B]part A[/B];
while([B]part B[/B])
{
  /*
      The loop's body
  */
  [B]part C[/B];
}

So if your for-loop looks like this:

for(int i = 0; i < 5; i++)
{
   cout << "Hello..." << endl;
}

Then an equivalent approach using while looks as follows:

int i = 0; // part A;
while(i < 5) // i < 5 -> part B
{
   // START of user-defined body
       cout << "Hello..." << endl;
   // END of user-defined body
   
   i++; // part C;
}

Here is a recursion version. Try to make it non-recursion.

//row is the number of rows
//itr is the number of column (sort of)
void prnt(int row = 5,int itr = 1)
{
	if(itr >= row)
		return;

	for(int i = 0; i < itr ; i++)
		cout<<(i+1);
	
	cout<<endl;

	prnt(row,itr+1);
}

but we are told that we need to use recursion in this code...

but we are told that we need to use recursion in this code...

Damn it. Please try to ignore my prev post. I thought this was
just a practice problem. Please do try to do this on your own.
I feel it coming soon.

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.