# Thread: power operation on a matrix question..

1. ## power operation on a matrix question..

i got a square matrix
and i need to calculate the matrix when it set by some power
i tried this one
it didnt work
??
Code:
```for (tndex=0;power>=tndex;tndex++){
for (index=0;index<rows;index++){

for (kndex=0;kndex<cols;kndex++){

sum[index][kndex]=sum[index][kndex]+matrix[index][kndex]*matrix[kndex][index]
}

}
}
//end i multiplication```

2. Do you know how to do matrix multiplication?

3. yes
http://en.wikipedia.org/wiki/Matrix_multiplication

but how to keep the sum matrix?

4. Well, given that what you've posted bears no resemblance to the wiki link you gave, I would suggest reading the wiki link again. The (i,j) element of your answer (I guess that's what you want sum to be?) will be the sum of the products of the elements of the i'th row of the first matrix and the j'th column of the second matrix.

Note that these matrices will be identical to your original matrix only in the case of finding a square; if you need to find a cube, for instance, you would first find the square of your matrix, and then multiply your matrix by that squared matrix.

5. i know the algorithm

i tried again its not working

Code:
```        for (index=0;kndex<cols;index++){

for (kndex=0;kndex<cols;kndex++){

sum2=sum2+matrix[index][kndex]*matrix[kndex][index];
sum[index][kndex]=sum2;
}
printf("\n");
}```

6. i need to put a square matrix on some input power

i tried to copy the algoritm
Code:
`http://en.wikipedia.org/wiki/Matrix_multiplication`
Code:
```#include <stdio.h>

int main(){//start
int rows,cols;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("&#37;d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int transpose [cols][rows];
int index,kndex;

for (index=0;index<rows;index++){

for (kndex=0;kndex<cols;kndex++){
matrix[index][kndex]=0;
sum[index][kndex]=0;
transpose[index][kndex]=0;
}//end inner for
}//end outer for

//stat input

for (index=rows-1;index>=0;index--){

for (kndex=cols-1;kndex>=0;kndex--){

scanf("%d",&matrix[index][kndex]);

transpose [kndex][index]=matrix[index][kndex];
}

}

getchar();  //needed because of scanf()
//end input

//start power multiplication

for (index=0;kndex<power;index++){
for (index=0;kndex<cols;index++){

for (kndex=0;kndex<cols;kndex++){

sum2=sum2+matrix[index][kndex]*matrix[kndex][index];
sum[index][kndex]=sum2;
}

}
}
//end power multiplication

//start print

for (index=0;index<rows;index++){

for (kndex=0;kndex<cols;kndex++){

printf("%d ",sum[index][kndex]);
}
printf("\n");
}

//end print

printf("\n");

return 0;
}//end main func```

7. You probably should compute every element of the answer array, not just the diagonal elements [0][0], [1][1], etc. Note that it is not possible to do this without three nested for-loops (two to figure out which entry in the answer matrix we're trying to find, and one to do the sum) -- right now you're trying to make one for-loop go through the entire matrix, which is why you're only getting the diagonal entries.