I have been able to make code that takes the Kronecker product of two matrices and store it within a new matrix. However, if the first matrix being multiplied has more rows or columns than the second matrix, the program gets stuck or returns incorrect values. I wanted to know if anyone can help me figure out how to fix this. Also, my coding is a lot of nested loops. I'm not very good at programming and feel that there is a less confusing/shorter code to accomplish the same thing. If there are any suggestions, please let me know.
Code:
#include <stdio.h>
/*Computes the Kronecker Product of two 2x2 matrices. If matrix A has more rows or columns than matrix B, the program does not finish. However, if matrix B has more rows or columns than A, the code works properly.*/
int main()
{
int cola, rowa, colb, rowb;
cola = 2;
rowa = 2;
colb = 2;
rowb = 2;
int A[2][2] = {{1, 2}, {3, 4}},
B[2][2] = {{0, 5},{6,7}};
int C[rowa*rowb][cola*colb];
int i=0, j=0, k=0,l=0;
while(i<rowa-1)
{
do{
while(l<rowb-1)
{
do{
C[i+l][j+k] = A[i][j]*B[l][k];
printf("%d\t",C[i+l][j+k]);
k++;
}while (k<colb);
k=0; j++;
do{
C[i+l][j+k+1] = A[i][j]*B[l][k];
printf("%d\t",C[i+l][j+k+1]);
k++;
}while (k<colb);
k=0; j=0; l++;
printf("\n");
do{
C[i+l][j+k] = A[i][j]*B[l][k];
printf("%d\t",C[i+l][j+k]);
k++;
}while (k<colb);
k=0; j++;
do{
C[i+l][j+k+1] = A[i][j]*B[l][k];
printf("%d\t",C[i+l][j+k+1]);
k++;
}while (k<colb);
k=0; j=0; i++; l=0;
printf("\n");
do{
C[i+l+1][j+k] = A[i][j]*B[l][k];
printf("%d\t",C[i+l+1][j+k]);
k++;
}while (k<colb);
k=0; j++;
do{
C[i+l+1][j+k+1] = A[i][j]*B[l][k];
printf("%d\t",C[i+l+1][j+k+1]);
k++;
}while (k<colb);
k=0; j=0; l++;
printf("\n");
do{
C[i+l+1][j+k] = A[i][j]*B[l][k];
printf("%d\t",C[i+l+1][j+k]);
k++;
}while (k<colb);
k=0; j++;
do{
C[i+l+1][j+k+1] = A[i][j]*B[l][k];
printf("%d\t",C[i+l+1][j+k+1]);
k++;
}while (k<colb);
k=0;
printf("\n");
}l=0; }while(j<cola-1);
}
/*printf("\n\n%d",C[0][3]);*/
return 0;
}
Thanks.