Facebook
From Bistre Goat, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 640
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int rowlength,collength,row2length,col2length;
  7.     int row=0,col=0;
  8.  
  9.     FILE* file;
  10.     file = fopen("macierz.txt", "r");
  11.  
  12.     printf("Input number of rows for the first array!");
  13.     scanf("%d",&rowlength);
  14.     printf("Input number of columns for the first array!");
  15.     scanf("%d",&collength);
  16.     printf("Input number of rows for the second array!");
  17.     scanf("%d",&row2length);
  18.     printf("Input number of columns for the second array!");
  19.     scanf("%d",&col2length);
  20.  
  21.  
  22.     int tab[rowlength][collength],tab1[row2length][col2length];
  23.  
  24.     printf("Elements in the text file:\n\n");
  25.  
  26.     while( fscanf( file, "%d,", &tab[row][col] ) != EOF )  //WCZYTYWANIE WARTOŚCI Z PLIKU TXT DO 2 TABLIC
  27.     {
  28.  
  29.         printf("%d ", tab[row][col]);
  30.  
  31.         col++;
  32.  
  33.         if(col==collength && row==rowlength-1)
  34.         {
  35.             printf("\n\n");
  36.             row=0;
  37.             col=0;
  38.  
  39.             while( fscanf( file, "%d,", &tab1[row][col] ) != EOF )
  40.             {
  41.                
  42.                 printf("%d ", tab1[row][col]);
  43.                 col++;
  44.  
  45.                 if(col==col2length)
  46.  
  47.                 {
  48.                     printf("\n");
  49.                     col=0;
  50.                     row++;
  51.                 }
  52.             }
  53.         }
  54.         if(col==collength)
  55.         {
  56.             printf("\n");
  57.             col=0;
  58.             row++;
  59.         }
  60.     }
  61.     printf("\n");
  62.  
  63.     fclose(file);
  64.  
  65.     Multiplication(rowlength,collength,row2length,col2length,tab, tab1);
  66.  
  67.     return 0;
  68. }
  69.  
  70. int Multiplication(int rowlength, int collength, int row2length, int col2length, int tab[rowlength][collength],int tab1[row2length][col2length]) // MNOŻENIE MACIERZY
  71. {
  72.     int i,j,k;
  73.     int tab2[rowlength][col2length];
  74.     printf("Result of multiplication:nn");
  75.  
  76.     for(i=0; i<rowlength; i++)
  77.     {
  78.         for(j=0; j<col2length; j++)
  79.         {
  80.             tab2[i][j] = 0;
  81.             for(k=0; k<row2length; k++)
  82.             {
  83.                 tab2[i][j] += tab[i][k] * tab1[k][j];
  84.             }
  85.         }
  86.     }
  87.  
  88.     for(i=0; i<row2length; i++)
  89.     {
  90.         for(j=0; j<col2length; j++)
  91.         {
  92.             printf("%d ", tab2[i][j]);
  93.         }
  94.         printf("\n");
  95.     }
  96.     Save(row2length, col2length, tab2);
  97. }
  98. void Save(int row2length, int col2length, int tab2[row2length][col2length]) // ZAPISYWANIE MACIERZY WYNIKOWEJ DO PLIKU
  99. {
  100.     int i,j;
  101.     FILE* file2;
  102.     file2 = fopen("wynik.txt", "w");
  103.     if(file2==NULL) printf("Can't open the file!");
  104.  
  105.     else
  106.     {
  107.         fprintf(file2,"The result: n");
  108.  
  109.         for(i=0; i<row2length; i++)
  110.         {
  111.             for(j=0; j<col2length; j++)
  112.             {
  113.                 fprintf(file2,"%d ", tab2[i][j]);
  114.             }
  115.  
  116.             fprintf(file2,"\n");
  117.         }
  118.         fclose(file2);
  119.  
  120.     }
  121. }
  122.