The pattern is as follows:
1. _ _ _ *
2. _ _ * *
3. _ * * *
4. * * * *
[Ignore numbers: just used them to increase readiblity, and underscores are spaces here for representation.]
Well its quiet easy to do it with 2 for or while loops , but my friend challenged me to it with only 1 loop.And i have to create a general program for it , so that it runs for not only 4 but 5 or 6 too.
Well i tried and tried , and till now i have reached this far:
int a=1;
int times=4; int totalelements=times*times;
int n=1;
int breakpoint=(times*n)-(n-1);
while(a<(totalelements+1))
{
if (breakpoint==a)
{cout<<"*";
n++; breakpoint=(times*n)-(n-1); }
else cout<<" ";
if(a%times==0) { cout<<endl;}
}
The expected output is :
1. _ _ _ *
2. _ _ * _
3. _ * _ _
4. * _ _ _
So the thing i am doing wrong is : i am able to find out breakpoint pattern for it , but it prints stars only at those breakpoints , not after that.
How should i do that , to print those stars after that breakpoint
one thing i can relate is the no. of stars being printed is the value of n at that time, so how should i link it?
The program must use only 1 loop , any no. of variables , and no goto statement .