OOP2
OOP2
#include<iostream.h>
#include<conio.h>
#include<process.h>
class Mat
{
protected:
int i,j,k,l,m,n,rows[3],cols[3],mat[3][3],sum_row,sum_col;
public:
Mat()
{
sum_row=sum_col=0;
}
void getRows();
void getColumns();
void getTValues();
void sumRC();
void checkRC();
};
void Mat::getRows()
{
cout<<"\nENTER THREE VALUES FOR ROW'S\n";
for(i=0;i<=2;i++)
cin>>rows[i];
}
void Mat::getColumns()
{
cout<<"\nENTER THREE VALUES FOR COLUMNS'S\n";
for(i=0;i<=2;i++)
cin>>cols[i];
}
void Mat::getTValues()
{
cout<<"\nENTER THE THREE VALUES:\n";
for(i=0;i<=2;i++)
cin>>mat[i][i];
}
void Mat::sumRC()
{
for(i=0;i<=2;i++)
{
sum_row+=rows[i];
sum_col+=cols[i];
}
}
void Mat::checkRC()
{
if(sum_row!=sum_col)
{
cout<<"\nINVALID INPUT:";
getch();
exit(0);
}
else
{
cout<<"\nPRESS,ANY KEY TO CONTINUE:\n";
getch();
}
}
class Compute:public Mat
{
private:
int f;
public:
int checkVal();
int checkMat();
void matProcess();
void showValues();
};
void Compute::matProcess()
{
int high;
high=checkVal();
high=high/3;
high+=5;
f=1;
for(i=0;i<=high;i++)
{
mat[0][1]=i;
for(j=0;j<=high;j++)
{
mat[0][2]=j;
for(k=0;k<=high;k++)
{
mat[1][0]=k;
for(l=0;l<=high;l++)
{
mat[1][2]=l;
for(m=0;m<=high;m++)
{
mat[2][0]=m;
for(n=0;n<=high;n++)
{
mat[2][1]=n;
f=checkMat();
if(f==0)
showValues();
}
}
}
}
}
}
cout<<"\nTHE PROBLEM HAS NO SOLUTION";
}
int Compute::checkMat()
{
int r,c,flag=0,rtot=0,ctot=0;
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
rtot+=mat[r][c];
}
if(rows[r]!=rtot)
flag=1;
rtot=0;
}
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
ctot+=mat[c][r];
}
if(cols[r]!=ctot)
flag=1;
ctot=0;
}
return (flag);
}
int Compute::checkVal()
{
int high=0;
for(i=0;i<=2;i++)
{
if(high<rows[i])
high=rows[i];
if(high<cols[i])
high=cols[i];
}
return(high);
}
void Compute::showValues()
{
cout<<"\n\nMATRIX VALUES ARE:\n\n";
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<"\t"<<mat[i][j];
}
cout<<"\t"<<rows[i]<<"\n";
}
cout<<"\n\n";
for(i=0;i<=2;i++)
cout<<"\t"<<cols[i];
getch();
exit(0);
}
void main()
{
Compute cm;
clrscr();
cm.getRows();
cm.getColumns();
cm.sumRC();
cm.getTValues();
cm.checkRC();
cm.matProcess();
getch();
}
OUTPUT:
ENTER THREE VALUES FOR ROW'S
40 30 50
ENTER THREE VALUES FOR COLUMNS'S
25 35 60
ENTER THE THREE VALUES:
10 5 20
PRESS,ANY KEY TO CONTINUE:
MATRIX VALUES ARE:
10
10
5
5
5
25
25
15
20
25
35
60
40
30
50