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