Table of Contents
Introduction
2D Arrays in C means ‘two-dimensional’ Arrays in C.
You have heard about the term ‘Matrix’. The term is commonly used in mathematics. Matrix is a structure consisting of rows and columns in which we can arrange any number of elements. In programming languages, 2D arrays are nothing else but the matrix.
Declaration of the 2D array
A 2D array can be declared as:
int A[x][y];
In the above declaration, ‘A’ is an array of type ‘int’. It consists of ‘x’ number of rows and ‘y’ number of columns. Always the number of rows are written before the number of columns. If the declaration is like:
int A[y][x]; then ‘y’ represents the number of rows and ‘x’ represents the number of columns.
For example, we want to declare an array consisting of 2 rows and 4 columns then it is declared as:
int A[2][4];
and it is like
Column 0 | Column 1 | Column 2 | Column 3 | |
Row 0 | A[0][0] | A[0][1] | A[0][2] | A[0][3] |
Row 1 | A[1][0] | A[1][1] | A[1][2] | A[1][3] |
The values of rows and columns always start from 0. That’s for 2 rows the names to the rows given are ‘Row 0’ and ‘Row 1’. Similarly, for columns too.
Every element is written with row number first and then with the column number.
‘A[0][0]’ represents that it is the element of the 0th row and 0th column.
‘A[0][1]’ belongs to the 0th row and 1st column and so on.
Note: To find out the total number of elements in an array just do the product of the number of rows with the number of columns. In the above example, 2*4=8 elements are there in the array.
Assigning values to a 2D array
In a 1D array, there are two methods to assign values to the elements of the array. Similarly, in a 2D array, we can assign values in 2 ways as well.
Method 1: Declare the array first and then assign the values to the elements of the array.
Suppose we declare a 2D array and then one by one we assign value to the elements.
int n[2][2];
n[0][0]=50;
n[0][1]=40;
n[1][0]=30;
n[1][1]=10;
If no value is assigned to any element of the array, then its value will be 0 by default.
Method 2: Declaration of the array and assignment of values to the elements, both are done simultaneously.
int n[2][3]={1, 3, 5, 7, 9, 11};
or
int n[2][3]={
{1, 3, 5},
{7, 9, 11}};
Both statements are valid, we can use any of them at our convenience.
In the above declaration, the value of n[0][0] is 1, n[0][1] is 3, n[0][2] is 5, n[1][0] is 7, n[1][1] is 9, n[1][2] is 11.
Note: While making the declaration and assigning the values, in a 1D array, it is valid to keep the ‘size’ part of the array empty but in a 2D array, it is important to give at least the value of the second dimension i.e., you must specify the column number.
Applications of the 2D array
Since 1D arrays working fine then what is the need for 2D arrays. What is the use of a 2D array?
To record the performance of 5 students in 1 subject.
Here we can use a 1D array because we have different students but the subject is common for all.
It is like 1*5 i.e., 1 row and 5 columns which will make a 1D array.
To record the marks of 5 students in 3 subjects.
Suppose we have 5 students and each student is studying 3 subjects. The aim is to record the marks of all students in all subjects. Here we can’t use a 1D array.
The 2D array will help us to record the marks in the form of a matrix.
Program
#include<stdio.h> int main() { float marks[5][2]; int i,j; for(i=0;i<5;i++) { printf("Enter marks of student %d\n",(i+1)); for(j=0;j<2;j++) { printf("Subject %d\n",(j+1)); scanf("%f",&marks[i][j]); } } for(i=0;i<5;i++) { printf("Marks of student %d\n",(i+1)); for(j=0;j<2;j++) { printf("Subject %d: %f\n",(j+1),marks[i][j]); } } return 0; }
Output
The above output is something like,
Subject 1 | Subject 2 | |
Student 1 | 50 | 40 |
Student 2 | 90 | 80 |
Student 3 | 75 | 45 |
Student 4 | 53 | 42 |
Student 5 | 78 | 87 |
The code may look complicated but it is simple enough.
A 2D array named ‘marks’ is declared with dimensions 5(number of rows) and 2(number of columns).
The first ‘for’ loop is used to take inputs from the user.
An inner ‘for’ loop is used to make a 2D array. When ‘i’ is 0 ‘j’ iterate from 0 to 1, thus we enter the value of marks of student 1(with i=0) in both (j=0 & j=1) subjects. Then the value of ‘i’ is updated to 1 and then we enter the data of student 2. The loop continues till the value of ‘i’ reaches 4 and ended with the data of student 5.
Then again, we use the same process of using an outer ‘for’ loop and an inner ‘for’ loop to print the data entered by the user.
0 Comments