int main()
{
int i, j,k,q;
double G[M][M],P[M],L[M][M],Q[M][M];
G[0][0] = 3;
G[0][1] = 6;
G[0][2] = -8;
G[1][0] = 0;
G[1][1] = 0;
G[1][2] = 6;
G[2][0] = 0;
G[2][1] = 0;
G[2][2] = 2;
cout <<"Matrix A:" <<endl;
for (i=0;i<=M-1;i++)
{
for (j=0;j<=M-1;j++)
{
cout << setw(7) << G[i][j] << " ";
}
cout << endl;
}
cout << "The eigenvalue are:" << endl;
for (i = 0; i <= M-1 ; i++)
{
for (j = 0; j <= M-1 ; j++)
{
if (i == j)
{
P[i] = 0;
cout << "P[" << i << "] = " << G[i][j] << endl;
}
}
cout << endl;
}
//applied identity matrix code
cout << "Eigenvalue in matrix form:" << endl;
for (i = 0; i <= M - 1; i++)
{
for (j = 0; j <= M - 1; j++)
{
if (i == j)
{
L[i][j] = P[i];
L[i][j] = 0;
cout <<L[i][j]<< " ";
Whatever you put in the first line will be immediately updated by the second ... and hence will be zero ... which is what you get.
Beyond this, I don't understand from your explanation what you are trying to do. If these are the eigenvalues of your matrix and you want to put them on the diagonal of the matrix (usually denoted D) then first create a zero matrix, then update the [i][i] diagonal component, looping on i only. (If, alternatively, you wanted the transformation matrix, then the eigenvectors are the columns.)