In this video tutorial, two of the more patterns have been discussed that relates to the similar pattern problems. What's different about these are that they also include spaces before the stars as a part of the pattern to be printed. Having this extra sub pattern to track spaces in the desired pattern requires an extra loop to track and control it.
For a brief review, We discussed that these patterns if observed carefully are just representation of tables i.e. a combination of rows and columns. And few of the cells in that table are populated with some data according to a logically programmed algorithm with represents a pattern.
In these cases, the red triangles shows us the spaces represented by "sp" and therefore unlike the previous pattern these need to be managed by the loop as they occur before the stars in the pattern. So, taking the same approach of rows and columns managing loops, we will have an outer loop that tracks the rows which will start at 0 and goes upto the number of rows which in these cases is 5 (excluding 5 since we are starting from 0).
Now we can traverse through each and every row one by one. While we are in each row we need two things to be done :
In the first pattern, the spaces in each row can be calculated by subtracting the row number from maximum number of rows eg. row 1 : space => 5 - 1 = 4, row 2 : space = 5 - 2 = 3. In the second pattern the number of spaces are equal to row number - 1.
In the first pattern, the stars can be calculated by starting from 0 and going upto row number (excluding row number since we start from 0). In the second pattern, the number of stars can be calculated by subtracting row number - 1 from maximum number of rows eg. row 1 : stars => 5 - 0 = 5, row 2 : stars => 5 - 1 = 4.
Since we have figured out the logic for all the loops needed, we can just lay them down in order and the desired patterns will be printed.
Test Code : (Use this code to produce the desired patterns)
For pattern 1 =>
#include<stdio.h>
int main()
{
for (int i = 0; i < 5; ++i) //rows 0,1,2,3,4
{
for (int s = i; s < 4; ++s) //spaces 0,1,2,3
{
printf(" ");
}
for (int j = 0; j <= i; ++j) //stars
{
printf("*");
}
printf("\n");
}
return 0;
}
For pattern 2 =>
#include<stdio.h>
int main()
{
for (int i = 0; i < 5; ++i) //rows 0,1,2,3,4
{
for (int s = 0; s < i; ++s) //space
{
printf(" ");
}
for (int j = i; j < 5; ++j) //star 0,1,2,3,4
{
printf("*");
}
printf("\n");
}
return 0;
}
NOTE: Few more of the examples of such problems will be discussed soon. Although it is
highly recommended to try out few of the more complex examples on your own after you
have understood the concept of how it works.
Watch the video below to know more.
For a brief review, We discussed that these patterns if observed carefully are just representation of tables i.e. a combination of rows and columns. And few of the cells in that table are populated with some data according to a logically programmed algorithm with represents a pattern.
In these cases, the red triangles shows us the spaces represented by "sp" and therefore unlike the previous pattern these need to be managed by the loop as they occur before the stars in the pattern. So, taking the same approach of rows and columns managing loops, we will have an outer loop that tracks the rows which will start at 0 and goes upto the number of rows which in these cases is 5 (excluding 5 since we are starting from 0).
Now we can traverse through each and every row one by one. While we are in each row we need two things to be done :
- Print the spaces
- Print the stars (any data according to the pattern)
In the first pattern, the spaces in each row can be calculated by subtracting the row number from maximum number of rows eg. row 1 : space => 5 - 1 = 4, row 2 : space = 5 - 2 = 3. In the second pattern the number of spaces are equal to row number - 1.
In the first pattern, the stars can be calculated by starting from 0 and going upto row number (excluding row number since we start from 0). In the second pattern, the number of stars can be calculated by subtracting row number - 1 from maximum number of rows eg. row 1 : stars => 5 - 0 = 5, row 2 : stars => 5 - 1 = 4.
Since we have figured out the logic for all the loops needed, we can just lay them down in order and the desired patterns will be printed.
Test Code : (Use this code to produce the desired patterns)
For pattern 1 =>
#include<stdio.h>
int main()
{
for (int i = 0; i < 5; ++i) //rows 0,1,2,3,4
{
for (int s = i; s < 4; ++s) //spaces 0,1,2,3
{
printf(" ");
}
for (int j = 0; j <= i; ++j) //stars
{
printf("*");
}
printf("\n");
}
return 0;
}
For pattern 2 =>
#include<stdio.h>
int main()
{
for (int i = 0; i < 5; ++i) //rows 0,1,2,3,4
{
for (int s = 0; s < i; ++s) //space
{
printf(" ");
}
for (int j = i; j < 5; ++j) //star 0,1,2,3,4
{
printf("*");
}
printf("\n");
}
return 0;
}
NOTE: Few more of the examples of such problems will be discussed soon. Although it is
highly recommended to try out few of the more complex examples on your own after you
have understood the concept of how it works.
Watch the video below to know more.
Comments
Post a Comment