0% found this document useful (0 votes)
2 views

CSE101-Lec15-(Part-B)

The document provides an overview of two-dimensional arrays (2D arrays) in programming, explaining their structure, declaration, initialization, and memory representation. It includes examples of how to declare and access 2D arrays, as well as practical applications such as matrix operations. Additionally, it presents practice questions and code snippets for tasks like matrix addition, transposition, and multiplication.

Uploaded by

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

CSE101-Lec15-(Part-B)

The document provides an overview of two-dimensional arrays (2D arrays) in programming, explaining their structure, declaration, initialization, and memory representation. It includes examples of how to declare and access 2D arrays, as well as practical applications such as matrix operations. Additionally, it presents practice questions and code snippets for tasks like matrix addition, transposition, and multiplication.

Uploaded by

allexhshibiakd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CSE101-Lec#15(Part-B)

• Multidimensional Arrays(2D Array)

CSE101-Computer 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:

• Examples:
Representing the marks of 60 students of class in 5 subjects, we
can take a 2D array of row size:60 and column size:5
Representing the sales done by all(50) sales persons of a
particular branch for all months(12), we can take a 2D array of row
size:50 and column size:12CSE101-Computer 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
CSE101-Computer 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
CSE101-Computer Programming
Memory representation of 2D-
• AArray
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:

CSE101-Computer 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:

CSE101-Computer 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
3
0
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

CSE101-Computer 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]

CSE101-Computer 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

CSE101-Computer Programming
Accessing(o
r Traversing)
2D array
elements
after taking
input from
the user

CSE101-Computer Programming
enter the value of a[0][0] :1
enter the value of a[0][1] :2
enter the value of a[0][2] :3
enter the value of a[1][0] :4
enter the value of a[1][1] :5
enter the value of a[1][2] :6
enter the value of a[2][0] :7
enter the value of a[2][1] :8
enter the value of a[2][2] :9
Element of 2D matrix are:
1 2 3
4 5 6
7 8 9
Polling Questions
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
CSE101-Computer Programming
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]={};

CSE101-Computer Programming
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
CSE101-Computer Programming
What will be the output of following A. 12345
code?
B. 123450
#include <stdio.h>
int main() C. 1 2 3 4 5 Garbage value
{ D. Compile time error
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]);
return 0;
}
CSE101-Computer Programming
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

CSE101-Computer Programming
WAP to find the sum of two matrices
#include <stdio.h>
int main() for(i=0; i<3; i++)
{ {
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("\nSum Of Matrix:\n");
printf("Enter a%d%d: ", i, j);
scanf("%f", &a[i][j]); for(i=0; i<3; i++)
} {
} for(j=0; j<3; j++)
// Taking input using nested for loop {
printf("Enter elements of 2nd matrix\n"); printf("%.1f\t", c[i][j]);
for(i=0; i<3; i++) }
{ printf("\n");
for(j=0; j<3; j++) }
{ return 0;
printf("Enter b%d%d: ", i, j); }
scanf("%f", &b[i][j]);
}
}
// adding corresponding elements of two
CSE101-Computer Programming
arrays
CSE101-Computer Programming
CSE101-Computer 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; for(j=0; j<c; j++)
printf("Enter rows and columns of matrix: "); {
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++)
{ // Displaying the transpose of matrix a
for(j=0; j<c; j++) printf("\nTranspose of Matrix:\n");
{ for(i=0; i<r; i++)
printf("Enter element a%d%d: ",i, j); {
scanf("%d", &a[i][j]); for(j=0; j<c; j++)
} {
} printf("%d ",transpose[i][j]);
// Displaying the matrix a[][] */ }
printf("\nEntered Matrix: \n");
for(i=0; i<r; i++) printf("\n\n");
{ }
for(j=0; j<c; j++)
{
printf("%d ", a[i][j]); return 0;
} }
printf("\n\n"); CSE101-Computer Programming
}
CSE101-Computer 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);
}
}
CSE101-Computer Programming
CSE101-Computer Programming
CSE101-Computer Programming
WAP to perform multiplication of 2 matrices and display the
result
#include <stdio.h>
// Initializing all elements of result matrix to 0
int main()
for(i=0; i<r1; i++)
{
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; for(j=0; j<c2; j++)
printf("Enter rows and column for first matrix: "); {
scanf("%d %d", &r1, &c1); result[i][j] = 0;
}
printf("Enter rows and column for second matrix: "); }
scanf("%d %d",&r2, &c2); // Multiplying matrices a and b and
// Column of first matrix should be equal to column of // storing result in result matrix
second matrix and
while (c1 != r2)
{ for(i=0; i<r1; i++)
printf("Error! No. of columns of first matrix not equal {
to no.of row of second.\n\n"); for(j=0; j<c2; j++)
printf("Enter rows and column for first matrix: "); {
scanf("%d %d", &r1, &c1); for(k=0; k<c1; k++)
printf("Enter rows and column for second matrix: "); {
scanf("%d %d",&r2, &c2); result[i][j]+=a[i][k]*b[k][j];
} }
// Storing elements of first matrix. }
printf("\nEnter elements of matrix 1:\n"); }
for(i=0; i<r1; i++) // Displaying the result
{ printf("\nOutput Matrix:\n");
for(j=0; j<c1; j++) for(i=0; i<r1; i++)
{ {
printf("Enter elements a%d%d: ",i,j); for(j=0; j<c2; j++)
scanf("%d", &a[i][j]); {
} printf("%d ", result[i][j]);
} }
printf("\n\n");
// Storing elements of second matrix.
}
printf("\nEnter elements of matrix 2:\n");
return 0;
for(i=0; i<r2; i++) }
{
for(j=0; j<c2; j++)
{ CSE101-Computer Programming
printf("Enter elements b%d%d: ",i, j);
Dry
running

CSE101-Computer Programming
Practice Questions
WAP to display the maximum sales done(monthly) from a particular
branch in a year[Take input for number of sales persons and sales
done in each month for each sales person]
 WAP to display the minimum marks scored in each subject from a
particular section[ Take input for marks of n no. of students in m no.
of subjects from user]
WAP to display average marks scored by n no. of students of a section
in their registered m no. of courses.[Inputs will be given by user]

CSE101-Computer Programming

You might also like