Pattern printing coding technique

Dilip Kumar
10 min readOct 13, 2024

--

How to write code to print the following pattern on screen?

Step#1: Find out canvas size

First thing we need to find out is the size of canvas. For above problem we have 5*26 canvas size.

Q. For practice, what would be the canvas size for following pattern?

Ans. It would be 5*5

Step #2: Positional logic

Every position (i,j) will have it’s own logic. In general, we need to apply co-ordinate geometry to implement the positional value of the cell.

We can apply equation for line to print above grid as below.

string cellValue (int r, int c, int i, int j) {
if( i == 4) {
return "*";
}
if(j == 2) {
return ".";
}
return " ";
}

We can write following code to print following simple pattern.

string cellValue (int r, int c, int i, int j) {
if(i >=j) { // Lower bottom will print *
return "*";
}
return " ";
}

Similarly, we can take a general line equation and print it.

y = m*x + c, here (x,y) is co-ordinate on line and m is slope. 
i = m*j + c, here converted into above code pattern, here j represent x axis
and i represent inverted y axis.

Note: Generally in co-ordinate geometry, we keep (0,0) at left bottom corner.
Here, we keep (0,0) at left top corner.

Let’s take following line as sample.

y = 2x + 1;

We can write following code

string cellValue (int r, int c, int i, int j) {
if( i== 2*j + 1) {
return "*";
}
return ".";
}

It prints following for 10*10 grid.

..........
*.........
..........
.*........
..........
..*.......
..........
...*......
..........
....*.....

Let’s write code to print pyramid for following

We can write code as below.

string cellValue (int r, int c, int i, int j) {
if(i >= 4 -j && i >= j -4) {
return "*";
}
return ".";
}

It will print following for 5*9 grid.

....*....
...***...
..*****..
.*******.
*********

Following is code to print inverted pyramid.

string cellValue (int r, int c, int i, int j) {
if(i <= j && i <= 8 -j) {
return "*";
}
return ".";
}

Following is output for 5*9 grid.

*********
.*******.
..*****..
...***...
....*....

Q. How to repeat above pattern to print as following?

Step #3: Repeat the pattern

To repeat the pattern, we need to find out the monomer (chemistry concept) and repeat it to build the polymer(chemistry concept again).

Here to repeat, we need to apply mod(j%8) as below.

#include <bits/stdc++.h>
using namespace std;

string cellValue (int r, int c, int i, int j) {
if(i <= j && i <= 8 -j) {
return "*";
}
return ".";
}

void solve(int r, int c) {
for (int i=0; i <r; i++) {
for (int j=0; j < c; j++) {
cout<<cellValue(r,c,i,j%8);
}
cout << endl;
}
}
int main()
{
int r, c;
cin >>r >> c;
solve(r,c);
return 0;
}

Following is pattern for 5*50 grid.

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

We can use following code to just print the line instead of filling it.

string cellValue (int r, int c, int i, int j) {
if(i == j || i == 8 -j) {
return "*";
}
return " ";
}

It prints following.

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

We can rewirte modulo operation within cellValue method as below too. It works same.

string cellValue (int r, int c, int i, int j) {
if(i == j%8 || i == 8 -j%8) {
return "*";
}
return " ";
}

Why this works?

We initially wrote correct logic for j in [0,7] as below

Here for every (i,j) we are repeating the logic for (i,j%8) that helps to repeat.

Interestingly, if we print on 50*50 grid we get following. This is due to bigger canvas let lines to grow.

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

We can similarly repeat vertically by appling i%8 as below.

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

We can do i%5 to repeat first 5 pattern as below.

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

Going back to original problem to print a---z , we can simply use j value to print the alphabet as below.

char cellValue (int r, int c, int i, int j) {
if(i == j%8 || i == 8 -j%8) {
return j + 'a';
}
return ' ';
}

For canvas 5*26 it will print below.

a       i       q       y 
b h j p r x z
c g k o s w
d f l n t v
e m u

Q. Can we print christmas tree as below?

Q. What will be the canvas size here?

Ans. It would be (5*4)*9 i.e 20*9 . It means vertically we need to repeat first 5 followed by last 5 for bar.

Let’s first print the first 5*9 i.e. tree upper part.

char cellValue (int r, int c, int i, int j) {
if(i >= 4 - j && i >= j -4) {
return '*';
}
return ' ';
}

It will print following for 5*9 grid.

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

Let’s repeat vertically three times on 15*9 grid.

char cellValue (int r, int c, int i, int j) {
if(i%5 >= 4 - j && i%5 >= j -4) {
return '*';
}
return ' ';
}

It will print following.

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

To print the bar we need j=3 and j=5 line as below.

char cellValue (int r, int c, int i, int j) {
// Add bar at the bottom i.e. after i>15
if(i >= 15) {
if(j>=3 && j <=5) {
return '*';
}
return ' ';
}
if(i%5 >= 4 - j && i%5 >= j -4) {
return '*';
}
return ' ';
}

It will print following pattern on 20*9 grid.

#include <bits/stdc++.h>
using namespace std;

char cellValue (int r, int c, int i, int j) {
// Add bar at the bottom i.e. after i>15
if(i >= 15) {
if(j>=3 && j <=5) {
return '*';
}
return ' ';
}
if(i%5 >= 4 - j && i%5 >= j -4) {
return '*';
}
return ' ';
}

void solve(int r, int c) {
for (int i=0; i <r; i++) {
for (int j=0; j < c; j++) {
cout<<cellValue(r,c,i,j);
}
cout << endl;
}
}
int main()
{
int r, c;
cin >>r >> c;
solve(r,c);
return 0;
}

This post is based on https://maang.in/ taught by great instructor Vivek Gupta. Feel free to buy his dam cheap course (I don’t know how he runs his business with such a cheap course with the best quality of content). Thanks to him for keep his course cheap as well as many content free on YouTube :-)

--

--

Dilip Kumar
Dilip Kumar

Written by Dilip Kumar

With 18+ years of experience as a software engineer. Enjoy teaching, writing, leading team. Last 4+ years, working at Google as a backend Software Engineer.

No responses yet