2D Arrays
2D Arrays
Array
Collection of individual elements of the same data type,
all referenced by the array name plus its index, stored
in contiguous/continuous/ sequential/linear memory
locations.
Element
An individual element or entry in an array
Subscript or index
An integer number that identifies an element’s position
in the array
1
Contd..
Array declaration: array_name[size];
Ex: int a[3];
Contd…
If we want to assign a value to any of the
memory locations , for ex: a[1]=100, then
we can assign as follows:
Evocation
Two-Dimensional (2D) Arrays
General Objective
To understand and apply the concept of
two dimensional arrays in C
programming.
Specific Objectives
Students will be able to -
1. Classify the types of arrays
2. Differentiate one-dimensional and multi-
dimensional arrays.
3. Define two-dimensional arrays
4. Represent the syntax to initialize 2D arrays
5. Implement 2D arrays using C program
Types of Arrays
One-Dimensional vs Multi-
Dimensional Arrays
1-Dimensional array
Multi-Dimensional arrays
9
Two-Dimensional Array
2-Dimensional arrays are most common type of multi
dimensional array.
11
2D Array Declaration
Syntax :
datatype variable_name [row_size] [col_size]
Example: int num[2][3];
Here,
the name of the array : num
the type of the array elements: integer
the dimensions of the array: 2
the total number of elements or size of the array= 2*3
=6
Initialization of Two Dimensional
Array
int num[2][4] = {{10, 11, 12, 13},{14, 15, 16, 17}};
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
int abc[2][2] = {1, 2, 3 ,4 } /* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 } /* Valid declaration*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second
dimension*/
int abc[2][] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second
dimension*/
Accessing an Element in 2D Array
Assume that the two dimensional array called
val[3][4] looks like the following:
val Col 0 Col 1 Col 2 Col 3
Row 0 8 16 9 52
Row 1 3 15 27 6
Row 2 14 25 2 10
14
1
Input and Output of 2D Arrays
A nested for loop is used to get input/print output
elements of a two dimensional array.
By increasing/decreasing the index value of the
array the elements can be entered in a 2d array as
well as the elements stored at that index value are
printed on the output screen.
Formative Assessment 1
What is the output of this C code?
#include <stdio.h>
void main()
{
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]);
}
a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
Ans: a
What is the output of this C code?
#include <stdio.h>
void main()
{
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]);
}
a) 1 2 3 junk 4 5
b) Compile time error
c) 1 2 3 0 4 5
d) 1 2 3 3 4 5
Ans: b
A program to input elements in a two
dimensional array and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3];
int i,j;
clrscr();
printf(“enter the elements in the array:”);
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
printf(“%d”,a[i][j]);
} printf(“\n”);
}
getch();
}
OUTPUT :-
Enter elements in array:
1
2
3
4
5
6
7
8
9
123
456
789
A program to add two matrix entered
by the user and print it.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
printf(“enter the elements in both the array:”);
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
getch();
}
OUTPUT:-
enter the elements in both the array:
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 2 4 6
9 8 10 12
1 14 16 18
2
Program for Matrix
Multiplication
Dimension of Matrix 1 : 1 X 3
Dimension of Matrix 2 : 3 X 2
Multiplication is Possible if –
No. of Columns of Matrix 1 = No of Rows of Matrix 2
Dimension of Resultant Matrix:
c[No. of Rows of Mat1][No. of Columns of Mat2]
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
printf("\nEnter First Matrix : n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Multiplication Logic
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
{
for (k = 0; k <= 2; k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\nMultiplication Of Two Matrices : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
return (0);
}
DISCUSSION
Mindmap
Summary
Types of arrays
Two-dimensional arrays
Structure of 2D arrays
Accessing elements of 2D arrays
Initialization of 2D arrays
Implement 2D arrays using C program
Stimulating Question
How will you display the transpose of a
matrix?