0% found this document useful (0 votes)
2 views18 pages

Lecture 5 2darray

This document provides an overview of two-dimensional arrays in C programming, including their definition, declaration, initialization, and examples of operations such as matrix addition and calculating the sum of even and odd elements. It includes sample code for various tasks involving 2D arrays, such as finding the sum of left diagonals, displaying lower triangular matrices, calculating determinants, and checking matrix equality. Additionally, it offers references for further learning and assessment questions related to the topic.

Uploaded by

ashutoshjha.mail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

Lecture 5 2darray

This document provides an overview of two-dimensional arrays in C programming, including their definition, declaration, initialization, and examples of operations such as matrix addition and calculating the sum of even and odd elements. It includes sample code for various tasks involving 2D arrays, such as finding the sum of left diagonals, displaying lower triangular matrices, calculating determinants, and checking matrix equality. Additionally, it offers references for further learning and assessment questions related to the topic.

Uploaded by

ashutoshjha.mail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEPARTMENT OF

onec

COMPUTER SCIENCE & ENGINEERING

Subject Name Basics Data Structure


Subject Code 25CSH-103
Semester 1
Mapping With Co’s CO2, CO3

LESSON-5 2-Dimensional Array

LESSON OBJECTIVES

• To learn how two-dimensional array is defined.


• To learn how to code and run a program that processes a two dimensional array.

STRUCTURE OF THE LESSON


• Concepts of 2D array
• declaration and initialization of 2D arrays
• Examples of 2D array
• References
• FAQs
• Discussion forum
Concept of 2D array: A two-dimensional array is an array where its elements are selected
(identified) using two indices. In 2-D array, to declare and access elements of a 2-D array we
use 2 subscripts instead of 1.

Syntax: datatype array_name[ROW][COL];

The total number of elements in a 2-D array is ROW*COL.

Example: int a[m][n];

In rectangular 2-dimensional arrays, m number of rows and n number of columns in


the array has the same number of array elements.
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Figure1 A conceptual representation of 2D array

Initializing 2-D array: Initialization of 2-D array is similar to a 1-D array.

Example:

Figure2 Initialization of 2D array

After this initialization, each element is as follows:

1 temp[0][0] : 1
2 temp[0][1] : 2
3 temp[0][2] : 3
4 temp[1][0] : 11
5 temp[1][1] : 22
6 temp[1][2] : 33
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Example1: The addition of two matrices.


#include<stdio.h>
#define ROW 2
#define COL 3

int main()
{
int mat1[ROW][COL], mat2[ROW][COL], mat3[ROW][COL];
int i, j;
printf("Enter first matrix: \n\n");
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
printf("Enter a[%d][%d]: ", i, j);
scanf("%d", &mat1[i][j]);
}
}

printf("\nEnter Second matrix: \n\n");


for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
printf("Enter a[%d][%d]: ", i, j);
scanf("%d", &mat2[i][j]);
}
}
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COL; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j] ;
}
}
printf("\nResultant array: \n\n");

for(i = 0; i < ROW; i++)


{
for(j = 0; j < COL; j++)
{
printf("%5d ", mat3[i][j]);
}
printf("\n");
}
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return 0;
}

How it works:
Two matrices can be added or subtracted, only if they have the same dimension. In other
words, a matrix of size 2*3 can be added to another matrix of 2*3, but you can’t add or
subtract it to a matrix of 2*4 or 3*2. The resultant array will be a matrix of the same
dimension as the original two. First two for loops asks the user to enter two matrices. The
third for loop adds corresponding elements of mat1 and mat2 in a new array mat3. Fourth for
loop prints the elements of array mat3.
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Example2 Sum of even and odd of Two Dimensional Array


#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
int even=0;
int odd=0;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(arr[i][j]%2==0)

{
even=even+arr[i][j];
}
else
{
odd=odd+arr[i][j];
}
}
}
printf("Sum of even =%d \n",even);
printf("Sum of odd =%d",odd);
return 0;
}
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Summary

• An array of arrays is known as 2D array. The two dimensional (2D) array in C


programming is also known as matrix. A matrix can be represented as a table of rows
and columns.

• To store the elements entered by user we are using two for loops, one of them is a
nested loop.

REFERENCES

Book References:

1. Thareja Reema (2014) Programming in C. 2nd ed.

2. Zed A. Shaw, Learn C the Hard Way’

3. [Link]

Video Lecture:

1. [Link]
2. [Link]
3. [Link]

Websites:

1. [Link]

[Link]://[Link]/tutorials/2darray/

[Link]://[Link]/c-programming/c-multi-dimensional-arrays
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Frequently Asked Questions:


Q1 Write a program in C to find the sum of left diagonals of a matrix
#include <stdio.h>

void main()

int i,j,arr1[50][50],sum=0,n,m=0;

printf("\n\nFind sum of left diagonals of a matrix :\n");

printf("---------------------------------------\n");

printf("Input the size of the square matrix : ");

scanf("%d", &n);

m=n;

printf("Input elements in the first matrix :\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

printf("The matrix is :\n");

for(i=0;i<n;i++)

for(j=0;j<n ;j++)

printf("% 4d",arr1[i][j]);

printf("\n");

}
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING


for(i=0;i<n;i++)

m=m-1;

for(j=0;j<n ;j++)

if (j==m)

sum= sum+arr1[i][j];

printf("Addition of the left Diagonal elements is :%d\n",sum);

Output:

Find sum of left diagonals of a matrix :


---------------------------------------
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
The matrix is :
1 2
3 4
Addition of the left Diagonal elements is :5
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Q2 Write a program in C to print or display the lower triangular of a given matrix


