Two-Dimensional Arrays
Two-Dimensional Arrays
Lecture 7
1
Contents:
Declaring, initializing 2-dimensional array.
Declaration and Initialization of a three dimensional array.
2
Two dimensional array can be considered as a table
that consists of rows and columns .
Each element in 2-d array is referred with the help of
two indexes.
One index is used to indicate the row and the second
index indicates the column of the element.
3
Data_type identifier[Rows][cols];
Identifier indicate the name of array
Rows indicates the number of rows in the array.
Cols indicates the number of columns in the array.
Int arr[4][3];
4
Declaring and & Initializing 2D Array
Similar to a 1D array, the elements of a 2D array can be initialized either
one at a time or all at once.
Syntax
Data_type arrName[size1][size2] = {{value1, value2, value3, …},
{value1, value2, value3, …},…..}
Int myArray[2][3] = {{10, 20, 30}, {40, 50, 60}}
The preceding example, the myArray is a 2D array containing 2 rows and
3 columns.
myArrya[0][0] = 10;
myArrya[0][1] = 20;
myArrya[0][2] = 30;
myArrya[1][0] = 40;
myArrya[1][1] = 50;
myArrya[1][2] = 60;
5
• In C programming, you can create array of an array known
as multidimensional array. For example,
• float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold
12 elements. You can think the array as table with 3 row and
each row has 4 column.
6
Declaring, initializing of a 2-dimension
array.
Declaration of two dimension array is:
Float c[2][3];
7
Accessing elements from 2d array
int main(){
int arr[4][4] = { {1,2,3,4},{2,3,4,1}, {3,4,1,2}, {4,1,2,3} };
int i, j, m=4, n=4;
printf("Elements of the array are:\n");
for(i=0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
8
Program
Write a program that store integer values in an array of 2 rows and 4 columns.
#include<stdio.h>
#include<conio.h>
main()
{
int arr[2][4];
9
Write a program that initializes two dimensional array of
3 rows and 3 columns and then display the max and
min number of the initialized array.
#include<iostream.h>
int main()
{
int arr[3][3],max,min;
for(int a=0;a<3;a++)
for(int b=0;b<3;b++)
{
cout<<"arr["<<a<<"]["<<b<<"]=";
cin>>arr[a][b];
cout<<endl;
}
max=min=arr[0][0];
for(int c=0;c<3;c++)
for(int d=0;d<3;d++)
{
if(arr[c][d]>max)
max=arr[c][d];
if(arr[c][d]<min)
min=arr[c][d];
}
cout<<"max="<<max<<"\t min="<<min<<endl;
system("pause");
}
Sum of 2D array elements
#include<stdio.h>
#include<conio.h>
int main(){
int arr[3][3], i, j, sum=0;
/*Accepts input from the user and stores it in 2-D array*/
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nEnter the value for A[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
/*Calculate sum of elements in 2-D array*/
for(i=0;i<3;i++){
for(j=0;j<3;j++){
sum=sum+arr[i][j];
}
}
/*Display the value of sum*/
printf("\nThe sum of the elements of 2-D array is %d", sum);
getch();
return 0;
}
Declaration and Initialization of a three
dimensional array.
You can initialize a three dimensional array in a similar
way like a two dimensional array. Here's an example,
[2] z
[3] rows
[4] columns
13
14
main(){
// this array can store 12 elements
int i, j, k, test[2][3][2];
printf("Enter 12 values: \n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j) {
for(k = 0; k < 2; ++k ) {
scanf("%d", &test[i][j][k]);
}
Example- C }
Program to store values }
entered by the user in a // Displaying values with proper index.
three-dimensional array printf("\nDisplaying values:\n");
for(i = 0; i < 2; ++i)
and display it.
{ for (j = 0; j < 3; ++j)
{ for(k = 0; k < 2; ++k )
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j
}
}
}
15
}
16