fixed matrix_mult.c - works now
authorhackbard <hackbard>
Wed, 10 Apr 2002 23:36:17 +0000 (23:36 +0000)
committerhackbard <hackbard>
Wed, 10 Apr 2002 23:36:17 +0000 (23:36 +0000)
matrix_mult.c

index 0c5b1ab..f57268d 100644 (file)
@@ -1,6 +1,6 @@
 #include <stdio.h>
 
-int *pelemente, *pmatrix1, *pmatrix2;
+int *pelemente, *pmatrix1, *pmatrix2; 
 
 void read_matrix ( int *pzeilen, int *pspalten ) {
        
@@ -49,35 +49,54 @@ int z1,z2;
 int s1,s2;
 int i,j,k,ergebnis;
 
+/* welcome */
+printf("\nMatrixmultiplikation:\n\n");
+
+/* read matrix1 */
+printf("Matrix1:\n");
 read_matrix(&z1,&s1);
 
+/* save matrix1 for future use */
 pmatrix1=(int *)malloc(z1*s1 * sizeof(int));
-*pmatrix1=*pelemente;
+for (i=0;i<(s1*z1);++i) *(pmatrix1 + i)=*(pelemente + i);
 
-free( *pelemente );
+/* freeing memory */
+free( pelemente );
 
+/* read matrix2 */
+printf("Matrix2:\n");
 read_matrix(&z2,&s2);
 
+/* save matrix2 for future use */
 pmatrix2=(int *)malloc(z2*s2 * sizeof(int));         
-*pmatrix2=*pelemente;
-
-free( *pelemente );
+for (i=0;i<(s2*z2);++i) *(pmatrix2 + i)=*(pelemente + i);
 
+/* freeing memory */
+free( pelemente );
 
+/* just calculate if it is defined! */
 if ( s1!=z2 ) {
-       printf("Nicht definiert!\n");
+       printf("\n...well, i ll calculate it for you,\n\nestimated finish on the answer of the question of what life is all about:\t2323.Jan.15th\nand by the way, thanx for the fish. :)\n");
        return;
 }
 
+/* allocating memory for target matrix */
+
+pelemente=(int *)malloc(z1*s2 * sizeof(int));
+/* calculating matrix product */
 for (i=0;i<z1;i++) {
        for (j=0;j<s2;j++) {
-               for (k=0;k<s1;++k) ergebnis+=(*(pmatrix1 + i*s1 + k) * *(pmatrix2 + k*s2 + j));
+               ergebnis=0;
+               for (k=0;k<s1;++k) ergebnis+=((*(pmatrix1 + i*s1 + k)) * (*(pmatrix2 + k*s2 + j)));
                *(pelemente + i*s2 + j)=ergebnis;
        }
 }
        
 
+/* printout target matrix */
+printf("\n\nDie neue Matrix:\n\n");
 write_matrix(z1,s2);
+printf("\n");
 
 return;
 }