You are on page 1of 5

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;

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

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

Equations->findAllSolutions(). return 0.} ary[solutionNumber][j] = temp[index].i<this->noOfSolutions. Equations->getMandN(). Equations->getMatrix(). } Output: 1)0 -1 2 -0.j++) cout<<this->solutions[i][j]<<' '. } solutionNumber = solutionNumber + 1.5 0 2.continue.5 3 5 0 ###################################################### Question 2 ###################################################### int main(){ Solutions* Equations = new Solutions(). Equations->printSolutions(). Equations->getMatrix(). } } }. return 0. // Equations->printMatrix(). Equations->reduceMatrix().File: /home/anurag1/Downloads/To_be_printed. int main(){ Solutions* Equations = new Solutions().i++){ for(int j=0.j<this->n-1.j<this->n-1. int a = Equations->findRank(). Equations->printSolutions(). for(int j=0.5 0 1. } void printSolutions(){ for(int i=0. } } } this->solutions = ary. Equations->reduceMatrix().continue. index = index + 1. // Equations->printMatrix(). } 1)0 0 -5 -6 0 -5 0 -6 0 0 15 0 5 0 0 -15 2 0 9 0 .5 -2 3 0 2)0 -1 3 0.} if(s==j) {ary[solutionNumber][j] == 0.j++){ if(i==j) {ary[solutionNumber][j] == 0. Equations->getMandN(). Equations->findAllSolutions(). Equations->printMax(). int a = Equations->findRank().txt Page 4 of 5 int index = 0. cout<<'\n'.

2.05263 2.36842 0 0 Maximum Value : 12.05263.36842) .txt Page 5 of 5 1.3684 at (1.File: /home/anurag1/Downloads/To_be_printed.