vendredi 27 mars 2015

Matrix multiplication - C++


Vote count:

1





void power(int a[][21], int n, int d)
{
int e[21][21], k, i, j, l;

if(d==1)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}

}
else
{

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
e[i][j]=0;
}
}

for(l=1;l<d;l++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=n;k++)
{
e[i][j]=e[i][j]+(a[i][k]*a[k][j]);
}
}
}
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<e[i][j]<<" ";
}
cout<<endl;
}
}

}


I am trying to build a function that calculates the power of a matrix. The program works for the 1st and 2nd power of the matrix but if I want to calculate the 3rd power the function will return incorrect values. I think the problem is at retaining the previous results but I can't figure out on how to fix it.



asked 3 mins ago

Vlad

23






Matrix multiplication - C++

Aucun commentaire:

Enregistrer un commentaire