Saturday, July 5, 2014

Star pattern triangle in c with explanation and animation

Star pattern in C





So viewers !
Before writing any code we need to create its logic, that how to instruct computer ?
To create logic is very important part in programming.
Everyone's logic is different but it does not make any difference , output should be the same.
so let's begin with a simple logic that i have created. Hope it will be helpful.

_ _ _ _ _ *_             
_ _ _ _ * _ * _         
_ _ _ * _ * _ * _      
_ _ * _ * _ * _ * _   
_ * _ * _ * _ * _ * _

Logic

Look at the pattern carefully, there are few spaces and stars
we divide spaces into two categories : -
  1. The spaces before stars
  2. The spaces between stars
So,  As in example there are 5 lines or rows, so outer loop runs 5 times. And after each row there must be a newline.
Hint:-  Outer loop decide the no of rows.

One important thing to be discuss is that, "first all inner loops execute then next increment in outer loop proceed".

Means if outer loop runs first time so next time it will run after moving to the next row or line

Outer loop = no. of rows.
for( int i = 1; i <=5 ; i++)
{
}
First we are coding for equal gaps and equal stars ( without variation )
to clarify its logic..

Code


Outer loop:
for ( int i = 1 ; i <= 5 ; i++ )
{

Inner loop for spaces before stars: 

for ( int j = 1 ; j <= 5 ; j++ )
{
printf(" ");
}

Inner loop for stars and spaces between stars: 

for (int k = 1 ; k <= 5 ; k++ )
{
printf("*");
printf(" ");
}

For newline ( it is in outer loop )

printf("\n");

}

(End of outer loop)  

  Explanation


Compiling line by line:

First line.
when first time loop execute, int i = 1. (First time execution of outer loop)
Then program enters into first Inner loop 
executing the loop 5 times resulting
_ _ _ _ _

Then program enters into second inner loop
executing the loop 5 times resulting
* _ * _ * _ * _ * _

Then next line.

complete execution of first line reslulted as,

_ _ _ _ _ * _ * _ * _ * _ * _ 

Now similarly , next time outer loop execute when int i = 2,
Again we get the same 

_ _ _ _ _ * _ * _ * _ * _ * _

Then next line and so on. So the complete outer loop results 


_ _ _ _ _ * _ * _ * _ * _ * _
_ _ _ _ _ * _ * _ * _ * _ * _
_ _ _ _ _ * _ * _ * _ * _ * _
_ _ _ _ _ * _ * _ * _ * _ * _
_ _ _ _ _ * _ * _ * _ * _ * _

so this is the actual output of above program,

  Logic : 1

Now look at the lines variation
1st line  = 5 spaces before star.
2nd line = 4 spaces before stars.
3rd line = 3 spaces. So on
means in every line spaces are decreasing.

about stars and spaces between stars (Treat them as single unit)

1st line = 1 
2nd line = 2
3rd line = 3 and so on
means in every line stars and spaces between stars are increasing.
So code inner loop is responsible for a complete line.
Now our logic is done.
Now for a triangle we need some variations.. 
as the variations are discussed above so now we are finally inserting and generating the final
code.

  Code

Outer loop ( same ).
for ( int i = 1 ; i <= 5 ; i++ )
{
Inner loop for spaces before stars.
Changing the loop condition for variation.
for ( int j = i ; j <= 5 ; j++ )
{
printf(" ");
}

Inner loop for stars and spaces between stars.
Changing the loop condition for variation.

for (int k = 1 ; k <= i ; k++ )
{
printf("*");
printf(" ");
}
For newline ( it is in outerloop )
printf("\n");
}
(end of outer loop).

Explaination

Compiling line by line :-

First line.
when first time loop execute, int i = 1, first time execution of outer loop

Then program enters into first inner loop 
Now as loop condition ,
for ( j = i (1) ; j < = 5 ; j ++ )
loop executes 5 times resulting
_ _ _ _ _

Then program enters into second inner loop
now as loop condition ,

for ( k = 1 ; k <= i (1) ; k++ )
loop executes only one time resulting

* _

complete execution of first line reslulted as,
_ _ _ _ _ * _ 

Now next line , Next turn of outer loop , now i = 2
When first time loop execute, int i = 2 (first time execution of outer loop)
then program enters into first inner loop 
now as loop condition ,
for ( j = i (2) ; j < = 5 ; j ++ )
loop executes 4 times resulting
_ _ _ _ 

Then program enters into second inner loop
now as loop condition ,
for ( k = 1 ; k <= i (2) ; k++ )
loop executes only one time resulting
* _ * _

complete execution of 2nd line resulted as,
_ _ _ _ * _ * _

and so on.. resulting the desired output ... 

_ _ _ _ _ *_
_ _ _ _ * _ * _
_ _ _ * _ * _ * _
_ _ * _ * _ * _ * _
_ * _ * _ * _ * _ * _

Final Code

# include<stdio.h>
# include<conio.h>
void main (void)
{
clrscr();
for ( int i = 1 ; i <= 5 ; i++ )
{
for ( int j = i ; j <= 5 ; j++ )
{
printf(" ");
}
for (int k = 1 ; k <= i ; k++ )
{
printf("*");
printf(" ");
}
printf("\n");
}
getch();
}

Logic 2

If logic 1 is little bit complex or you are confusing about the variation and don't want to use
variation in inner loops with respect to int i , so this is a simple and new logic.
Look at the pattern,
As we proceed to next line spaces are decreasing and stars are increasing simultaneously. So if we just follow a simple principle that.

  1. First we initialize a new integer variable for spaces say "spaces" and assign it the value of spaces of first line of output i.e 5 spaces in first row. So, int spaces=5.
    And then another new integer variable for stars say "stars" and assign it the value of stars of first line of output i.e 1 star in the first row. So, int stars=1.
    As we proceed to next line, spaces are decreasing and stars are increasing simultaneously. 
  2. Now simply used increment operator in stars i.e "++" and decrement operator in spaces i.e "--".
That's it 

  Final Code

# include<stdio.h>
# include<conio.h>
void main (void)
{
clrscr();
int spaces = 5;
int stars = 1;
for ( int i = 1 ; i <= 5 ; i++ )
{
for ( int j = 1 ; j <= spaces ; j++ )
{
printf(" ");
}
for (int k = 1 ; k <= stars ; k++ )
{
printf("*");
printf(" ");
}
printf("\n");
stars++ ;
spaces-- ;
}
getch();
}

Share this

1 Comment to "Star pattern triangle in c with explanation and animation "

Ashes Mondal said...

Outstanding