×

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer believes and the audience would like to believe we see when they type in the code in their compilers a greenish output keeps falling without stopping, we will try to implement this now using a C++ programming language.

The technique is generally coined as the falling matrix technique; the output which will be printed, or so to say, which keeps printing, will have no effect if we allow it to be displayed in the console but should immediately be terminated as it will be printed until our desktop or laptop's memory is complete which may have a severe effect on our system hardware part.

Implementation of the falling matrix takes place in three main steps they are as follows.

1. We must be mindful of defining the width of the matrix.

2. Any two successive symbols or characters may or may not have the same amount of gap as we may expect them to be.

3. We can expect a certain amount of the output printed onto our respective consoles may be delayed for various reasons.

Note that the program sometimes would not be able to run on your local IDEs for various reasons, one such being system might be disabled in such sensitive cases; if possible, try fixing it or, in such cases, use the below code snippet on your GCC to avoid the error while compiling the falling matrix program in C++ programming language which will then work perfectly fine without any errors and program being disabled by your personal local computing system's hardware.

Code Snippet

$ g++ -std=c++11 abc.cpp -o falling.o
$ falling.o

Code

//Here, we are writing the C++ program to implement a falling matrix.
#include<iostream>
#include<string>
#include<thread>
#include<cstdlib>
#include<ctime>
#include<chrono>
const int width_here = 170;
// Defines the number of flips in Boolean Array 'switches'
const int flipsperline =115;
// Delay between two successive line print
const int sleepTime = 10;
using namespace std;
int main(){
    int i=0, x=0;
    srand(time(NULL));
    bool switches[width] = {0};
    const string ch = "1234567890qwertyuiopasdfghjkl"
                                   "zxcvbnm,./';[]!@()-=_+";
    const int l = ch.size();
    system("Color 0A");
    while (true){
        for (i=0;i<width;i+=2){
            if (switches[i])
                cout << ch[rand() % l] << " ";
            else
                cout<<"  "; }


        for (i=0; i!=flipsPerLine; ++i)
        {
            x = rand() % width;
            switches[x] = !switches[x];
        }
        cout << endl;
        this_thread::sleep_for(chrono::milliseconds(sleepTime));
    }
    return 0;
}

Output

  g   h 5 &   y   , 8   7     e     * 
o       7 d v               #   b   0 h 0   '   # t * ,           l 
e     k o n _ e   &         3   b   0 o q   c   + ] a v w         5 
z   ! 6 ^ _ - @   j   '     6   4   - b e   + h . b m w           v 
&   1 # 7 n b 9   ;   ;     8   )   k   b   m & . c [ 9   x       q 
[   7 6 v 2 r ^   $   p     j   /       @   h   s ( a 9   k       t 
m ]   m 2 % ( _ c   1   p     8   2           v   t 7 w +   4       _ 
p o   8 3 8 ; b 3   h   l p y d b %           /   - r ' @   k       5 
% 5   t ' ; b 6 ]   +   # n i x 6 ,           #   k r ( g   h       8 
. b f % ; @ s d k   =   ! e e ^ $ x           s   w h h =           / 
m . t 6 i 7 l g $   c   # k 2 2 x             z   r s m !           2 
6 9 ^ & - j # j % d v   n $ / 0 *             9 q   ( = +         f o 
s h d ] * r n $ ' ' c   z 5 d 8 0 4     v     3 l   ]   e     h   [ m 
2   o ] g 5 ( . = + ,   , x   g   +     )     % 9       y d   j   ! g 
o   -   l n t $ m = *   z n   8   l     z ! i r !       r p       q @ 
u   g   ]   % ] _ o a   e f   t   0     b 4 3 ; c       ^ ]       9 5 
+   q   =   ; 4 r   9   6 p   *   4     -   z c a       4 ;       e 7 
And it keeps going on like this...

Related Topics

Counting Frequencies of Array Elements in C++

We have an array of integer items with duplicate values, and our objective is to compute the frequencies of the different elements in the array. Methods: Methods that can be used to...

3 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

C++ Global Variable

In C++, a global variable is a variable that is defined outside of any function or class and can be accessed by any function or class in the program. Global...

4 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

Reserved Keywords in C++

What are reserved keywords in C++? There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved...

15 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Features and Use of Pointers in C/C++

What is a pointer? A pointer is mainly used to store the address of another variable. The * operator creates a pointer variable, which points to a data type (like an...

7 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

C++ int into String

Data type conversion is a standard editing process. You may need to convert variable from one type of data to another in a variety of situations. There are two ways...

5 minutes read.

Differences Between C Structures and C++ Structures

The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance...

3 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.