Task: Kronecker product

C: #include<stdlib.h>
#include<stdio.h>

int main(){
	
	char input[100],output[100];
	int i,j,k,l,rowA,colA,rowB,colB,rowC,colC,startRow,startCol;
	double **matrixA,**matrixB,**matrixC;
	
	printf("Enter full path of input file : ");
	fscanf(stdin,"%s",input);
	
	printf("Enter full path of output file : ");
	fscanf(stdin,"%s",output);
	
	FILE* inputFile = fopen(input,"r");
	
	fscanf(inputFile,"%d%d",&rowA,&colA);
	
	matrixA = (double**)malloc(rowA * sizeof(double*));
	
	for(i=0;i<rowA;i++){
		matrixA[i] = (double*)malloc(colA*sizeof(double));
		for(j=0;j<colA;j++){
			fscanf(inputFile,"%lf",&matrixA[i][j]);
		}
	}
	
	fscanf(inputFile,"%d%d",&rowB,&colB);
	
	matrixB = (double**)malloc(rowB * sizeof(double*));
	
	for(i=0;i<rowB;i++){
		matrixB[i] = (double*)malloc(colB*sizeof(double));
		for(j=0;j<colB;j++){
			fscanf(inputFile,"%lf",&matrixB[i][j]);
		}
	}
	
	fclose(inputFile);
	
	rowC = rowA*rowB;
	colC = colA*colB;
	
	matrixC = (double**)malloc(rowC*sizeof(double*));
	
	for(i=0;i<rowA*rowB;i++){
		matrixC[i] = (double*)malloc(colA*colB*sizeof(double));
	}
	
	for(i=0;i<rowA;i++){
		for(j=0;j<colA;j++){
			startRow = i*rowB;
			startCol = j*colB;
			for(k=0;k<rowB;k++){
				for(l=0;l<colB;l++){
					matrixC[startRow+k][startCol+l] = matrixA[i][j]*matrixB[k][l];
				}
			}
		}
	}

	FILE* outputFile = fopen(output,"w");
	
	for(i=0;i<rowC;i++){
		for(j=0;j<colC;j++){
			fprintf(outputFile,"%lf\t",matrixC[i][j]);
		}
		fprintf(outputFile,"\n");
	}
	
	fclose(outputFile);
	
	printf("\n\n\nKronecker product of the two matrices written to %s.",output);
}










Fortran:       MODULE ARRAYMUSH	
       CONTAINS		
        SUBROUTINE KPRODUCT(A,B,AB)	
Considers the arrays to be addressed as A(row,column), despite any storage order arrangements.        .
Creating array AB to fit here, adjusting the caller's array AB, may not work on some compilers.
         INTEGER A(:,:),B(:,:)		
         INTEGER, ALLOCATABLE:: AB(:,:)	
         INTEGER R,RA,RB,C,CA,CB,I	
          RA = UBOUND(A,DIM = 1)	
          CA = UBOUND(A,DIM = 2)	
          RB = UBOUND(B,DIM = 1)	
          CB = UBOUND(B,DIM = 2)	
          WRITE (6,1) "A",RA,CA,"B",RB,CB,"A.k.B",RA*RB,CA*CB	
    1     FORMAT (3(A," is ",I0,"x",I0,1X))	
          IF (ALLOCATED(AB)) DEALLOCATE(AB)	
          ALLOCATE (AB(RA*RB,CA*CB))		
          R = 0		
          DO I = 1,RA	
            C = 0		
            DO J = 1,CA		
              AB(R + 1:R + RB,C + 1:C + CB) = A(I,J)*B	
              C = C + CB		
            END DO		
            R = R + RB		
          END DO	
        END SUBROUTINE KPRODUCT	

        SUBROUTINE SHOW(F,A)	
         INTEGER F	
         INTEGER A(:,:)	
         INTEGER R	
          DO R = 1,UBOUND(A,DIM = 1)	
            WRITE (F,1) A(R,:)		
    1       FORMAT (666I3)		
          END DO			
        END SUBROUTINE SHOW	
      END MODULE ARRAYMUSH	

      PROGRAM POKE
      USE ARRAYMUSH
      INTEGER A(2,2),B(2,2)		
      INTEGER, ALLOCATABLE:: AB(:,:)	
      INTEGER C(3,3),D(3,4)		
      DATA A/1,3, 2,4/,B/0,6, 5,7/	
      DATA C/0,1,0, 1,1,1, 0,1,0/	
      DATA D/1,1,1, 1,0,1, 1,0,1, 1,1,1/

      WRITE (6,*) "First test..."
      CALL KPRODUCT(A,B,AB)
      CALL SHOW (6,AB)

      WRITE (6,*)
      WRITE (6,*) "Second test..."
      CALL KPRODUCT(C,D,AB)
      CALL SHOW (6,AB)

      END