×

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

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

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.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 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.

Top best IDEs for C/C++ Developers in 2024

Nothing in the current digital world is conceivable without programming. Everything needs programming, from the cell phones in our pockets to self-driving cars. Programming is also necessary for the mouse...

9 minutes read.

C++ Bitset

Overview In C++, bitset represents a fixed-sequence of some bits values by either 0 and 1. Zero represents the value as false or unset, while 1 represents the value as true...

4 minutes read.