Sunday, August 17, 2014

Adventure of FOR LOOPS for Beginners

Most of students who doing studies in software engineering field most probably face FOR LOOP challenges in beginning of their academic life. I think most of student get trouble, because they try to start coding without analyzing the problem. In this post I decide to introducing some tips for solve these kind of problems.

  1. Let’s think we have a problem like this.


  2. Before we start the coding we should clearly analyzing the position of the stars and the positions of the spaces. In order to do that I think better to doing some rough work like this.(You can easily sketch a draft using word processing software like MS Office.)


 Ok Let’s go through the draft.

a.       I think you have some idea about how to create the basic star triangle, basically we need two for loops to crate triangle.

                                                               i.      Main For Loop

For maintain horizontal view of the triangle.

                                                             ii.      Secondary For Loop(s)

Always inside the Main for loop, this would be handle the vertical view of the triangle. In basic level mostly we can see only one secondary for loop, But in the advanced triangle problem’s like this we should add more than one secondary  for loop.

b.      Ok, let’s consider main for loop.

I mention main for loop is always maintain the horizontal view of the triangle. In this problem also the same. We can easily define a for loop for handle a height of the triangle.

for(i=1;i<=h;i++)
            {
}

c.       Secondary for loop(s)

Let’s consider first row of the draft.


If you clearly analyze the draft you can see four major parts of the vertical view of the draft. Those are space(s), filling cell(s), space(s) & filing cell(s).
When we consider the whole draft we can easily understand increasing and decreasing of these spaces(s) and the filing cell(s).  

Let’s go through the major parts of the vertical view.

                                                               i.      Space(s) I

This part follow decreasing pattern (2,1,0). It’s a arithmetic series decreasing values one by one in each step.
And also I’ll numbering each row 1 to 3, from top to bottom. My target is get the equation of the space(s) and row numbers.

In 1st row 2 spaces.
In 2nd row 1 spaces.
In 3rd roe 0 spaces.

I think you catch the pattern. In each row we should print the spaces following this equation.

Height of row – Row Number = Number of spaces

I’ll put first secondary for loop for handle this.

for(j=h-i;j>=1;j--)
                        {
                                        printf(" ");
                        }

                                                             ii.      Filing cell(s) / Star(s)

This part follow increasing pattern (1,3,5). It’s also arithmetic series increasing values two by two in each step.
I’ll numbering each row 1 to 3, from top to bottom again. My target is same as the first step. It’s for get the equation of the filing cell(s) and row numbers.

In 1st row 1 spaces.
In 2nd row 3 spaces.
In 3rd roe 5 spaces.

I think you catch the pattern. In each row we should print the stars following this equation.

Row Number + Row Number - 1= Number of stars

I’ll put second secondary for loop for handle this.

for(k=i+(i-1);k>=1;k--)
                        {
                                        printf("*");
                        }

                                                            iii.      Space(s) II

This part follow decreasing pattern (4,2,0). It’s a arithmetic series decreasing values two by two in each step.
And also I’ll numbering each row 1 to 3, from top to bottom. My target is get the equation of the space(s) and row numbers.

In 1st row 4 spaces.
In 2nd row 2 spaces.
In 3rd roe 0 spaces.

I think you catch the pattern. In each row we should print the spaces following this equation.

(Height of row – Row Number) * 2 = Number of spaces

I’ll put first secondary for loop for handle this.

for(l=(h-i)*2;l>=1;l--)
                        {
                                        printf(" ");
                        }

                                                           iv.      Filing cell(s) / Star(s)

This part follow increasing pattern (1,3,5). It’s also arithmetic series increasing values two by two in each step.
I’ll numbering each row 1 to 3, from top to bottom again. My target is same as the first step. It’s for get the equation of the filing cell(s) and row numbers.

In 1st row 1 spaces.
In 2nd row 3 spaces.
In 3rd roe 5 spaces.

I think you catch the pattern. In each row we should print the stars following this equation.

Row Number + Row Number - 1= Number of stars

I’ll put second secondary for loop for handle this.

for(k=i+(i-1);k>=1;k--)
                        {
                                        printf("*");
                        }

d.      New Line

After each row we should print a new line(\n). In order to do that we will put a
printf("\n");
code for bottom of the secondary for loops.

Here’s the final code for this problem.

int main(void)
{
 int i=0;
 int j=0;
 int k=0;
 int l=0;
 int m=0;
 int h=0;
 
 printf("Enter hight of tree: ");
 scanf("%d",&h);
 
 for(i=1;i<=h;i++)
 {
  for(j=h-i;j>=1;j--)
  {
   printf(" ");
  }
  
  for(k=i+(i-1);k>=1;k--)
  {
   printf("*");
  }
  
  for(l=(h-i)*2;l>=1;l--)
  {
   printf(" ");
  }
  
  for(m=i+(i-1);m>=1;m--)
  {
   printf("*");
  }
  
  printf("\n");  
 }
}
I think you got some idea about how to solve these kind of problems. Now you can try different pattern by yourself.

Happy Coding……………………

Sunday, August 10, 2014

That's one small step for mankind, one giant leap for man

Last few days I'm trying to find a way to sharing my academic base experiences with the world, Last wednesday, if my memory is correct.mmmm Yes wednesday, We have an additional lecture, In this session show me the way to how to do it. Thank you ! Mr. Jaliya Udagedara, he is the person who lead me to show this path.
So "Hello world !"