You are on page 1of 10

FDS ASSIGNMENT NO.

:03
Name: Aditya Adke
Roll no.: 231002
GR No.: 22010806
Batch:A1

Aim:
Perform matrix operations with and without pointers
Code:

#include <iostream>
using namespace std;
void insert(int *ar,int
x,int y)
{
int *p=&ar[0];
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
cout<<"Enter element of row "<<i+1<<" of
column "<<j+1<<endl; cin>>*p; p++;
}
}
p=ar;
cout<<endl;
}
void print(int *ar,int r,int c)
{
int *p=&ar[0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

cout<<*p;
cout<<" ";
p++;
}

cout<<endl;
}
p=ar;

}
void add(int *ar1,int*ar2,int r,int c)
{ int
arr[r][c];
int
*p1=&ar1[0];
int
*p2=&ar2[0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

arr[i][j]=*p1+*p2;
p1++;
p2++;
}
}
print(arr[0],r,c);
}
void multiply(int *ar1,int*ar2,int r,int c)
{ int
arr[r][c];
int arr1[r][c];
int mar[r][c];
int
*p1=&ar1[0];
int
*p2=&ar2[0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{

arr[i][j]=*p1;
arr1[i][j]=*p2;
p1++;
p2++;
}

cout<<endl;
}
for (int i = 0; i < r;
i++) { for (int j
= 0; j < c; j++) {
mar[i][j] = 0;

for (int k = 0; k < c; k++)


mar[i][j] = mar[i][j] + (arr[i][k] *
arr1[k][j]);
}
}
print(mar[0],r,c);
}
void trnspose(int *ar,int r,int c)
{ int
arr[r][c];
int trr[r][c];
int
*p1=&ar[0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
arr[i][j]=*p1;
p1++;
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
trr[i][j]=arr[j][i];
}
}
print(trr[0],r,c);

}
int main()
{
int r,c;
cout<<"Enter dimensions of
arrary"<<endl; cout<<"rows:";
cin>>r; cout<<endl;
cout<<"columns:"; cin>>c;
int (*ptr)[c]; int arr[r][c]; int
ar1[r][c]; ptr= arr; int a;

while(a!=4)
{
cout<<"Enter 1 to add two
matrix"<<endl; cout<<"Enter 2 to multiply
two matrix"<<endl; cout<<"Enter 3 to
transpose a matrix"<<endl; cout<<"Enter 4
to exit"<<endl;
cin>>a;
switch(a){
case(1):
{
cout<<"Enter elments for
matrix 1"<<endl;
insert(ptr[0],r,c); cout<<"Enter
elments for matrix 2"<<endl;
insert(ar1[0],r,c);
cout<<"Addition of two
matrix"<<endl;

add(arr[0],ar1[0],r,c);
break;
}
case(2):
{
cout<<"Enter elments for
matrix 1"<<endl;
insert(ptr[0],r,c); cout<<"Enter
elments for matrix 2"<<endl;
insert(ar1[0],r,c);
cout<<"multiplication of two matrix"<<endl;

multiply(arr[0],ar1[0],r,c);
break;
}
case(3):
{
cout<<"Enter elments for
matrix "<<endl;
insert(ptr[0],r,c);
cout<<"Transpose of a
matrix"<<endl;
trnspose(arr[0],r,c);
break;
}
case(4):
{
break;
}
default:
{
cout<<"invalid input"<<endl;
}

}
}
return 0;
}
Output:
Enter dimensions of

arrary rows:2

columns:2
Enter 1 to add two matrix
Enter 2 to multiply two matrix
Enter 3 to transpose a matrix
Enter 4 to exit
1
Enter elments for matrix 1
Enter element of row 1 of column 1
1
Enter element of row 1 of column 2
1
Enter element of row 2 of column 1
1
Enter element of row 2 of column 2
1

Enter elments for matrix 2


Enter element of row 1 of column 1
2
Enter element of row 1 of column 2
2
Enter element of row 2 of column 1
2
Enter element of row 2 of column 2
2

Addition of two matrix


33
33
Enter 1 to add two matrix
Enter 2 to multiply two matrix
Enter 3 to transpose a matrix
Enter 4 to exit
2
Enter elments for matrix 1
Enter element of row 1 of column 1
1
Enter element of row 1 of column 2
0
Enter element of row 2 of column 1
0
Enter element of row 2 of column 2
1

Enter elments for matrix 2


Enter element of row 1 of column 1
-98
Enter element of row 1 of column 2
67
Enter element of row 2 of column 1
31
Enter element of row 2 of column 2
1

multiplication of two matrix

-98 67
31Enter 1 to add two matrix 6
Enter 2 to multiply two matrix Enter 3
to transpose a matrix Enter 4 to exit 3 6
Enter elments for matrix 1 Enter
element of row 1 of column 7
1 Enter element of row 1 of column 7

0
Enter element of row 2 of column 1
0
Enter element of row 2 of column 2
1

Transpose of a matrix
10
01
Enter 1 to add two matrix
Enter 2 to multiply two matrix
Enter 3 to transpose a matrix
Enter 4 to exit
3
Enter elments for matrix 1
Enter element of row 1 of column 1
7
Enter element of row 1 of column 2
8
Enter element of row 2 of column 1
3
Enter element of row 2 of column 2
4

Transpose of a matrix
7 3
84

Enter 1 to add two matrix


Enter 2 to multiply two matrix
Enter 3 to transpose a matrix
Enter 4 to exit
4

You might also like