DSA - Unit1 - U-5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

2 Dimensional Arrays

• Example: Matrix Operations


• Addition
• Subtraction
• Scaling
• Determinant
• Transpose
• Multiplication

41
2 D Arrays – Example Program
Matrix Addition
#include <stdio.h> for(i=0;i<2;i++)
#include <conio.h> for(j=0;j<3;j++)
void main() c[i][j]=a[i][j]+b[i][j];
{ printf("\nTHE VALUES OF MATRIX C ARE:\n");
int a[2][3],b[2][3],c[2][3],i,j; for(i=0;i<2;i++)
clrscr(); {
printf("\nENTER VALUES FOR MATRIX A:\n"); for(j=0;j<3;j++)
for(i=0;i<2;i++) printf("%5d",c[i][j]);
for(j=0;j<3;j++) printf("\n");
scanf("%d",&a[i][j]); }
printf("\nENTER VALUES FOR MATRIX B:\n"); getch();
for(i=0;i<2;i++) }
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);

42
2 D Arrays – Example Program
Transpose of a matrix in C
• To obtain Transpose of a matrix, we interchange rows and columns of
the matrix.

• For example, consider the following 3 X 2 matrix:


12
34
56
Transpose of the matrix:
135
246
43
2 D Arrays – Example Program
Transpose of a matrix in C
#include <stdio.h> printf("Transpose of the matrix:\n");
int main()
{ for (c = 0; c < n; c++) {
int m, n, c, d, matrix[10][10], transpose[10][10]; for (d = 0; d < m; d++)
printf("Enter the number of rows and columns of a printf("%d\t", transpose[c][d]);
matrix\n"); printf("\n");
scanf("%d%d", &m, &n); }
return 0;
}
printf("Enter elements of the matrix\n");
for (c = 0; c < m; c++){
for (d = 0; d < n; d++){
scanf("%d", &matrix[c][d]);}}

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


for (d = 0; d < n; d++){
transpose[d][c] = matrix[c][d];}}

44
2 D Arrays – Example Program
Determinant of a matrix in C
#include<stdio.h> determinant = a[0][0] * ((a[1][1]*a[2][2]) -
(a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
int main(){
* a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] -
int a[3][3], i, j; a[2][0] * a[1][1]);
long determinant; printf("\nDeterminant of 3X3 matrix: %ld",
determinant);
printf("Enter the 9 elements of matrix: );
return 0;
for(i = 0 ;i < 3;i++)
}
for(j = 0;j < 3;j++)
scanf("%d", &a[i][j]);

printf("\nThe matrix is\n");


for(i = 0;i < 3; i++){
printf("\n");
for(j = 0;j < 3; j++)
printf("%d\t", a[i][j]);
}

45
2 D Arrays – Example Program
Scalar Matrix Multiplication
#include<stdio.h>
for(row = 0; rows< i; row++)
int main()
for(col = 0; col < j; col++)
{
Multi[row][col] = Number *
int i, j, row, col, Multi[10][10], Number; Multi[row][col];
printf("\n Please Enter Number of rows and columns\n"); printf("\n The Result of a Scalar Matrix Multiplication
scanf("%d %d", &i, &j); is : \n");
printf("\n Please Enter the Matrix Elements \n"); for(row = 0; row < i; row++)
for(row = 0; row < i; row++) for(col = 0; col < j; col++)
for(col = 0;col < j; col++) printf("%d \t ", Multi[row][col]);
scanf("%d", Multi[rows][columns]); printf("\n");
printf("\n Please Enter the Multiplication value: "); return 0; }
scanf("%d", &Number);

46
2 D Arrays – Example Program
Matrix Multiplication

47
2 D Arrays – Example Program
Matrix Multiplication
#include<stdio.h>
printf("multiply of the matrix=\n");
#include<stdlib.h>
for(i=0;i<r;i++) {
int main(){
for(j=0;j<c;j++) {
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
mul[i][j]=0;
printf("enter the number of row=");
scanf("%d",&r); for(k=0;k<c;k++) {

printf("enter the number of column="); mul[i][j]+=a[i][k]*b[k][j];


scanf("%d",&c); }} }
printf("enter the first matrix element=\n"); //for printing result
for(i=0;i<r;i++) { for(i=0;i<r;i++) {
for(j=0;j<c;j++) { for(j=0;j<c;j++) {
scanf("%d",&a[i][j]); }} printf("%d\t",mul[i][j]);
printf("enter the second matrix element=\n"); }
for(i=0;i<r;i++) {
for(j=0;j<c;j++) { printf("\n");
scanf("%d",&b[i][j]); } } }
return 0;

48
POINTER
Definition and Features of Pointer

➢Definition:
• Pointer is a variable that stores the address of another variable.
➢Features
• Pointer saves the memory space.
• Execution time of pointer is faster because of direct access to the memory
location.
• With the help of pointers, the memory is accessed efficiently, i.e., memory
is allocated and deallocated dynamically.
• Pointers are used with data structures.

You might also like