Arrays 2D Lecture 17

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

CSE101-Lec#17(Part-2)

• Multidimensional Arrays(2D Array)

©LPU CSE101 C Programming


2D-Array
• The basic form of declaring a two-dimensional array of size x,
y:
• Syntax:
• data_type array_name[x][y];
• data_type: Type of data to be stored. Valid C/C++ data type.
• We can declare a two dimensional integer array say ‘x’ of size
10,20 as:
• int x[10][20];
• Elements in two-dimensional arrays are commonly referred
by x[i][j] where i is the row number and ‘j’ is the column
number

©LPU CSE101 C Programming


2D-Array
• A two – dimensional array can be seen as a table with ‘x’ rows
and ‘y’ columns where the row number ranges from 0 to (x-1)
and column number ranges from 0 to (y-1). A two –
dimensional array ‘x’ with 3 rows and 3 columns is shown
below:

©LPU CSE101 C Programming


Also known as Multiple-Subscripted Arrays

• Multiple subscripted arrays


– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
int a[rows][column];

Column 0 Column 1 Column 2 Column 3


0 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
a[
Row 0
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]

Column subscript
Array name
Row subscript
4
©LPU CSE101 C Programming
Memory representation of 2D-Array
• A 2D array’s elements are stored in continuous memory locations. It can
be represented in memory using any of the following two ways:

1. Column-Major Order
2. Row-Major Order

1. Column-Major Order:
In this method the elements are stored column wise, i.e. m elements of
first column are stored in first m locations, m elements of second column
are stored in next m locations and so on. E.g.
A 3 x 4 array will stored as below:

©LPU CSE101 C Programming


Memory representation of 2D-Array
• 2. Row-Major Order:
In this method the elements are stored row wise, i.e. n
elements of first row are stored in first n locations, n elements
of second row are stored in next n locations and so on. E.g.
A 3 x 4 array will stored as below:

©LPU CSE101 C Programming


Initialization
1) Initializing at the point of declaration:
1 2
3 4
• int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces
• If not enough, unspecified elements set to zero
1 0
3 4
• Int a[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• int a3[2][2]={1,2};//Remaining elements are zero
• int a4[2][2]={0};//All elements are zero

©LPU CSE101 C Programming


Initialization
• int a5[][2]={1,2,3};//It is possible to skip row size, if
elements are initialized at the point of declaration
• int a[2][]={1,2,3};//Not possible to skip column
size[Error will come]
• int a[][]={1,2,3};//Not possible to skip both row and
column size[Error will come]

©LPU CSE101 C Programming


Initialization
2) Taking input from user
int a[3][3], i, j;
for(i=0; i<3; i++)
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf(“enter the value ofa[%d][%d]: ”, i, j);
scanf(“%d”, &a[i][j]);
} //end for columns

} //end for rows

©LPU CSE101 C Programming


#include<stdio.h>
void main() Program to
{
int a[3][3], i, j;
display 2D
for(i=0; i<3; i++) array
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf(“enter the value of a[%d][%d]: ”, i, j);
scanf(“%d”, &a[i][j]);
} //end for columns
} //end for rows
printf(“elements of 2D matrix are”);
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
print(“%d\t”, a[i][j]);
} //end for
printf(“\n”);
} //end for
} //end main

©LPU CSE101 C Programming


enter the value of a[0][1] :1
enter the value of a[0][1] :2
enter the value of a[0][1] :3
enter the value of a[0][1] :4
enter the value of a[0][1] :5
enter the value of a[0][1] :6
enter the value of a[0][1] :7
enter the value of a[0][1] :8
enter the value of a[0][1] :9
Element of 2D matrix are:
1 2 3
4 5 6
7 8 9
Matrix operations using 2D arrays
• WAP to find the sum of two matrices
• WAP to display the transpose of a matrix
• WAP to find the sum of diagonal elements of a
matrix
• WAP to perform multiplication of 2 matrices
and display the result

©LPU CSE101 C Programming


