Hello, dear all. My algorithm as follows. my input is in Code2D.h file. actually i have to put in file 'Code2D.in'. but i dont know have to create it. By the way, my algorithm have no problem with N <= 4. but when i change N = 5, 6 , or 7, so on. the output is trouble. is it something is not right?

#include <fstream>
#include <iostream>
#define N 5
using namespace std;
void main()
{
    int i,j,k;
	double A[N+1][N+1];

	cout.setf(ios::fixed);
	cout.precision(5);
    cout << "Input matrix A: " << endl;
    ifstream InFile("Code2D.h");
   	for (i=1;i<=N;i++)
   	{
       	for (j=1;j<=N;j++)
		{
           	InFile >> A[i][j];
			cout << A[i][j] << " ";
		}
		cout << endl;
   	}
	InFile.close();

	// row operations
   	double Product,m;
   	for (k=1;k<=N-1;k++)
       	for (i=k+1;i<=N;i++)
		{
			m=A[i][k]/A[k][k];
			for (j=1;j<=N;j++)
				A[i][j]-=m*A[k][j];
		}

	cout << endl << "matrix U:" << endl;
   	for (i=1;i<=N;i++)
   	{
       	for (j=1;j<=N;j++)
			cout << A[i][j] << " ";
		cout << endl;
   	}

	Product=1;
	for (i=1;i<=N;i++)
		Product *= A[i][i]; // determinant

	// display results
	cout << endl << "det(A)=" << Product << endl;
}

>>for (i=1;i<=N;i++)
That's a problem. Arrays indices always, always begin with 0, never with 1. So what you want there is this: for(i = 0; i < N; i++) The same with the other loops.

>>my algorithm have no problem with N <= 4.
Only because you were lucky, not because your code is right.

It seems this function had its Fortran prototype, so indicies started from 1 to N (and right corrected dimension bound N+1): typical ad hoc porting solution.
Better give us a reference to this alg method and source (present data file contents too).
I have some doubts about the algorithm. It looks like some kind of LU decomposition but it's not LU decomposition...

thanks. its work.
i got the source from the book title: Computing numerical methods using visual c++. (2008)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.