Must do pattern problems before starting DSA

In this article we are going to learn about the following content:-

  1. What are pattern problems?
  2. Why are they useful before starting DSA?
  3. Initial pattern code.
  4. Explanation.
  5. Must do pattern problems before starting DSA

What are pattern problems?

Almost everything we see in our daily life has a certain pattern to it i.e. stars have certain patterns in the sky, clouds have a certain pattern, and humans have different patterns.

Everything depends on the logic it has.

To master your coding skills or to crack the interview in a logical way patterns help you out.

Simply pattern problems define the logic used in printing patterns.

A pattern is known as a repeated series or a sequence. To solve these pattern problems we need to observe the problem fashion and think of the logic to solve it.

Problems related to math follow some rules in maths and patterns related to nature like stars following their structure and repeating sequences of them.

We need to observe the pattern and decode it.

Why are they useful before starting DSA?

Data Structure and Algorithm plays a key role in getting placed in multinational companies, to become master in DSA we need to have logical thinking. By doing multiple pattern problems our logical and thinking ability increases. Which helps us to complete the data structures and algorithms so efficiently.

See also  Collection in Java2

So as a beginner, we can’t directly get into the DSA. These patterns help us to get through the course so easily and smoothly.

Initial pattern code

Now let us see a pattern given below and the code, later we will discuss the logic behind it.

Print the following pattern:-

*****
*****
*****
*****
*****

Code for the problem:-

#include<iostream>
using namespace std;
int main()
{
    int n, i,j;
    cout<<"enter the number";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cout<<"*";
        }
        cout<<endl;
    }
}

Output:-

Enter the number:- 5

*****
*****
*****
*****
*****

Explanation:-

Step 1:-  In this given problem if we observe the pattern they asked us to print five rows and columns of stars.

Step 2:- By declaring  the integer value n;

Step 3:- Here we have taken “i” as rows and “j” as columns.

Step 4:-we declare them to the integer datatype.

Step 5:-  In the first for loop, we have taken rows equal to the given number.

Step 6:- In the second for loop, we have taken columns equal to the given number.

Step 7:- By incrementing in both rows and columns i.e. i++,j++. We print the output with the stars “*”. Until the i and j values get equal to the value of n. the iteration continues.

Step 8:- We use endl; to go to the new line.

Step 9:- And finally if we run our program the expected output executes.

More examples of the patterns

      Now let us see some examples of patterns that contain stars and numbers.

Pattern 1

	*
	* *
	* * *
	* *  * *
	* * * * *

let us see how to solve the above pattern, the increasing triangle.

See also  DATA STRUCTURES AND ALGORITHMS

code in c++

#include<iostream>
using namespace std;
int main()
{
    int n, i,j;
    cout<<"enter the number";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<"* ";
        }
        cout<<endl;
    }
}

Explanation

In the above code we have taken rows equal to the given number in the first for loop, whereas in the second for loop, we have taken columns equal to rows.

Because here we have to print the increasing triangle.

Initially, the i value is 1, so when it comes to the second for loop, j =i. j value becomes 1.

So one star will be printed, and later all the other iterations continue till i=n. and the increasing triangle is printed.

Pattern 2

       * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * *

Let us see how to solve the above pattern, the pyramid triangle.

Code in c++

#include<iostream>
using namespace std;
int main()
{
    int n, i,j;
    cout<<"enter the number";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n-i;j++)
        {
            cout<<"  ";
        }
        for(j=1;j<=2*i-1;j++)
        {
            cout<<"* ";
        }
        cout<<endl;
    } }

Explanation

In the above code, we have asked to print a pyramid triangle. It is as same as pattern 1,

But there is a slight change in the logic.

Here we should know about two things printing the spaces and printing stars and printing an odd number of stars.

In the second for loop, we have taken j = n-i.

Which is used to print the space.

Initially 5-1=4, so in the first line, four spaces get printed, and later the star prints.

Same repeats for the other iterations.

In the third for loop, j=2*i-1 is actually used to print the odd number stars.

Pattern 3

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Printing of numbers in increasing order, now this is a logical one let’s see how to solve it.

See also  Infosys Free Certification Course- Java Programming

Code in c++

#include<iostream>
using namespace std;
int main()
{
    int n, i,j,number=1;
    cout<<"enter the number";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<number<<" ";
            ++number;
        }
        
        cout<<endl;
    }
}

Explanation

In the above code, we have seen numbers increasing in order wise.

While declaring the i and j we have given number = 1,

The rest of the code is the same but after a second for loop,

While printing the output we have given to print the number and later incremented it. So that after each iteration in every new line, the value of the number increases in order wise.

Conclusion

Hurray! you have learned how to get the logic to code different patterns. In the next article, we will look at some basic math problems and their solutions using c++ code.

If you have any queries regarding this article, please do comment below and we will try to resolve it.

Thank you! Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.