Then an essay is used to solve the n-ary linear equations by using Kramofa.
Code:
1#include <iostream>2#include <cmath>3 using namespacestd;4 voidAppdescribe ();//Program Description5 DoubleValdet (Double* Detptr,intrank);//Solving determinant6 classlinearequations7 {8 Public:9Linearequations ();//Tectonic equation setTen voidSol ();//solving One voidShowans ();//Display Solution A Private: - intRank//Order Number - Double* PARPTR;//determinant of coefficients the Double* CONPTR;//Constant Entry - Double* ANSPTR;//Solution - }; - linearequations::linearequations () + { -cout<<"Input equation element number:"; +Cin>>rank; A intrank2=rank*rank; atParptr=New Double[RANK2]; -Conptr=New Double[rank]; -cout<<endl<<"determinant of input coefficients:"<<Endl; - for(intI=0; i<rank2;i++) -Cin>>Parptr[i]; -cout<<endl<<"Enter a constant entry:"<<Endl; in for(intI=0; i<rank;i++) -Cin>>Conptr[i]; to } + voidLinearequations::sol () - { the intrank2=rank*rank; * Doubledet=Valdet (Parptr,rank); $Ansptr=New Double[rank];Panax Notoginseng Double*tempparptr=New Double[RANK2]; - DoubleTempdet; the if(det==0) + { AAnsptr=NULL; the return; + } - for(intI=0; i<rank;i++) $ { $ for(intj=0; j<rank2;j++) -tempparptr[j]=Parptr[j]; - for(intj=0; j<rank;j++) thetempparptr[i+j*rank]=Conptr[j]; -tempdet=Valdet (Tempparptr,rank);Wuyiansptr[i]=tempdet/det; the } - } Wu voidLinearequations::showans () - { About if(ansptr==NULL) $ { -cout<<"the systems of linear equations have no solution or countless solutions ."<<Endl; - return; - } Acout<<endl<<"The solution of this linear equation group is:"<<Endl; + for(intI=0; i<rank;i++) thecout<<'x'<<i+1<<':'<<ansPtr[i]<<Endl; - } $ intMain () the { the appdescribe (); the linearequations L1; the L1.sol (); - L1.showans (); inSystem"Pause"); the return 0; the } About voidAppdescribe () the { thecout<<"***************************************"<<Endl the<<"The program uses KRAMOFA to solve the n-ary linear equations. "<<Endl +<<"Case:"<<Endl -<<"input equation element: 4"<<endl<<Endl the<<"determinant of input coefficients:"<<EndlBayi<<"1 1 1 1"<<Endl the<<"1 2-1 4"<<Endl the<<"2-3 -1-5"<<Endl -<<"3 1 2 each"<<endl<<Endl -<<"Enter a constant entry:"<<Endl the<<"5-2-2 0"<<endl<<Endl the<<"The solution of this linear equation group is:"<<Endl the<<"x1:1"<<Endl the<<"X2:2"<<Endl -<<"X3:3"<<Endl the<<"x4:-1"<<Endl the<<"***************************************"<<Endl; the } 94 DoubleValdet (Double* Detptr,intrank) the { the DoubleVal=0; the if(rank==1)returndetptr[0];98 for(intI=0; i<rank;i++)//calculation cofactor saved in nextdetptr[] About { - Double*nextdetptr=New Double[(rank-1) * (rank-1)];101 for(intj=0; j<rank-1; j + +)102 for(intk=0; k<i;k++)103nextdetptr[j* (rank-1) +k]=detptr[(j+1) *rank+K];104 for(intj=0; j<rank-1; j + +) the for(intk=i;k<rank-1; k++)106nextdetptr[j* (rank-1) +k]=detptr[(j+1) *rank+k+1];107Val+=detptr[i]*valdet (nextdetptr,rank-1) *pow (-1.0, i);108 }109 returnVal; the }111 /* the Data 1:113 2 the the 1 2 the 2 1117 118 3 3119 Data 2: - 4121 122 1 1 1 1123 1 2-1 4124 2-3 -1-5 the 3 1 2 each126 127 5-2-2 0 - */
Solving n-element systems of linear equations with Kramofa