0% found this document useful (0 votes)
39 views8 pages

Mat PTR

The document describes a C program that performs various matrix operations using pointers. It defines functions to add, subtract, multiply and transpose matrices. It also finds the saddle point of a matrix. The main() function gets the dimensions and elements of two matrices from the user and displays a menu to perform the different operations. It displays the results and allows the user to continue or exit.

Uploaded by

assassinator
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Mat PTR

The document describes a C program that performs various matrix operations using pointers. It defines functions to add, subtract, multiply and transpose matrices. It also finds the saddle point of a matrix. The main() function gets the dimensions and elements of two matrices from the user and displays a menu to perform the different operations. It displays the results and allows the user to continue or exit.

Uploaded by

assassinator
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

//Roll No.

2521
//MATRIX OPERAITIONS WITH POINTERS

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define lim 10

#define upl printf("%c",218); //to print "┌" ┌ ┐


#define upr printf("%c",191); //to print "┐" │ │
#define line printf("%c",179); //to print "│" == │ │
#define lowl printf("%c",192); //to print "└" │ │
#define lowr printf("%c",217); //to print "┘" └ ┘

//Function Delcarations
void add(int *a[lim], int *b[lim]);
void sub(int *a[lim], int *b[lim]);
void matmul(int *a[lim], int *b[lim]);
void display(int *op[lim],int,int,int flag,char);
void read(int *a[lim],char ch);
void transpose(int *a[lim],int,int,char ch);
void saddle(int *a[lim],int,int,char ch);

int ra,ca; //Global Variables - Dimensions of Matrix A


int rb,cb; //Global Variables - Dimensions of Matrix B

void main()
{

int *a[lim], *b[lim]; //Array of Pointers


int i,j;
char n,ch;
clrscr();
printf("Enter the order of matrix A : ");
scanf("%d%d",&ra,&ca);

for(i=0;i<ra;i++) //Dyamic Memory Allocation


a[i]=(int*)malloc(ca*sizeof(int)); //of Matirx A

printf("\n\nEnter the elements :\n");


for(i=0;i<ra;i++)
{
for(j=0;j<ca;j++)
scanf("%d",(a[i]+j));
}

printf("\n\nEnter the order of matrix B : ");


scanf("%d%d",&rb,&cb);

for(i=0;i<rb;i++) //Dyamic Memory Allocation


b[i]=(int*)malloc(cb*sizeof(int)); //of Matirx B

printf("\n\nEnter the elements :\n");


for(i=0;i<rb;i++)
{
for(j=0;j<cb;j++)
scanf("%d",(b[i]+j));
}
do
{ clrscr();
printf(" A\t B\n\n");
display(a,ra,ca,0,'-');
printf(" ");
display(b,rb,cb,1,'-');
flushall();
printf("\n\nMatrix Operations:\nPress\n1 => Addition\n2 => Subtraction\n3
=> Multiplication\n4 => Transpose of Matrix A\n5 => Transpose of Matrix B\n6 =>
Saddle Point of Matrix A\n7 => Saddle Point of Matrix B\n0 => Exit\nYour Choice :
");
n=getche();
switch(n)
{
case '1': add(a,b);break;
case '2': sub(a,b);break;
case '3': matmul(a,b);break;
case '4': transpose(a,ra,ca,'A');break;
case '5': transpose(b,rb,cb,'B');break;
case '6': saddle(a,ra,ca,'A');break;
case '7': saddle(b,rb,cb,'B');break;
case '0': exit(0);
default : printf("\n\nWrong choice!");
}
printf("\n\n\nWould you like to continue?(y/n)");
ch=getche();
}while(ch=='y');
}

//Function Definitions:-

void matmul(int *a[lim], int *b[lim])


{
int i,j,k,sum,*c[lim];

if(ca!=rb)
printf("\n\nMultiplication not possible...");
else
{
for(i=0;i<=ra;i++)
c[i]=(int*)malloc(cb*sizeof(int));

for(k=0;k<ra;k++)
{
for(i=0;i<cb;i++)
{
sum=0;
for(j=0;j<rb;j++)
sum += *(a[k]+j) * (*(b[j]+i));
*(c[k]+i)=sum;
}
}
printf("\n\nA x B => ");
display(a,ra,ca,2,'x');
display(b,rb,cb,2,'=');
display(c,ra,cb,1,'-');
}
}
void display(int *op[lim],int r,int c,int flag,char ch)
{
int i,j,x,y,x1,y1,temp,temp2;
x1=wherex();
y1=wherey();
upl
gotoxy(x1,y1+1);
for(i=0;i<(r+r-1);i++)
{
line
gotoxy(x1,y1+i+2);
}
lowl;
x1++;
y1++;
gotoxy(x1,y1);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%-5d",*(op[i]+j));
}
x=wherex();
gotoxy(x1,y1+(2*i+2));
}
gotoxy(x-3,y1-1);
upr
gotoxy(x-3,y1);
for(i=0;i<(r+r-1);i++)
{
line
gotoxy(x-3,y1+i+1);
}
lowr;
if(flag==1)
printf("\n");
else if(flag==0)
gotoxy(x,y1-1);
else
{
gotoxy(x,y1+(r/2)+1);
printf("%c",ch);
gotoxy(x+2,y1-1);
}
}
void add(int *a[lim],int *b[lim])
{
int i,j;
int *c[lim];

if((ra!=rb)||(ca!=cb))
printf("\n\nAddition Not possible...\n");
else
{

for(i=0;i<=ra;i++)
c[i]=(int*)malloc(ca*sizeof(int));

for(i=0;i<ra;i++)
{
for(j=0;j<ca;j++)
*(c[i]+j)=(*(a[i]+j))+(*(b[i]+j));
}
printf("\n\nA + B => ");
display(a,ra,ca,2,'+');
display(b,rb,cb,2,'=');
display(c,ra,ca,1,'-');
}
}

