Arrays in C-2D
Arrays in C-2D
Arrays in C
1. Through this lecture, students will understand
Learning the concept of arrays, types of arrays,
Outcomes application in real life
2. Students will learn how to declare arrays in C
Contents
• Types of array – 2D
• How to declare 2D arrays
Two dimensional Array (2-D array)
Syntax: data_type varname[size1][size2];
Initialization Method 1: int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
#include <stdio.h>
int main()
{ int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, i, j;
for(i = 0; i < 3; i++)
{ 123
for(j = 0; j < 3; j++) 456
{ 789
printf("%2d ", matrix[i] [j]);
} printf("\n");
}
return 0; }
Initialization of a 2-D Array
int a[2][3]={1,2,3,4,5,6};
int a[2][3]={{1,2,3}, {4,5,6}};
int a[][3]={{1,2,3}, {4,5,6}}
int a[2][3]={0}
int a[3][]={2,4,6,8,10,12};
int a[][]={2,4,6,8,10,12};
Note: If the first bracket pair is empty, then compiler take the size
from the number of inner brace pairs
Example
#include <stdio.h>
#define row 4
#define col 3
void main()
{
int M[row][col];
int i,j,k;
printf(“Enter data for Matrix M1\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&M1[i][j]);
}
printf(“\n”);
}
Memory Map for 2-D Arrays
Kept in memory as a linear sequence of variables.
Two methods for storing-
– Row major
– Column major
Example:
int a[3][3];
Row major storage:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2],
a[2][0], a[2][1], a[2][2]
Column major storage:
a[0][0], a[1][0], a[2][0], a[0][1], a[1][1], a[2][1],
a[0][2], a[1][2], a[2][2]
Program on Multidimensional Arrays
#include <stdio.h>
int main()
Output:
{ Temperature @ location 1
float temp[2][4] = {{12.31, 14.36, 13.4, 12.4}, {11.2, ---> 12.31 (1 time)
11.24, 14.2, 12.5}}; Temperature @ location 1
---> 14.36 (2 time)
int i, j; Temperature @ location 1
for(i = 0; i < 2; i++) ---> 13.4 (3 time)
{ Temperature @ location 1
---> 12.4 (4 time)
for(j = 0; j < 4; j++)
Temperature @ location 2
{ ---> 11.2 (1 time)
printf("\nTemperature @location%d ---> %f (%d time) Temperature @ location 2
---> 11.24 (2 time)
",i+1, temp[i][j] ,j+1);
Temperature @ location 2
} ---> 14.2 (3 time)
} Temperature @ location 2
return 0; ---> 12.2 (4 time
}
Transpose of a matrix
#include <stdio.h> printf("Transpose of matrix is \n");
void main() for (j = 0; j < n; ++j)
{ static int array[10][10]; {
int i, j, m, n;
for (i = 0; i < m; ++i)
printf("Enter the order of the matrix \n");
{
scanf("%d %d", &m, &n);
printf(" %d", array[i][j]);
printf("Enter the coefiicients of the matrix\n");
}
for (i = 0; i < m; ++i)
{ for (j = 0; j < n; ++j) printf("\n");
{ scanf("%d", &array[i][j]); }
}} }
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{ for (j = 0; j < n; ++j)
{ printf(" %d", array[i][j]); }
printf("\n"); }
Matrix Multiplication
#include <stdio.h> else
int main() { printf("Enter the elements of second matrix\n");
{ int m, n, p, q, c, d, k, sum = 0; for ( c = 0 ; c < p ; c++ )
int first[10][10], second[10][10], multiply[10][10]; for ( d = 0 ; d < q ; d++ )
printf("Enter the number of rows and columns of first scanf("%d", &second[c][d]);
matrix\n");
for ( c = 0 ; c < m ; c++ )
scanf("%d%d", &m, &n);
{ for ( d = 0 ; d < q ; d++ )
printf("Enter the elements of first matrix\n");
{ for ( k = 0 ; k < p ; k++ )
for ( c = 0 ; c < m ; c++ )
{ sum = sum + first[c][k]*second[k][d]; }
for ( d = 0 ; d < n ; d++ )
multiply[c][d] = sum;
scanf("%d", &first[c][d]);
sum = 0; } }
printf("Enter the number of rows and columns of
second matrix\n"); printf("Product of entered matrices:-\n");
2. For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler.
Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
t0 = i * 1024 t1= j * 32 t2 = k * 4 t3 =t1 + t0 t4 = t3 + t2 t5 = X[t4]Which one of the following
statement about the source code of C program is correct?
(A) X is declared as “int X[32][32][8]” (B) X is declared as “int X[4][1024][32]”
(C) X is declared as “char X[4][32][8]” (D) X is declared as “char X[32][16][2]”
3. Assume the following C variable declaration
int *A [10], B[10][10]; of the following expressions
I. A[2] II. A[2][3] III. B[1] IV. B[2][3], which will not give compile-time errors if
used as left hand sides of assignment statements in a C program.
(A) I, II, and IV only (B) II, III, and IV only
(C) II and IV only (D) IV only