Facebook
From Silly Octupus, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 394
  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.  
  23.     int tab[rowlength][collength],tab1[row2length][col2length];
  24.  
  25.     printf("Elements in the text file:\n\n");
  26.  
  27.     while( fscanf( file, "%d,", &tab[row][col] ) != EOF )  //WCZYTYWANIE WARTOŚCI Z PLIKU TXT DO 2 TABLIC
  28.     {
  29.  
  30.         printf("%d ", tab[row][col]);
  31.  
  32.         col++;
  33.  
  34.         if(col==collength && row==rowlength-1)
  35.         {
  36.             printf("\n\n");
  37.             row=0;
  38.             col=0;
  39.  
  40.             while( fscanf( file, "%d,", &tab1[row][col] ) != EOF )
  41.             {
  42.  
  43.                 printf("%d ", tab1[row][col]);
  44.                 col++;
  45.  
  46.                 if(col==col2length)
  47.  
  48.                 {
  49.  
  50.                     printf("\n");
  51.                     col=0;
  52.                     row++;
  53.  
  54.                 }
  55.             }
  56.         }
  57.         if(col==collength)
  58.         {
  59.  
  60.             printf("\n");
  61.             col=0;
  62.             row++;
  63.  
  64.         }
  65.     }
  66.     printf("\n");
  67.  
  68.     fclose(file);
  69.  
  70.     Multiplication(rowlength,collength,row2length,col2length,tab, tab1);
  71.  
  72.     return 0;
  73. }
  74.  
  75. int Multiplication(int rowlength, int collength, int row2length, int col2length, int tab[rowlength][collength],int tab1[row2length][col2length]) // MNOŻENIE MACIERZY
  76. {
  77.     int i,j,k;
  78.     int tab2[rowlength][col2length];
  79.     printf("Result of multiplication:nn");
  80.  
  81.     for(i=0; i<rowlength; i++)
  82.     {
  83.         for(j=0; j<col2length; j++)
  84.         {
  85.             tab2[i][j] = 0;
  86.             for(k=0; k<row2length; k++)
  87.             {
  88.  
  89.                 tab2[i][j] += tab[i][k] * tab1[k][j];
  90.  
  91.             }
  92.         }
  93.     }
  94.  
  95.     for(i=0; i<row2length; i++)
  96.     {
  97.         for(j=0; j<col2length; j++)
  98.         {
  99.  
  100.             printf("%d ", tab2[i][j]);
  101.  
  102.         }
  103.         printf("\n");
  104.     }
  105.     Save(row2length, col2length, tab2);
  106. }
  107. void Save(int row2length, int col2length, int tab2[row2length][col2length]) // ZAPISYWANIE MACIERZY WYNIKOWEJ DO PLIKU
  108. {
  109.     int i,j;
  110.     FILE* file2;
  111.     file2 = fopen("wynik.txt", "w");
  112.     if(file2==NULL) printf("Can't open the file!");
  113.  
  114.     else
  115.     {
  116.         fprintf(file2,"The result: n");
  117.  
  118.         for(i=0; i<row2length; i++)
  119.         {
  120.             for(j=0; j<col2length; j++)
  121.             {
  122.  
  123.                 fprintf(file2,"%d ", tab2[i][j]);
  124.  
  125.             }
  126.  
  127.             fprintf(file2,"\n");
  128.         }
  129.  
  130.         fclose(file2);
  131.  
  132.     }
  133. }
  134.