0% found this document useful (0 votes)
32 views17 pages

2D Arrays

Uploaded by

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

2D Arrays

Uploaded by

245123735015
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2D Arrays

One Dimensional Array Representation


Applications of Arrays in C
• Arrays are used to Store List of values
• Arrays are used to Perform Matrix Operations
• Arrays are used to implement Search Algorithms
Linear Search
Binary Search
• Arrays are used to implement Sorting Algorithms
• We use single dimensional arrays to implement sorting algorihtms like
Insertion Sort, Bubble Sort,Selection Sort,Quick Sort,Merge Sort, etc.,
Two Dimensional Array(2D Array)
• An array, which has two subscript is known as two dimensional array.
• It has two subscripts, first one represents row and second one
represents column.
• Subscripts are always starts with 0.
2D array representation
• The 2D array is organized as matrices which can be represented as the
collection of rows and columns.
Syntax
• data_type array_name [rows][columns];
• For example here I am declaring a integer array
• int a[3][4];
Syntax - Two Dimensional Array
data_type varname[size1][size2];

Initialization Methods - Two Dimensional Array


Initialisation-1
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Initialisation-2
int matrix[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Program - Two Dimensional Array
#include <stdio.h>
main()
{
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
printf("%2d ", matrix[i] [j]);
}
printf("\n");
}
}
Storing elements in a matrix and printing it.

#include <stdio.h>
main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Multi Dimensional Array
• What Is Multi Dimensional Array?
• An array having more than one subscript or dimension is known
as multi dimensional array i.e) int arr[3][3].
• Multi dimensional arrays are also known as arrays of arrays or matrix.
Syntax - Multi Dimensional Array
•Two Dimensional array requires two subscript variables

•In the following syntax size1 represents no of rows whereas size2 represents no of columns.
Syntax

data-type varname [size1][size2];


Matrix Addition Example
Program for Addition of Two Matrices
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
Matrix Multiplication in C
A matrix is a collection of numbers organized in rows and columns,
represented by a two-dimensional array in C.
Matrices can either be square or rectangular.

Input: mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{5, 6}, {7, 8}}
Multiplication of two matrices:
{{1*5 + 2*7 1*6 + 2*8}, {3*5 + 4*7 3*6 + 4*8}}
Output: {{19, 22}, {43, 50}}
#include<stdio.h> printf("multiply of the matrix=\n");
#include<stdlib.h> for(i=0;i<r;i++)
int main(){ {
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; for(j=0;j<c;j++)
system("cls"); {
printf("enter the number of row="); mul[i][j]=0;
scanf("%d",&r); for(k=0;k<c;k++)
printf("enter the number of column="); {
scanf("%d",&c); mul[i][j]+=a[i][k]*b[k][j];
printf("enter the first matrix element=\n"); }
for(i=0;i<r;i++) }
{ }
for(j=0;j<c;j++) //for printing result
{ for(i=0;i<r;i++)
scanf("%d",&a[i][j]); {
} for(j=0;j<c;j++)
} {
printf("enter the second matrix element=\n"); printf("%d\t",mul[i][j]);
for(i=0;i<r;i++) }
{ printf("\n");
for(j=0;j<c;j++) }
{ return 0;
scanf("%d",&b[i][j]); }
}
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element= 1 1 1 2 2 2 3 3 3
enter the second matrix element= 1 1 1 2 2 2 3 3 3
multiply of the matrix= 6 6 6 12 12 12 18 18 18

You might also like