C Programming
In this tutorial we will learn about two dimensional array in C programming language.
We have already covered about what are arrays in the previous tutorial. Now, lets explore the two dimensional arrays.
So, from the previous tutorial we know that when we want to store one dimensional data like score of a single player in 5 matches we use one dimensional array.
Now, if we want to store two dimensional data like storing respective scores of 4 players for the 5 matches then we need the two dimensional array.
To create a two dimensional array we use the following syntax.
type arrName[rows][cols];
Example
int score[4][5];
In the above example we are creating an array named score
of type int
. It has 4 rows and 5 columns i.e., total 4x5 = 20 elements.
So, the above code will instruct the computer to allocate memory to store 20 elements of type int and we can represent the score array as shown below.
Array indexing starts from 0 so, in the above image there are 4 rows having index 0, 1, 2 and 3. And there are 5 columns having index 0, 1, 2, 3 and 4.
We can assign value to a 2D array by targeting the cell using the row-index and col-index.
For example, if we want to assign 10 to element at row-index 0 and column-index 0 then we will write the following code.
score[0][0] = 10;
Similarly, if we want to assign lets say 50 to an element of an array at row-index 1 and column-index 3 then we will write the following code.
score[1][3] = 50;
There are a couple of ways we can create and initialise a 2D array in C.
In the following code we are creating a 2D array bonus
of type int
having 2 rows and 3 columns.
int bonus[2][3] = {
1, 2, 3,
4, 5, 6
};
In the above code 1, 2, 3
is for the first row and 4, 5, 6
is for the second row of the array bonus.
We can achive the same result by separating the rows with curly brackets { }
as show below.
int bonus[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
We can also skip elements and they will be auto filled with 0s.
In the following example we are creating an array bonus of type int having 2 rows and 3 columns but this time we are initialising only few elements.
int bonus[2][3] = {
{1, 2},
{3}
};
The above code will create the bonus array having 2 rows and 3 columns and we will get the following initialisation.
int bonus[2][3] = {
{1, 2, 0},
{3, 0, 0}
};
If we want to initialise all elements of a row to lets say 0 then we can write the following.
int bonus[2][3] = {
{0},
{0}
};
So, the above code will give us the following result.
int bonus[2][3] = {
{0, 0, 0},
{0, 0, 0}
};
Now, before we end this tutorial lets write a program in C to take score of 4 players for 5 matches and print them out.
/**
* file: 2d-array.c
* author: yusuf shakeel
* date: 2010-12-21
* description: 2d array program
*/
#include <stdio.h>
int main(void)
{
//variable
int
score[4][5],
r, c;
//user input
for (r = 0; r < 4; r++) {
printf("Enter 5 scores of Player #%d: ", (r + 1));
for (c = 0; c < 5; c++) {
scanf("%d", &score[r][c]);
}
}
//output
printf("You have entered the following:\n");
for (r = 0; r < 4; r++) {
printf("---------- Score of Player #%d ----------\n", (r + 1));
for (c = 0; c < 5; c++) {
printf("Match #%d\tScore: %d\n", (c + 1), score[r][c]);
}
}
printf("End of code\n");
return 0;
}
Output
Enter 5 scores of Player #1: 11 12 13 14 15
Enter 5 scores of Player #2: 21 22 23 24 25
Enter 5 scores of Player #3: 31 32 33 34 35
Enter 5 scores of Player #4: 41 42 43 44 45
You have entered the following:
---------- Score of Player #1 ----------
Match #1 Score: 11
Match #2 Score: 12
Match #3 Score: 13
Match #4 Score: 14
Match #5 Score: 15
---------- Score of Player #2 ----------
Match #1 Score: 21
Match #2 Score: 22
Match #3 Score: 23
Match #4 Score: 24
Match #5 Score: 25
---------- Score of Player #3 ----------
Match #1 Score: 31
Match #2 Score: 32
Match #3 Score: 33
Match #4 Score: 34
Match #5 Score: 35
---------- Score of Player #4 ----------
Match #1 Score: 41
Match #2 Score: 42
Match #3 Score: 43
Match #4 Score: 44
Match #5 Score: 45
End of code
ADVERTISEMENT