void sub(int *a[lim], int *b[lim])


{
int i,j,*c[lim];
if((ra!=rb)||(ca!=cb))
printf("\n\nSubtraction Not possible...\n");
else
{
for(i=0;i<=ra;i++)
c[i]=(int*)malloc(ca*sizeof(int));

for(i=0;i<ra;i++)
{
for(j=0;j<ca;j++)
*(c[i]+j)=(*(a[i]+j))-(*(b[i]+j));
}
printf("\n\nA - B => ");
display(a,ra,ca,2,'-');
display(b,rb,cb,2,'=');
display(c,ra,ca,1,'-');
}
}

void transpose(int *a[lim],int r,int c,char ch)


{
int i,j,temp,*t[lim];
for(i=0;i<=r;i++)
t[i]=(int*)malloc(c*sizeof(int));

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
*(t[j]+i)= (*(a[i]+j));
}
printf("\n\n%c' => ",ch);
display(t,c,r,1,'-');
}
void saddle(int *a[lim],int ra,int ca,char ch)
{
int i,j,k,mx[lim][3],mn[lim],temp,m,n,r,c;
//max in rows - min
for(i=0;i<ra;i++)
{
temp=*(a[i]+0);
for(j=1;j<ca;j++)
{
if(*(a[i]+j)>temp)
{
temp=*(a[i]+j);
mx[i][2]=j;
}
}
mx[i][1]=temp;
}
temp=mx[0][1];
r=0;
c=mx[0][2];
for(i=1;i<ra;i++)
{
if(temp>mx[i][1])
{
temp=mx[i][1];
r=i;
c=mx[i][2];
}
}
m=temp;

//min in col-max
for(i=0;i<ca;i++)
{
temp=*(a[0]+i);
for(j=1;j<ra;j++)
{
if(temp>(*(a[j]+i)))
temp=(*(a[j]+i));
}
mn[i]=temp;
}
temp=mn[0];
for(i=1;i<ca;i++)
{
if(temp<mn[i])
temp=mn[i];
}
n=temp;
if(m==n)
printf("\n\nSADDLE POINT of %c : %d\n\nPOSITION : (%d,
%d)\n",ch,m,r+1,c+1);
else
printf("\n\nNo Saddle Point.");
}
OUTPUT:

Enter the order of matrix A : 3 3


Enter the elements :
1 2 3

4 5 6

7 8 9

Enter the order of matrix B : 3 3


Enter the elements :
1 0 0

0 1 0

0 0 1

A B
┌ ┐ ┌ ┐
│1 2 3│ │1 0 0│
│ │ │ │
│4 5 6│ │0 1 0│
│ │ │ │
│7 8 9│ │0 0 1│
└ ┘ └ ┘

Matrix Operations:
Press
1 => Addition
2 => Subtraction
3 => Multiplication
4 => Transpose of Matrix A
5 => Transpose of Matrix B
6 => Saddle Point of Matrix A
7 => Saddle Point of Matrix B
0 => Exit
Your Choice : 1

A + B => ┌ ┐ ┌ ┐ ┌ ┐
│1 2 3│ │1 0 0│ │2 2 3 │
│ │ │ │ │ │
│4 5 6│ + │0 1 0│ = │4 6 6 │
│ │ │ │ │ │
│7 8 9│ │0 0 1│ │7 8 10│
└ ┘ └ ┘ └ ┘

Would you like to continue?(y/n)y


Matrix Operations:
Press
1 => Addition
2 => Subtraction
3 => Multiplication
4 => Transpose of Matrix A
5 => Transpose of Matrix B
6 => Saddle Point of Matrix A
7 => Saddle Point of Matrix B
0 => Exit
Your Choice : 2

A - B => ┌ ┐ ┌ ┐ ┌ ┐
│1 2 3│ │1 0 0│ │0 2 3│
│ │ │ │ │ │
│4 5 6│ - │0 1 0│ = │4 4 6│
│ │ │ │ │ │
│7 8 9│ │0 0 1│ │7 8 8│
└ ┘ └ ┘ └ ┘

Would you like to continue?(y/n)y

Matrix Operations:
Press
1 => Addition
2 => Subtraction
3 => Multiplication
4 => Transpose of Matrix A
5 => Transpose of Matrix B
6 => Saddle Point of Matrix A
7 => Saddle Point of Matrix B
0 => Exit
Your Choice : 3

A x B => ┌ ┐ ┌ ┐ ┌ ┐
│1 2 3│ │1 0 0│ │1 2 3│
│ │ │ │ │ │
│4 5 6│ x │0 1 0│ = │4 5 6│
│ │ │ │ │ │
│7 8 9│ │0 0 1│ │7 8 9│
└ ┘ └ ┘ └ ┘

Would you like to continue?(y/n)y


Matrix Operations:
Press
1 => Addition
2 => Subtraction
3 => Multiplication
4 => Transpose of Matrix A
5 => Transpose of Matrix B
6 => Saddle Point of Matrix A
7 => Saddle Point of Matrix B
0 => Exit
Your Choice : 4

A' => ┌ ┐
│1 4 7│
│ │
│2 5 8│
│ │
│3 6 9│
└ ┘

Would you like to continue?(y/n)y

Matrix Operations:
Press
1 => Addition
2 => Subtraction
3 => Multiplication
4 => Transpose of Matrix A
5 => Transpose of Matrix B
6 => Saddle Point of Matrix A
7 => Saddle Point of Matrix B
0 => Exit
Your Choice : 6

SADDLE POINT of A : 3

POSITION : (1,3)

Would you like to continue?(y/n)n

You might also like