You are on page 1of 5

File: /home/anurag1/Downloads/To_be_printed.

txt Page 1 of 5

###################################################### Question 1
######################################################

#include<iostream>
#include<bits/stdc++.h>
using namespace std;

class Solutions{
public:
int m,n;
float** matrix;
float* prevsolution;
float* currsolution;
float iteration;
int rank;
float** solutions;
int noOfSolutions;
Solutions(){}
void reduceMatrix(){
int k = 0;
float a;
for(int i=0;i<this->m-1;i++){
for(int j=i+1;j<this->m;j++){d
if(this->matrix[i][k]==0) continue;
a = float(this->matrix[j][k])/this->matrix[i][k];
for(int r=0;r<this->n;r++){
this->matrix[j][r] = this->matrix[j][r] - a*this->matrix[i][r];
}
}
k = k + 1;
}
}
void getMandN(){
int m,n;
cout<<"Enter m:";
cin>>m;
cout<<"Enter n:";
cin>>n;
this->prevsolution = new float[n-1];
this->currsolution = new float[n-1];
this->m = m;
this->n = n;
this->iteration=1000;
}
void getMatrix(){
float** ary = new float*[this->m];
for(int i = 0; i < this->m; ++i)
ary[i] = new float[this->n];
for(int i = 0; i < this->m; ++i){
cout<<"Enter"<<i<<endl;
for(int j = 0; j < this->n; ++j){
cin>>ary[i][j];
}
}
this->matrix = ary;
}
void printMatrix(){
for(int i = 0; i < this->m; ++i){
for(int j = 0; j < this->n; ++j){
cout<<this->matrix[i][j]<<' ';
}
cout<<endl;
}
}
int findRank(){
int rank = this->m,a,nosolution;
File: /home/anurag1/Downloads/To_be_printed.txt Page 2 of 5

for(int j=0;j<this->m;j++){
a = 0;
for(int i=0;i<this->n;i++){
if(this->matrix[j][i] != 0) a = 1;
}
if(this->matrix[j][this->n-1] != 0 && a==0) nosolution = 1;
if(a==0) rank = rank -1;
}
this->rank = rank;
return rank;
}
int nCr(int a,int b){
if(b==1) return a;
else return (a*(a-1))/2;
}
void findAllSolutions(){
int Unknowns = this->n - 1;
int rank = this->rank;
int UnknownsZero = Unknowns - rank;
int noOfSolutions = nCr(Unknowns,rank);
this->noOfSolutions = noOfSolutions;
float** ary = new float*[noOfSolutions];
for(int i = 0; i < noOfSolutions; ++i)
ary[i] = new float[this->n-1];
int solutionNumber = 0;
if(UnknownsZero == 1){
for(int i=0;i<this->n-1;i++){
float** copyMatrix = new float*[this->m];
for(int j=0;j<this->m;j++){
copyMatrix[j] = new float[rank+1];
int a = 0;
for(int k=0;k<this->n;k++){
if(i==k) continue;
copyMatrix[j][a] = this->matrix[j][k];
a = a + 1;
}
}
if(copyMatrix[1][0]==0){
float* temp = new float[rank];
temp[1] = copyMatrix[1][2]/copyMatrix[1][1];
temp[0] = (copyMatrix[0][2] - copyMatrix[0][1]*temp[1])/copyMatrix[0][0];
int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j) {ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
continue;
}
if(copyMatrix[1][1]==0){
float* temp = new float[rank];
temp[0] = copyMatrix[1][2]/copyMatrix[1][0];
temp[1] = (copyMatrix[0][2] - copyMatrix[0][0]*temp[0])/copyMatrix[0][1];
int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j){ ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
continue;
}
int factor;
factor = copyMatrix[0][0]/copyMatrix[1][0];
for(int k=0;k<this->rank+1;k++){
File: /home/anurag1/Downloads/To_be_printed.txt Page 3 of 5

copyMatrix[1][k] = copyMatrix[1][k]*factor - copyMatrix[0][k];


}
float* temp = new float[rank];
temp[1] = copyMatrix[1][2]/copyMatrix[1][1];
temp[0] = (copyMatrix[0][2] - copyMatrix[0][1]*temp[1])/copyMatrix[0][0];
int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j) {ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
}
}
if(UnknownsZero == 2){
for(int i=0;i<this->n-2;i++){
for(int s=i+1;s<this->n-1;s++){
float** copyMatrix = new float*[this->m];
for(int j=0;j<this->m;j++){
copyMatrix[j] = new float[rank+1];
int a = 0;
for(int k=0;k<this->n;k++){
if(i==k) continue;
if(s==k) continue;
copyMatrix[j][a] = this->matrix[j][k];
a = a + 1;
}
}
if(copyMatrix[1][0]==0){
float* temp = new float[rank];
temp[1] = copyMatrix[1][2]/copyMatrix[1][1];
temp[0] = (copyMatrix[0][2] - copyMatrix[0][1]*temp[1])/copyMatrix[0]
[0];
int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j) {ary[solutionNumber][j] == 0;continue;}
if(s==j) {ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
continue;
}
if(copyMatrix[1][1]==0){
float* temp = new float[rank];
temp[0] = copyMatrix[1][2]/copyMatrix[1][0];
temp[1] = (copyMatrix[0][2] - copyMatrix[0][0]*temp[0])/copyMatrix[0]
[1];
int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j){ ary[solutionNumber][j] == 0;continue;}
if(s==j) {ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
continue;
}
int factor;
factor = copyMatrix[0][0]/copyMatrix[1][0];
for(int k=0;k<this->rank+1;k++){
copyMatrix[1][k] = copyMatrix[1][k]*factor - copyMatrix[0][k];
}
float* temp = new float[rank];
temp[1] = copyMatrix[1][2]/copyMatrix[1][1];
temp[0] = (copyMatrix[0][2] - copyMatrix[0][1]*temp[1])/copyMatrix[0][0];
File: /home/anurag1/Downloads/To_be_printed.txt Page 4 of 5

int index = 0;
for(int j=0;j<this->n-1;j++){
if(i==j) {ary[solutionNumber][j] == 0;continue;}
if(s==j) {ary[solutionNumber][j] == 0;continue;}
ary[solutionNumber][j] = temp[index];
index = index + 1;
}
solutionNumber = solutionNumber + 1;
}
}
}
this->solutions = ary;
}
void printSolutions(){
for(int i=0;i<this->noOfSolutions;i++){
for(int j=0;j<this->n-1;j++)
cout<<this->solutions[i][j]<<' ';
cout<<'\n';
}
}
};

int main(){
Solutions* Equations = new Solutions();
Equations->getMandN();
Equations->getMatrix();
Equations->reduceMatrix();
// Equations->printMatrix();
int a = Equations->findRank();
Equations->findAllSolutions();
Equations->printSolutions();
return 0;
}

Output:

1)0 -1 2
-0.5 0 1.5
-2 3 0

2)0 -1 3
0.5 0 2.5
3 5 0

###################################################### Question 2
######################################################

int main(){
Solutions* Equations = new Solutions();
Equations->getMandN();
Equations->getMatrix();
Equations->reduceMatrix();
// Equations->printMatrix();
int a = Equations->findRank();
Equations->findAllSolutions();
Equations->printSolutions();
Equations->printMax();
return 0;
}

1)0 0 -5 -6
0 -5 0 -6
0 0 15 0
5 0 0 -15
2 0 9 0
File: /home/anurag1/Downloads/To_be_printed.txt Page 5 of 5

1.05263 2.36842 0 0
Maximum Value : 12.3684 at (1.05263,2.36842)

You might also like