C Programm matrixmultiplikation

Neue Frage »

alimohsen Auf diesen Beitrag antworten »
C Programm matrixmultiplikation
Meine Frage:
Hallo leute,
ich habe ein programm geschrieben dass 2 3 mal 3 matrizen aus einer datei (matrix.txt) ausliest, sie miteinander multipliziert und in eine andere datei abspeichert. ich sollte jedoch kein malloc benutzen, und die programmteile in fuktionen einteilen (einlesen, multiplizieren, ausgeben, abspeichern)
hilft mir bitte !!!

Meine Ideen:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
#include <stdio.h>
#include <stdlib.h>
#define n 3
int main()
{
  int i;
  int j;
 
// Allocate enough memory for the arrays and the result
int** mat=malloc(n*sizeof(int*));
int** mat2=malloc(n*sizeof(int*));
int** result=malloc(n*sizeof(int*));
for(i=0;i<n;++i){
 
mat[i]=malloc((2*n)*sizeof(int));
mat2[i]=malloc((2*n)*sizeof(int));
result[i]=malloc((2*n)*sizeof(int));
}
// Open the file to read
  FILE *file;
  file=fopen("matrix.txt", "r");
//Copy the matrices to the arrays mat and mat2
for(i = 0; i < 2*n; i++)
  {
      for(j = 0; j < n; j++)
      {
          //When i reaches n we should set it to 0 to enable writing to the second array mat2
          if(i > n-1){
              if (!fscanf(file, "%i", &mat2[i-n][j]))
           break;
     
       printf("%i ",mat2[i-n][j]);
       
          }else{
  //Use lf format specifier, %c is for character
       if (!fscanf(file, "%i", &mat[i][j]))
           break;
    
       printf("%i ",mat[i][j]); //Use lf format specifier, \n is for new line
      }
}printf("\n");
  }
  // Matrices product
  int k;
  int sum;
     for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
         sum = 0;
         for (k = 0; k < n; k++) {
            sum = sum + mat[i][k] * mat2[k][j];
         }
         result[i][j] = sum;
      }
   }
   //Print the result as matrix format
      printf("\nMultiplication Of Two Matrices : \n");
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
         printf(" %i ", result[i][j]);
      }
      printf("\n");
   }
// Close the file after all tasks are fixed
  fclose(file);
  return (0);
}


Edit by IfindU: Code-Umgebung eingefuegt, damit man es besser lesen kann.
HAL 9000 Auf diesen Beitrag antworten »
RE: C Programm matrixmultiplikation
Wenn du nur die feste Matrixgröße nxn betrachten willst, brauchst du doch kein malloc! Das kannst du doch direkt deklarieren statt über dynamische Felder. Also

Zitat:
Original von alimohsen
// Allocate enough memory for the arrays and the result
int** mat=malloc(n*sizeof(int*));
int** mat2=malloc(n*sizeof(int*));
int** result=malloc(n*sizeof(int*));
for(i=0;i<n;++i){

mat[i]=malloc((2*n)*sizeof(int));
mat2[i]=malloc((2*n)*sizeof(int));
result[i]=malloc((2*n)*sizeof(int));
}

ersetzen durch

Zitat:
int mat[n][n], mat2[n][n], result[n][n];

In deinem Fall landen die Matrizen dann auf dem Stack. Sofern nur moderat groß ist (bei modernen 32Bit- oder 64Bit-PC o.ä. sagen wir mal <100), dann ist das durchaus Ok.


P.S.: Seltsam an deinem Programm finde ich, dass für die Zeilen doppelt so viel Speicher "malloc"t wird, wie nötig. verwirrt
alimohsen Auf diesen Beitrag antworten »

hey HAL9000 du hast recht Augenzwinkern
weisst du vielleicht auch wie die einzelnen funktionen aussehen ? auslesen , multiplizieren, ausgeben, abspeichern ?
danke
HAL 9000 Auf diesen Beitrag antworten »

Ich hatte oben schon den Verdacht, nur um es mal des Verständnis wegen zu klären: Du hast das Programm oben von irgendwo her blind kopiert und versuchst es nun zu verstehen?

Anders kann ich mir die Fragen

Zitat:
Original von alimohsen
weisst du vielleicht auch wie die einzelnen funktionen aussehen ? auslesen , multiplizieren, ausgeben, abspeichern ?

eigentlich nicht erklären.

Na was soll's:


Dynamische Speicherreservierung:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
// Allocate enough memory for the arrays and the result
int** mat=malloc(n*sizeof(int*));
int** mat2=malloc(n*sizeof(int*));
int** result=malloc(n*sizeof(int*));
for(i=0;i<n;++i){

mat[i]=malloc((2*n)*sizeof(int));
mat2[i]=malloc((2*n)*sizeof(int));
result[i]=malloc((2*n)*sizeof(int));
}

Einlesen der beiden Matrizen "mat" und "mat2" aus Textfile & Ausgabe der beiden auf Terminal
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
// Open the file to read
  FILE *file;
  file=fopen("matrix.txt", "r");
//Copy the matrices to the arrays mat and mat2
for(i = 0; i < 2*n; i++)
  {
      for(j = 0; j < n; j++)
      {
          //When i reaches n we should set it to 0 to enable writing to the second array mat2
          if(i > n-1){
              if (!fscanf(file, "%i", &mat2[i-n][j]))
           break;

       printf("%i ",mat2[i-n][j]);

          }else{
  //Use lf format specifier, %c is for character
       if (!fscanf(file, "%i", &mat[i][j]))
           break;

       printf("%i ",mat[i][j]); //Use lf format specifier, \n is for new line
      }
}printf("\n");
  }
fclose(file);


Berechnen des Matrixprodukts "result = mat * mat2"
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  // Matrices product
  int k;
  int sum;
     for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
         sum = 0;
         for (k = 0; k < n; k++) {
            sum = sum + mat[i][k] * mat2[k][j];
         }
         result[i][j] = sum;
      }
   }

Ausgabe der Ergebnismatrix "result" auf Terminal
code:
1:
2:
3:
4:
5:
6:
7:
8:
   //Print the result as matrix format
      printf("\nMultiplication Of Two Matrices : \n");
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
         printf(" %i ", result[i][j]);
      }
      printf("\n");
   }


"Abspeichern" (in File) ist oben nicht realisiert - du kannst aber natürlich die Ausgabe in eine Datei umleiten, auf Kommandozeilenebene per > result.txt.
alimohsen Auf diesen Beitrag antworten »

HAL9000
nicht so ganz, also ein freund hat mir dabei geholfen... ich verstehe wie das programm funktioniert aber ich bin nicht routiniert im programmieren deswegen habe ich schwierigkeiten das hinzubekommen... ich sollte einfach mit zeiger arbeiten (* und & ) das erspart schreiben und aufwand.
danke dir , durch deine kommentare wird übersichtlicher und einfacher zu verstehen
Neue Frage »
Antworten »



Verwandte Themen

Die Beliebtesten »
Die Größten »
Die Neuesten »