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

2D Arrays

The document provides an overview of 2D arrays in programming, including their declaration, initialization, and usage in C. It also covers multidimensional arrays and includes example programs for counting negative elements, summing diagonals, and calculating row sums in a 2D matrix. Additionally, it discusses a problem involving calculating the volume of boxes that can pass through a tunnel based on their dimensions.

Uploaded by

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

2D Arrays

The document provides an overview of 2D arrays in programming, including their declaration, initialization, and usage in C. It also covers multidimensional arrays and includes example programs for counting negative elements, summing diagonals, and calculating row sums in a 2D matrix. Additionally, it discusses a problem involving calculating the volume of boxes that can pass through a tunnel based on their dimensions.

Uploaded by

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

Week 9

2D Arrays

2D ARRAY DECLARATION

􀀀 SYNTAX:

data_type variable_name [row _size] [column_size];

Example:

􀀀 To represent a data of (3 x 3) matrix using 2D array

int matrix[3][3];

􀀀 Array index / subscript always starts from

[0][0] to [rsize-1][colsize-1]

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]

INITIALIZATION

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}

};

The initialization is done

row by row
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

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)

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

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

#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

#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;

}
MCQ

Ans: A

Ans: D
Ans: A

Ans: D
Ans: A

Ans: D
Ans: B

Ans: E

You are transporting some boxes through a tunnel, where each box is a parallelepiped, and is
characterized by its length, width and height. The height of the tunnel 41 feet and the width can be
assumed to be infinite. A box can be carried through the tunnel only if its height is strictly less than
the tunnel’s height. Find the volume of each box that can be successfully transported to the other
end of the tunnel. Note: Boxes cannot be rotated.

Input
4

5 5 5 //length, width and height

1 2 40

10 5 41

7 2 42

Output

125

80

Explanation 0

The I box is only 5 feet tall, so it can pass through the tunnel and its volume is 5 x 5 x 5 = 125.

The second box is sufficiently low, its volume is 1 x 2 x 4= = 80.

The third box is exactly 41 feet tall, so it cannot pass. The same can be said about the fourth box.

#include<stdio.h>

int main()

int n;

scanf("%d",&n);

int arr[n][3];

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

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

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

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

if(arr[i][2]<41)

printf("%d ",arr[i][0]*arr[i][1]*arr[i][2]);

return 0;

You might also like