#include <stdio.h>

void main()

int arr1[10][10],i,j,n;

float determinant=0;

printf("\n\nDisplay the lower triangular of a given matrix :\n");

printf("----------------------------------------------------\n");

printf("Input the size of the square matrix : ");

scanf("%d", &n);

printf("Input elements in the first matrix :\n")

for(i=0;i<n;i++)

for(j=0;j<n;j++)

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

printf("The matrix is :\n");

for(i=0;i<n;i++)

for(j=0;j<n ;j++)

printf("% 4d",arr1[i][j]);

printf("\n");

}
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

printf("\nSetting zero in lower triangular matrix\n");

for(i=0;i<n;i++){

printf("\n");

for(j=0;j<n;j++)

if(i<=j)

printf("% 4d",arr1[i][j]);

else

printf("% 4d",0);

printf("\n\n");

Output:

Display the lower triangular of a given matrix :


----------------------------------------------------
Input the size of the square matrix : 3
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 6
element - [2],[0] : 7
element - [2],[1] : 8
element - [2],[2] : 9
The matrix is :
1 2 3
4 5 6
7 8 9
Setting zero in lower triangular matrix

1 2 3
0 5 6
0 0 9
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Q3 Write a program in C to calculate determinant of a 3 x 3 matrix.


#include <stdio.h>

void main()

int arr1[10][10],i,j,n;

int det=0;

printf("\n\nCalculate the determinant of a 3 x 3 matrix :\n");

printf("-------------------------------------------------\n");

printf("Input elements in the first matrix :\n");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

printf("The matrix is :\n");

for(i=0;i<3;i++)

for(j=0;j<3 ;j++)

printf("% 4d",arr1[i][j]);

printf("\n");

}
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

for(i=0;i<3;i++)

det = det + (arr1[0][i]*(arr1[1][(i+1)%3]*arr1[2][(i+2)%3] - arr1[1][(i+2)%3]*arr1[2][(i+1)%3]));

printf("\nThe Determinant of the matrix is: %d\n\n",det);

Output:

Calculate the determinant of a 3 x 3 matrix :


-------------------------------------------------
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 0
element - [0],[2] : -1
element - [1],[0] : 0
element - [1],[1] : 0
element - [1],[2] : 1
element - [2],[0] : -1
element - [2],[1] : -1
element - [2],[2] : 0
The matrix is :
1 0 -1
0 0 1
-1 -1 0

The Determinant of the matrix is: 1

Q4 Write a program in C to accept two matrices and check whether they are equal. .
#include <stdio.h>

#include <stdlib.h>

void main()

int arr1[50][50], brr1[50][50];

int i, j, r1, c1, r2, c2, flag =1;

printf("\n\nAccept two matrices and check whether they are equal :\n ");

printf("-----------------------------------------------------------\n");

printf("Input Rows and Columns of the 1st matrix :");


DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

scanf("%d %d", &r1, &c1);

printf("Input Rows and Columns of the 2nd matrix :");

scanf("%d %d", &r2,&c2);

printf("Input elements in the first matrix :\n");

for(i=0;i<r1;i++)

for(j=0;j<c1;j++)

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&arr1[i][j]);

printf("Input elements in the second matrix :\n");

for(i=0;i<r2;i++)

for(j=0;j<c2;j++)

printf("element - [%d],[%d] : ",i,j);

scanf("%d",&brr1[i][j]);

printf("The first matrix is :\n");

for(i=0;i<r1;i++)

for(j=0;j<c1 ;j++)

printf("% 4d",arr1[i][j]);

printf("\n");
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING


}

printf("The second matrix is :\n");

for(i=0;i<r2;i++)

for(j=0;j<c2 ;j++)

printf("% 4d",brr1[i][j]);

printf("\n");

if(r1 == r2 && c1 == c2)

printf("The Matrices can be compared : \n");

for(i=0; i<r1; i++)

for(j=0; j<c2; j++)

if(arr1[i][j] != brr1[i][j])

flag = 0;

break;

else

{ printf("The Matrices Cannot be compared :\n");

exit(1);

}
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if(flag == 1 )

printf("Two matrices are equal.\n\n");

else

printf("But,two matrices are not equal\n\n");

Output:

Accept two matrices and check whether they are equal :


-----------------------------------------------------------
Input Rows and Columns of the 1st matrix :2 2
Input Rows and Columns of the 2nd matrix :2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
The first matrix is :
1 2
3 4
The second matrix is :
1 2
3 4
The Matrices can be compared :
Two matrices are equal.
onecDEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
ASSESSMENT QUESTIONS:

[Link] of the following is true about arrays in C.

(A) For every type T, there can be an array of T.


(B) For every type T except void and function type, there can be an array of T.
(C) When an array is passed to a function, C compiler creates a copy of array.
(D) 2D arrays are stored in column major form

[Link] a program that declares a 2 x 3 array with the following values:

5 6 7
8 9 10

Print the array row-by-row, so your output looks like this:

row 0: 5 6 7
row 1: 8 9 10

Then print the array column-by-column, so your output looks like this:

col 0: 5 8
col 1: 6 9
col 2: 7 10

. 3. What will be the output of the following C code?

#include <stdio.h>
void main()
{
int a[2][3] = {1, 2, 3, 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

4. What will be the output of the following C code?

#include <stdio.h>
void f(int a[][3])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {0};
f(a);
}
a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
DEPARTMENT OF
onec

COMPUTER SCIENCE & ENGINEERING

Discussion Forum:

Q1 C Program to Multiply Two 3 X 3 Matrices

You might also like