C++: Creating Matrix-Style Effects

Creating Matrix-Style Effects

In this C++ video tutorial, we demonstrate how to create Matrix-style effects, using the material from our previous C++ Console lessons. This program gives an excellent introduction to random numbers with the generated number data is shown visually. The program uses modular arithmetic to give a simplified version of a random number generator. The final program C++ code is listed below.

ASCII Character Range

The program begins with two functions: Modulus() and GetChar(); we explained functions in C++ Console Lesson 12. The Modulus() function returns the remainder of the integer division of iN by iMod. For example, if iN = 10 and iMod = 7, then the function Modulus() returns 3. The Modulus() function is used by the GetChar() function to generate character values in the range [cBase, cBase - iRange - 1]. For example, if iGenerator = 52, cBase = '0' and iRange = 10, then the call to Modulus(iGenerator, iRange) returns 2, so that the call GetChar(52, '0', 10) returns '2'. Note that the single quotre marks indicate character values so that '2' = 50 in the ASCII table.

In the main() function, we begin with the array declaration of caRow; we explained arrays in C++ Console Lesson 10. This array is 80 characters long and is used to hold an entire row of characters at a time. Next, we declare four integer indices: j, k, l, and m. These four integers are declared outside of the next while loop so that they retain their values within the scope of the loop; we covered the rules of scope in C++ Console Lesson 11.

The outer while loop uses the value true as its conditional so that it continues to loop forever. Inside that loop, we have another while that generates each of the characters in the row. We use a chack to make sure that we do not write over spaces. If the character is not a space, we call GetChar() to generate a "random" character in the range of ASCII values from '!' to '>'. The value j + i*i just happens to work well. Finally, we output the character that we just generated.

After the loop, we have four statements that update the values of our four integers, which are them used to place either a space or '-' in order to end or start a streak of characters. The final while loop is just a delay that run 300,000 times. Notice that the value from the call to GetChar() is never used because we are merely trying to slow down the output. Although this lesson uses white text, we will have a C++ Miscellaneous lesson that continues to the next step of adding green text. as shown below

Next Step in C++ Matrix Effect
#include <iostream>

int Modulus(int iN, int iMod) {
	int iQ = (iN/iMod);
	return iN - (iQ*iMod);

char GetChar(int iGenerator, char cBase, int iRange) {
	return (cBase + Modulus(iGenerator, iRange));

int main() {
	char caRow[80];
	int j = 7;
	int k = 2;
	int l = 5;
	int m = 1;
	while (true) {
		int i = 0;
		// Output a random row of characters
		while (i < 80) {
			if (caRow[i] != ' ') {
				caRow[i] = GetChar(j + i*i, 33, 30);
			std::cout << caRow[i];
		j = (j + 31);
		k = (k + 17);
		l = (l + 47);
		m = (m + 67);
		caRow[Modulus(j, 80)] = '-';
		caRow[Modulus(k, 80)] = ' ';
		caRow[Modulus(l, 80)] = '-';
		caRow[Modulus(m, 80)] = ' ';
		// Delay
		i = 0;
		while (i < 300000) {
			GetChar(1, 1, 1);
    return 0;

If we compile and execute the program, we see the Matrix-style output:

C++ Matrix Program Output