2D - Arrays

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

2D Arrays

Column 0 Column 1 Column 2

Row 0 1 2 3
Row 1 4 5 6
Row 2 7 8 9

 Two dimensional array are used to represent


above matrix or table form of data
 It is an array of one dimensional array
 Represented by a single variable name using
two subscripts(rows, columns)
2D ARRAY DECLARATION
 SYNTAX:
data_type variable_name [row _ize] [column_size];
Example:
 To represent a data of (3 x 3) matrix using 2D array
int matrix[3][3];
Column 0 Column 1 Column 2
Row 0 [0][0] [0][1] [0][2]
Row 1 [1][0] [1][1] [1][2]
Row 2 [2][0] [2][1] [2][2]

 Array index / subscript always starts from


[0][0] to [rsize-1][colsize-1]
2D ARRAY DECLARATION &
INITIALIZATION The initialization is done
row by row.
 SYNTAX:
data_type variable_name [row size] [column
size] = {list of values};
Example:
int matrix[2][3] = {0, 0, 0, 1, 1, 1};
or
int matrix [2][3] = {{0, 0, 0}, {1, 1, 1}};
or
int matrix [2][3] = {
{0, 0, 0},
{1, 1, 1}
};
2D ARRAY DECLARATION
& INITIALIZATION
int matrix[ ][3] = {
{1, 1},
{2, 2, 2}
};
 first dimension size(here row size) is optional, when the array is
completely initialized with all values
 If the values are missing in an initializer, they are automatically
set to zero.
 The above declaration represents the following matrix
110
222
MULTIDIMENSION ARRAYS
 C allows arrays of three or more dimensions. The exact
limit is determined by the compiler.
 SYNTAX:
data_type variable_name [s1] [s2] [s3] …..
[sn];
where si is the size of ith dimension
 Examples
int sales[2][5][12];
Sales is a three-dimensional array declared to contain 120
(2 x 5 x 12) integer elements
Month 0 1 2 3 4 5 6 7 8 9 10 11
City

0
Year 0

1
2
3
4
Month 0 1 2 3 4 5 6 7 8 9 10 11
City

0
Year 0

1
2
3
4

int sales[2][5][12];//first index denotes year, the second city and


the third month
The array sales may represent a sales data of product during the
last two years from January to December in five cities.
Write the program to count the
number of negative elements in a 2D
matrix.
Input : [ 8 9 -2
4 -1 -3 ]
Output : 3
 /*to count the number of negative elements in a 2D matrix.*/
#include<stdio.h>
int main()
{
int row, col, i, j, count = 0;
int a[10][10];
scanf("%d %d", &row, &col);
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; j++)
scanf("%d", &a[i][j]);
}
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if(a[i][j] < 0)
count = count + 1;
}
}
printf("No. of Negative Elements = %d", count);
return 0;
}
Write the program to find the sum of
the diagonals of the square matrix
Input : 1 2 3 4
5 6 7 8
9 10 11 12
10 7 6 8
Output : 1st diagonal – 26
2nd diagonal – 31
#include<stdio.h>
 int main() {
int i,j,n,d1=0,d2=0,a[5][5];
printf("Enter size of square matrix:");
scanf("%d",&n);
printf("Enter Elements of matrix:\n");
for(i=0;i<n;++i)
for(j=0;j<n;++j) {
scanf("%d",&a[i][j]);
if(i==j)
d1+=a[i][j];
if((i+j)==(n-1))
d2+=a[i][j];
}
printf("\nFirst Diagonal Sum=%d",d1);
printf("\nSecond Diagonal Sum=%d",d2);
return 0;
}
Write the program which computes
and prints the sum of each rows of a
2D Matrix.
Input : [ 8 9 -2
4 -1 -3 ]
Output : 0th row – 15
1st row – 0
 /*to which computes and prints the sum of each rows of a 2D Matrix.*/
#include <stdio.h>
int main()
{
int matrix[10][10];
int i, j, r, c;
int sum[10];
scanf("%d %d", &r,&c);
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for(i = 0; i < r; i++) {
sum[i] = 0;
for(j = 0; j < c; j++)
{
sum[i] = sum[i] + matrix[i][j];
}
}
for(i = 0; i < r; i++)
printf(“%d row : %d\n", i,sum[i]);
return 0;
}
Write the program which computes and prints the sum of each
column of a 2D Matrix.

Write a program to find the sum of even cells(sum of row index and
column index will be even) in an matrix

Santha Teacher wants to find the female topper and male topper in her
class. She holds a 2D matrix with 2 columns,column 1 represents
male or female(male-0,female-1).Column 2 represents the marks.
Help here out to find the male and female topper.
Answer : A
Answer : A
Answer : D
Answer : D
Answer : B
Answer : E

You might also like