Apr 17, 2013 at 7:44am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "stdafx.h"
#include<iostream>
using namespace std;
void line2(int ,int ,int );
int main()
{int i;
do
{cout<<"How many rows? " ;
cin>>i;
if (i<0)
cout <<" must be nonnegative\n " ;
cout << " " ;
}while (i<0);
line2(1,1,i);
system("pause" );
return 0;
}
void line2(int m,int n,int p)
{int i;
if (m<n)
return ;
cout << " " ; for (i = 0; i <m; i++)
cout<<"* " ;
if (m>0)
cout<<endl;
if (m==p||n==0)
line2(m-1,0,p);
else
line2(m+1,n,p);
}
My code compiles just fines but stars are suppose to be in the middle but it doesn't space correctly, any ideas why or how to fix it???
1 2 3 4 5 6 7
*
* *
* * *
* * * *
* * *
* *
*
above image is how its suppose to show but it all starts in first line instead of making the correct shape. Thanks
Last edited on Apr 17, 2013 at 7:46am UTC
Apr 17, 2013 at 8:51am UTC
line 24 is suppose to space it out. although i does give it one space out it still doesn't show the correct format??
Apr 17, 2013 at 2:27pm UTC
If you didn't know it, in such problems (star printing problems) you should first write general formula and then code using loops (unless you are forced by your professor to use recursion):
n-r " " and r "* "
where n is inputted at the beginning of the program (in your case 4)
and r varies from 1 to n and then n-1 to 1
.
Last edited on Apr 17, 2013 at 2:27pm UTC
Apr 17, 2013 at 6:11pm UTC
MiiNipaa,
I can't thank you enough!!!
Sharma,
Thanks, but for this assignment i had no choice but to use recursion.
and point taken.