Losing data from a dynamic 2D array that is being passed in a loop

Hi there!!

I'm writing this code for QR decomposition (code bellow). My problem is that I calculate a matrix AM and which is the minor of my matrix QA. This AM matrix is to be passed to the next iteration of the loop and become my NewA matrix, but for some reason the data isn't going through



for(int k=0; k<N-1; k++)
{
cout << "n=" << n << "\n";
// assigning dynamic arrays
float **NewA = DynMem(n);
float **I = eye(N);


if(k == 0)
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++)
NewA[i][j] = A[i][j];
else
{
// Problem assigning AM to NewA
// Check if we need to delete the memeory in NewA
cout << "NewA2(old AM):" <<"\n";
for(int i = 0; i<n; i++)
{
for(int j = 0; j<n; j++)
{
NewA[i][j] = AM[i][j];
cout << NewA[i][j] << "\t";
}
cout << "\n";
}
}

// QA redelcared here because, QA gets smaller for each iteration
delete [] QA, AM;
float **QA = DynMem(n);
float **AM = DynMem(n-1);

/////////////////////////////////////////////////////////////////
// Finds alpha and the x vector
for(int i = 0; i<n; i++)
{
x[i] = NewA[i][0];
a_norm +=x[i];
}
a_norm = (-1)*a_norm;

// u vector and norm of u
for(int i = 0; i<n; i++)
{
if(e[i]==0)
u[i] = x[i];
else
u[i] =x[i] + (e[i]*a_norm);
u_norm += (u[i]*u[i]);
}
u_norm = sqrt(u_norm);

// v and initialising I
for(int i = 0; i<n; i++)
v[i] = u[i]/u_norm;

////////////////////////////////////////////////////////////////////
// Q
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++)
Q[i][j] = I[i][j] - (2*v[i]*v[j]);

// Q * A
QA = M_Mult(Q, NewA, n);

// QA minor: AM => i.e. QA: 3x3 and AM: 2x2
if(n>2)
{
for(int i = 0; i<n-1; i++)
for(int j = 0; j<n-1; j++)
AM[i][j]= QA[i+1][j+1]; // Gets passed up to become the NewA

** This AM matrix here is what I want to pass to the next iteration, but my new a matrix becomese:
-4.31602e+008 -4.31602e+008
-4.31602e+008 -4.31602e+008
it should be something like:
-49 -14
168 -77 **
//////////////////////////////////////////////////////////////////////
// Dont need data in NewA now
/*for(int i = 0; i<n; i++)
delete[] NewA[i];*/



// LEAVE IT ALONE
for(int i = 0; i< (n -1); i++)
for(int j = 0; j< (n-1); j++)
Q_Temp[i+C][j+C]= QA[i+C][j+C];

//////////////////////////////////////////////////////////////////
// Calculation and store all the different Qs
Q_Tran = Tran(Q, N);
Q_Temp_Tran = Tran(Q_Temp, N);

for(int i = 0; i<N; i++)
{
for(int j = 0; j<N; j++)
{
if(n==N)
Q3D[i][j][k]= Q_Tran[i][j];
else
Q3D[i][j][k]= Q_Temp_Tran[i][j];
}
}



Any help would be cool :0
Cheers,
Clio
Topic archived. No new replies allowed.