WAP to find the sum of two matrices
#include <stdio.h> for(i=0; i<3; i++)
int main()
{
{
float a[3][3], b[3][3], c[3][3]; for(j=0; j<3; j++)
int i, j; {
printf("Enter elements of 1st matrix\n"); c[i][j] = a[i][j] + b[i][j];
for(i=0; i<3; i++)
}
{
for(j=0; j<3 ;j++) }
{ // Displaying the sum
printf("Enter a%d%d: ", i, j); printf("\nSum Of Matrix:\n");
scanf("%f", &a[i][j]);
}
} for(i=0; i<3; i++)
// Taking input using nested for loop {
printf("Enter elements of 2nd matrix\n"); for(j=0; j<3; j++)
for(i=0; i<3; i++)
{
{
for(j=0; j<3; j++) printf("%.1f\t", c[i][j]);
{ }
printf("Enter b%d%d: ", i, j); printf("\n");
scanf("%f", &b[i][j]);
}
}
} return 0;
// adding corresponding elements of two arrays }
©LPU CSE101 C Programming
WAP to display the transpose of a matrix
#include <stdio.h> // Finding the transpose of matrix a
int main() for(i=0; i<r; i++)
{
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
for(j=0; j<c; j++)
scanf("%d %d", &r, &c); {
transpose[i][j] = a[j][i];
// Storing elements of the matrix }
printf("\nEnter elements of matrix:\n"); }
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
// Displaying the transpose of matrix a
{ printf("\nTranspose of Matrix:\n");
printf("Enter element a%d%d: ",i, j); for(i=0; i<r; i++)
scanf("%d", &a[i][j]); {
} for(j=0; j<c; j++)
}
{
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
printf("%d ",transpose[i][j]);
for(i=0; i<r; i++) }
{
for(j=0; j<c; j++) printf("\n\n");
{ }
printf("%d ", a[i][j]);
}
printf("\n\n");
return 0;
©LPU CSE101
} C Programming }
WAP to find the sum of diagonal elements of a matrix
#include<stdio.h> for(i=0;i<m;i++)
int main() {
{
for(j=0;j<n;j++)
int a[10][10],sum=0;
{
int i,j,m,n;
printf("Enter number of rows and if(i==j)
column:"); {
scanf("%d%d",&m,&n);
printf("Enter Elements : "); sum=sum+a[i][j];
for(i=0;i<m;i++) }
{
}
for(j=0;j<n;j++)
}
{
scanf("%d",&a[i][j]); printf("Sum of Diagonal
} Elements = %d ",sum);
}
}

©LPU CSE101 C Programming


WAP to perform multiplication of 2 matrices and display the result
#include <stdio.h>
int main()
{ // Initializing all elements of result matrix to 0
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; for(i=0; i<r1; i++)
printf("Enter rows and column for first matrix: "); {
scanf("%d %d", &r1, &c1); for(j=0; j<c2; j++)
{
printf("Enter rows and column for second matrix: "); result[i][j] = 0;
scanf("%d %d",&r2, &c2); }
// Column of first matrix should be equal to column of second matrix and }
while (c1 != r2) // Multiplying matrices a and b and
{ // storing result in result matrix
printf("Error! No. of columns of first matrix not equal to no.of row of
second.\n\n");
printf("Enter rows and column for first matrix: "); for(i=0; i<r1; i++)
scanf("%d %d", &r1, &c1); {
for(j=0; j<c2; j++)
printf("Enter rows and column for second matrix: ");
{
scanf("%d %d",&r2, &c2);
for(k=0; k<c1; k++)
}
{
// Storing elements of first matrix.
result[i][j]+=a[i][k]*b[k][j];
printf("\nEnter elements of matrix 1:\n");
}
for(i=0; i<r1; i++) }
{ }
for(j=0; j<c1; j++) // Displaying the result
{ printf("\nOutput Matrix:\n");
printf("Enter elements a%d%d: ",i,j); for(i=0; i<r1; i++)
scanf("%d", &a[i][j]); {
} for(j=0; j<c2; j++)
} {
// Storing elements of second matrix. printf("%d ", result[i][j]);
printf("\nEnter elements of matrix 2:\n"); }
for(i=0; i<r2; i++) printf("\n\n");
{ }
for(j=0; j<c2; j++) return 0;
• }
{
printf("Enter elements b%d%d: ",i, j);
©LPU CSE101 C Programming
scanf("%d",&b[i][j]);
Dry running

©LPU CSE101 C Programming


Q1
What will be the output of following code?
#include<stdio.h>
int main()
{
int a[][3]={1,2,3,4,5,6};
printf("%d",a[0][2]);
return 0;
}
A. 2
B. 3
C. 4
D. Compile time error

©LPU CSE101 C Programming


Q2
Which of the following is invalid initialization of 2D Array?
A. int a[][2]={1,2,3,4};
B. int a[2][2]={1,2,3,4};
C. int a[2][]={1,2,3,4};
D. int a[2][2]={};

©LPU CSE101 C Programming


Q3
What will be the output of following code?
#include<stdio.h>
int main()
{
int a[3][2]={{1,2},{3,4},{5,6}};
printf("%d",a[1][1]*a[2][1]);
return 0;
}
A. 24
B. 12
C. 8
D. 20

©LPU CSE101 C Programming

You might also like