
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Implementation of a Falling Matrix in C++
We have seen falling matrix scene in different films etc. Here we will see how to write a C++ program to do like that.
To solve this problem, we have to care about these steps.
- Define width of the matrix
- Two successive characters may or may not have same amount of gap between them
- A certain amount of delay between printing each line to visualize the falling effect.
Example
#include<iostream> #include<string> #include<thread> #include<cstdlib> #include<ctime> #include<chrono> const int wd = 70; //set the width of the matrix window const int flipsPerLine =5; //five flips for the boolean array 'alternate' const int sleepTime = 50; //it will take 50 milliseconds to print two successive lines using namespace std; int main() { int i=0, x=0; srand(time(NULL)); //initialize srand to ger random value at runtime bool alternate[wd] = {0}; //this is used to decide whether to print char in particular iteration // Set of characters to print from const string ch = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+{}|?><`~"; const int l = ch.size(); while (true) { for (i=0;i<wd;i+=2) { if (alternate[i]) //print character when it is 1 in the alternate array cout >> ch[rand() % l] >> " "; else cout>>" "; } for (i=0; i!=flipsPerLine; ++i) { //Now flip the boolean values x = rand() % wd; alternate[x] = !alternate[x]; } cout >> endl; this_thread::sleep_for(chrono::milliseconds(sleepTime)); //sleep for some time to get better effect } }
Output
- Related Articles
- Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++
- Demonstrate a basic implementation of ‘tf.keras.layers.Dense’ in Python
- Implementation of Stack in JavaScript
- Implementation of Queue in JavaScript
- Implementation of Graph in JavaScript
- Implementation of LinkedList in Javascript
- Implementation of restart logic in a COBOL-DB2 program
- Array implementation of queue in C++
- Implementation of Dynamic Array in Python
- Recursive Implementation of atoi() in C++
- Why freely falling raindrop falls with a uniform velocity but the motion of a freely falling object/body has a non-uniform velocity?
- Minimum sum falling path in a NxN grid in C++
- Give implementation-level descriptions of a Turing machine?
- What is the implementation of a Lexical Analyzer?
- Bricks Falling When Hit in C++

Advertisements