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

2D Arrays

Here are the steps to display the transpose of a matrix: 1. Define a 2D array to store the original matrix 2. Define another 2D array to store the transpose 3. Use a nested for loop to traverse the original matrix - The outer loop index represents rows - The inner loop index represents columns 4. Store the element of the original matrix at [row][col] in the transpose matrix at [col][row] 5. Print the transpose matrix after the nested loops. This swaps the row and column indices to display the transpose.

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

2D Arrays

Here are the steps to display the transpose of a matrix: 1. Define a 2D array to store the original matrix 2. Define another 2D array to store the transpose 3. Use a nested for loop to traverse the original matrix - The outer loop index represents rows - The inner loop index represents columns 4. Store the element of the original matrix at [row][col] in the transpose matrix at [col][row] 5. Print the transpose matrix after the nested loops. This swaps the row and column indices to display the transpose.

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Recap…

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

has only one subscript/index

Multi-Dimensional arrays

have more than one subscript/index.

9
Two-Dimensional Array
2-Dimensional arrays are most common type of multi
dimensional array.

A two dimensional array in C language is represented


in the form 2D matrix having rows and columns.

In a 2D array the first subscript denotes the row index


and the second subscript denotes column index.

 For example, if you create an array like array [3] [4],


which means it has 3 rows and 4 columns
Structure of 2-Dimensional Array

2nd dimension (col)

1st dimension (row)

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

To access the cell containing 6, we reference val[1]


[3], that is, row 1, column 3.

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?

You might also like