C++ Basics Part II — Grip on Language
I assume you have knowledge of Loops, Arrays, Matrices, Data-Type, Functions.
(If not take a look at PART-I and start reading from the textbook)
However knowledge of only loops will be enough for this article.
I strongly recommend you to try and run all code simultaneously while you read and understand from the article.
Let’s Begin
I have often found the best way to practice and get a good grasp at loops is by making patterns.
Pattern 1:
Print this pattern**********
**********
**********
**********
Program:
#include<iostream>
using namespace std;int main() {
cout<<“**********”<<endl;
cout<<“**********”<<endl;
cout<<”**********”<<endl;
cout<<“**********”<<endl; return 0;
}This works, but we can do better. Remember a program is most useful where you have to do something repeatedly.#include<iostream>
using namespace std;int main() { for(int i=0;i<4;i++)
cout<<“**********”<<endl; return 0;
}We can do still better. Remember a program is most useful where you have to do something repeatedly.#include<iostream>
using namespace std;int main() { for(int i=0;i<4;i++) {
for(int j=0;j<10;j++) {
cout<<“*";
}
cout<<endl; // next line after printing the complete row
} return 0;
}You are printing '*' for 10 times for a row, which is printed 4 times.It can also be seen as j 0 1 2 3 4 5 6 7 8 9
i
0 00 01 02 03 04 05 06 07 08 09
1 10 11 12 13 14 15 16 17 18 19
2 20 21 22 23 24 25 26 27 28 29
3 30 31 32 33 34 35 36 37 38 39you have to print * on each location produced by combination of i & j. (you will see why this approach is important)
Pattern 2:
Print this pattern*
*
*
*
*
Program:
j 0 1 2 3 4
i
0 00 01 02 03 04
1 10 11 12 13 14
2 20 21 22 23 24
3 30 31 32 33 34
4 40 41 42 43 44you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal.
Yes i == j is true for this diagonal.#include<iostream>
using namespace std;int main() {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(i==j) // The required condition
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Pattern 3:
Print this pattern *
*
*
*
*
Program:
j 0 1 2 3 4
i
0 00 01 02 03 04
1 10 11 12 13 14
2 20 21 22 23 24
3 30 31 32 33 34
4 40 41 42 43 44you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal.
Yes i + j == 4 is true for this diagonal.#include<iostream>
using namespace std;int main() {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(i+j==4) // The required condition
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
You’re now equipped to take down all the patterns.
Pattern 4:
Print this pattern *
* *
* * *
* * * *
* * * * *
Program:
j 0 1 2 3 4
i
0 00 01 02 03 04
1 10 11 12 13 14
2 20 21 22 23 24
3 30 31 32 33 34
4 40 41 42 43 44you have to print * on locations that are highlighted. Can you find a property that holds true for the diagonal.The diagonal holds property i+j == 4, anything below the diagonal will have higher value of i and j than on the diagonals therefore
the required condition is i+j >= 4#include<iostream>
using namespace std;int main() {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(i+j>=4) // The required condition
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Pattern 5:
Print this pattern* * * * *
* * * *
* * *
* *
*
Program:
j 0 1 2 3 4
i
0 00 01 02 03 04
1 10 11 12 13 14
2 20 21 22 23 24
3 30 31 32 33 34
4 40 41 42 43 44you have learned the trick now, try it yourself.#include<iostream>
using namespace std;int main() {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(j>=i) // The required condition
cout<<"*";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}The diagonal holds property i==j, anything on right of the diagonal would have higher value of j than on the diagonal.
Pattern 6:
Print this pattern* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Program:
j 0 1 2 3 4 5 6 7 8
i
0 00 01 02 03 04 05 06 07 08
1 10 11 12 13 14 15 16 17 18
2 20 21 22 23 24 25 26 27 28
3 30 31 32 33 34 35 36 37 38
4 40 41 42 43 44 45 46 47 48Often times a pattern can be broken into simpler patterns that we already know about. Like in this case j 0 1 2 3 4 4 5 6 7 8
i
0 00 01 02 03 04 04 05 06 07 08
1 10 11 12 13 14 14 15 16 17 18
2 20 21 22 23 24 24 25 26 27 28
3 30 31 32 33 34 34 35 36 37 38
4 40 41 42 43 44 44 45 46 47 48you have learned the trick now, try it yourself.#include<iostream>
using namespace std;int main() {
for(int i=0;i<5;i++) {
for(int j=0;j<9;j++) {
if(j>=i && j<=4) // The required condition for 1st part
cout<<"*";
else if(i+j<=8 && j>4) // condition for 2nd part
cout<<"*"; // observe j>4 and not j>=4
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Pattern 6:
Print this pattern* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Try this by yourself.
Pattern 7: (Try this by yourself first, then see the code)
Print this pattern 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Program:
j 0 1 2 3 4 5 6 7 8
i
0 00 01 02 03 04 05 06 07 08
1 10 11 12 13 14 15 16 17 18
2 20 21 22 23 24 25 26 27 28
3 30 31 32 33 34 35 36 37 38
4 40 41 42 43 44 45 46 47 48#include<iostream>
using namespace std;
int main() {
for(int i=0;i<5;i++) {
int n = 1; // to start printing from 1 for(int j=0;j<=4;j++) {
if(i+j>=4) {
cout<<n; // print and increments till middle
n++;
} else {
cout<<" ";
}
}
n--; // adjust the value to start decreasing
for(int j=5;j<10;j++) {
if(j-i<=4) {
n--; // print and decrements after middle
cout<<n;
} else {
cout<<" ";
}
} cout<<endl;
}
return 0;
}
If you are here and have tried generating all the above patterns. You’re ready to tackle all the patterns that you’ll ever face.