0% found this document useful (0 votes)
26 views

OOP2

The document describes a C++ program that finds values for a 3x3 matrix given the row and column sums. It defines classes to represent the matrix and operations on it like getting input, checking sums, and finding values. The main function gets inputs, checks sums, and calls methods to systematically assign values to the matrix until it finds integers that satisfy the row and column sums or determines no solution exists.

Uploaded by

19830325
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

OOP2

The document describes a C++ program that finds values for a 3x3 matrix given the row and column sums. It defines classes to represent the matrix and operations on it like getting input, checking sums, and finding values. The main function gets inputs, checks sums, and calls methods to systematically assign values to the matrix until it finds integers that satisfy the row and column sums or determines no solution exists.

Uploaded by

19830325
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

FIND MATRIX VALUES

#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

You might also like