# Thread: how to change this simple opeation into power operation..

1. ## how to change this simple operation into power operation..

I got this simple operation of 2 equal matrix multiplication:
How to change it so if i enter a number
it will put the matrix into the power of the input number.

How do i transform some how this simple multiplication operation into a power operation.

Ii have tried to use temporary matrix to save the local multiplication.
and multiply by it on the next step
but its not working
??

Code:
```#include <stdio.h>

int main(){//star
int rows,cols,jndex,input;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int temp[rows][cols];
int transpose [cols][rows];
int index,kndex,tndex,gndex,lndex;
printf("enter power:");
scanf("%d",&input);

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

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

printf("enter numbers in a row for a matrix:");                                                 //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 operation

//start temp=matrix
for (gndex=0;gndex<rows;gndex++){

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

temp[index][kndex]=matrix[index][kndex];

}//end inner for
}//end outer for
//end temp=matrix
for (tndex=0;tndex<input;tndex++){

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

sum[index][jndex]=0;
for(kndex=0;kndex<cols;kndex++) {
sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][jndex];
}
}
}

//start temp=sum
for (gndex=0;gndex<rows;gndex++){

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

temp[index][kndex]=sum[index][kndex];

}//end inner for
}//end outer for
//end temp=sum
}

//end power operation

//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```

2. Look at this loop again:
Code:
```                                   //start temp=matrix
for (gndex=0;gndex<rows;gndex++){

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

temp[index][kndex]=matrix[index][kndex];

}//end inner for
}//end outer for```
Notice how what's inside doesn't match the loop variables.

3. How many times now have you been asked to fix your indentation? I can't keep track...
Are you ignoring it on purpose?

4. thanks

5. Originally Posted by Elysia
How many times now have you been asked to fix your indentation? I can't keep track...
Are you ignoring it on purpose?

i did ordered the code as much as i could
look at the structure of the loops
there is a clear pyramid on every operation
for which you could easily see whats inside what

the same way i ordered the cols

how to order it??

6. this code is not working
for the input of

0 1 2
3 0 7
1 1 1

and power 3

i need to get
15 14 33
43 20 97
16 14 32

75 48 161
157 140 323
74 48 162

why..

7. Originally Posted by transgalactic2
i did ordered the code as much as i could
look at the structure of the loops
there is a clear pyramid on every operation
for which you could easily see whats inside what

the same way i ordered the cols

how to order it??
Except everything at the same level needs to stay at the same level -- no printfs going back to the left margin, or scanfs getting one extra level of indentation. This is your original code (without the fix), but you should have something like this:
Code:
```#include <stdio.h>

int main(){//star
int rows,cols,jndex,input;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int temp[rows][cols];
int transpose [cols][rows];
int index,kndex,tndex,gndex,lndex;
printf("enter power:");
scanf("%d",&input);

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

printf("enter numbers in a row for a matrix:");  //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 operation

//start temp=matrix
for (gndex=0;gndex<rows;gndex++){
for (lndex=0;lndex<cols;lndex++){
temp[index][kndex]=matrix[index][kndex];
}//end inner for
}//end outer for
//end temp=matrix

for (tndex=0;tndex<input;tndex++){
for(index=0;index<rows;index++) {
for(jndex=0;jndex< cols;jndex++) {
sum[index][jndex]=0;
for(kndex=0;kndex<cols;kndex++) {
sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][index];
}
}
}
//start temp=sum
for (gndex=0;gndex<rows;gndex++){
for (lndex=0;lndex<cols;lndex++){
temp[index][kndex]=sum[index][kndex];
}//end inner for
}//end outer for
//end temp=sum
}
//end power operation

//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```
Note also that you don't double-space your code, nor do you need eight blank lines between sections of code.

8. How about code that looks like this?

Code:
```#include <stdio.h>

int main(){//star
int rows,cols,jndex,input;
int sum2=0;
printf("enter rows and cols [1..50] ==> ");
scanf("%d %d",&rows,&cols);
int matrix[rows][cols];
int sum[rows][cols];
int temp[rows][cols];
int transpose [cols][rows];
int index,kndex,tndex,gndex,lndex;

printf("enter power:");
scanf("%d",&input);

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

printf("enter numbers in a row for a matrix:"); //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 operation

//start temp=matrix
for (gndex = 0; gndex < rows; gndex++)
{
for (lndex = 0; lndex < cols; lndex++)
temp[index][kndex] = matrix[index][kndex];
}//end outer for

//end temp=matrix
for (tndex = 0; tndex < input; tndex++)
{
for (index = 0; index < rows; index++)
{
for (jndex = 0; jndex < cols; jndex++)
{
sum[index][jndex] = 0;
for (kndex = 0; kndex < cols; kndex++)
sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][jndex];
}
}

//start temp=sum
for (gndex = 0; gndex < rows; gndex++)
{
for (lndex = 0; lndex < cols; lndex++)
temp[index][kndex] = sum[index][kndex];
}//end outer for
//end temp=sum
}

//end power operation

//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```

9. Your t loop goes one too far -- the first multiplication gives you matrix^2, but your program believes it is computing matrix^1.

10. thanks it works