DSA - Unit1 - U-5
DSA - Unit1 - U-5
DSA - Unit1 - U-5
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.
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]);
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++) {
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.