Czech version
logolink

< Back to the list of lessons

Two-dimensional Array (2D Array)

AlgortimyContent of the lesson:

  • Principle of 2D Array
  • Writing 2D Array to Screen
  • Filling in 2D Array

Principle of 2D Array

A 2D array uses another dimension compared to a standard array. You can see an illustration of an empty 2D array (5x5):

         
         
         
         
         

A perfect analogy is a list of squared paper with dimensions m x n squares.

Writing 2D Array to Screen

Take a look at writing an empty 2D array to screen:

Writing an empty 2D array to screen
#include "stdafx.h"
                
#define M 2
#define N 4

int _tmain(int argc, _TCHAR* argv[])
{
	int p[M][N];
    int radek, sloupec;
    
    for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        	p[radek][sloupec] = 0;
        }
    }
    
    for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        	printf("%d ", p[radek][sloupec]);
        }
    }

	fflush(stdin);
    getchar();
    return 0;
}

Numbers are written all together and you absolutely cannot see that this is a 2D array. To illustrate this fact you have to use another writeln command after writing every line to console.

Writing an empty 2D array to screen
#include "stdafx.h"
                
#define M 2
#define N 4

int _tmain(int argc, _TCHAR* argv[])
{
	int p[M][N];
    int radek, sloupec;
    
    for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        	p[radek][sloupec] = 0;
        }
    }
    
    for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        	printf("%d ", p[radek][sloupec]);
        }
        printf("\n");
    }

	fflush(stdin);
    getchar();
    return 0;
}

Filling 2D Array

In the following examples we will try to fill a square 2D array (dimensions set to nxn) with numbers. To be able to write it to screen, choose the dimensions to 10x10 items.

The basic algorithm to fill an array with numbers
#include "stdafx.h"

#define M 10
#define N 10

int _tmain(int argc, _TCHAR* argv[])
{
	int p[M][N];
    int radek, sloupec;
                
	for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        
        }
    }
    
    for (radek = 0; radek < M; radek++)
    {
    	for (sloupec = 0; sloupec < N; sloupec++)
        {
        	printf("%d ", p[radek][sloupec]);
        }
        printf("\n");
    }
	
    fflush(stdin);
    getchar();
    return 0;
}                

Ones Everywhere

111
111
111

Basic algorithm for filling 2D array with numbers
p[radek, sloupec]=1;

Numbers 1..n in Rows

123
123
123

Basic algorithm for filling 2D array with numbers
p[radek, sloupec]=sloupec;

Numbers 1..n in Columns

111
222
333

Basic algorithm for filling 2D array with numbers
p[radek][sloupec]=radek;

Small Multiplying Table

123
246
369

Basic algorithm for filling 2D array with numbers
p[radek][sloupec]=radek*sloupec;

Ones on the Diagonal Line

100
010
001

Basic algorithm for filling 2D array with numbers
if (radek==sloupec)
{
   p[radek, sloupec]=1;
}
else
{
   p[radek, sloupec]=0;
}

Ones on the Diagonal Line and Above It

111
011
001

Basic algorithm for filling 2D array with numbers
if (radek<=sloupec)
{
   p[radek, sloupec]=1;
}
else
{
   p[radek, sloupec]=0;
}

Ones on the Both Diagonal Lines

101
010
101

Basic algorithm for filling 2D array with numbers
if ((radek=sloupec) || (radek=M-sloupec+1))
{
   p[radek][sloupec]=1;
}
else
{
   p[radek][sloupec]=0;
}

Individual Task I

Write a program to compute the arithmetic mean of all inserted values inside a 2D array.

Individual Task II

Write a program to find out the minimum and the maximum values from all inserted values in a 2D array.

Questions

  1. Name several suitable situations when 2D arrays can be used.
webdesign, xhtml, css, php - Mgr. Michal Mikláš