Добавил:
Kolobok
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include<iostream.h>
#include<conio.h>
void main(void)
{
int rows, lines = 0;
int i,j,d=0;
float sum=0;
start:
//Vvod parametrov massiva: stolbcy i stroky
cout<<"Input lines of matrix\n";
cin>>lines;
cout<<"Input rows of matrix\n";
cin>>rows;
if(rows<lines)
{
cout<<"\nRows was input non correct\n";
goto start;
}
//Sozdanie dinamicheskogo massiva v kotoriy budut zapisivatsy elementy matrix
float **matrix = new float*[lines];
for(i=0; i<rows; i++)
*(matrix+i)=new float[rows];
float *roots = new float [rows-1];
cout<<"Input element of matrix\n";
for(i=0; i<lines; i++)
for(j=0; j<rows; j++)
cin>>matrix[i][j];
// cout<<"\nPress any key...\n";
// getch();
//Poisk elementov = 0 na glavnoy diagonaly
for(i=0; i<lines; i++)
if(!matrix[i][i])
{
for(j=i+1; j<lines; j++)
if(matrix[j][i])
{
float *f=matrix[i];
matrix[i]=matrix[j];
matrix[j]=f;
}
}
// cout<<"\nPress any key...\n";
// getch();
//Vivod resultatov posle udaleniy nuley is glavnoy diagonaly
for(i=0; i<lines; i++)
{
cout<<endl;
for(j=0; j<rows; j++)
cout<<matrix[i][j]<<ends;
}
// cout<<"\nPress any key...\n"<<endl;
// getch();
//Privedenie k stupenchatomu vidu
for (j=0; j<rows-1; j++)
{
for (i=0; i<lines; i++)
{
if ( (i>j) && (matrix[i] [j] !=0) )
for (d=rows-1; d>=0; d--)
matrix[i][d]-=matrix[j][d]*matrix[i][j];
if (i==j) for (d=rows-1; d>=0; d--)
matrix[i][d]/=matrix[i][j];
}
}
// cout<<"\nPress any key...\n";
// getch();
cout<<endl<<endl<<endl;
//Vivod resultatov posle privedeniy
for(i=0; i<lines; i++)
{
cout<<endl;
for(j=0; j<rows; j++)
cout<<matrix[i][j]<<ends;
}
// cout<<"\nPress any key...\n";
// getch();
// Poisk korney
cout << endl << endl << "Roots:";
for (i=rows-2; i>=0; i--) {
for (j=rows-2; j>i; j--)
sum+=roots[j]*matrix[i][j];
roots[i]=matrix[i][rows-1]-sum;
sum=0;
}
// Vivod korney
for(i=0; i<rows-1; i++)
cout << endl << "X" << i + 1 << " = " << roots [i];
cout<<"\n\n\ndelete matrix"<<endl;
//Deleting matrix
for(i=0; i<rows; i++)
delete matrix[i];
cout<<"\n\nPress any key..."<<endl;
getch();
}