Facebook
From Silly Dormouse, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 273
  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.                     printf("\n");
  50.                     col=0;
  51.                     row++;
  52.                 }
  53.             }
  54.         }
  55.         if(col==collength)
  56.         {
  57.  
  58.             printf("\n");
  59.             col=0;
  60.             row++;
  61.  
  62.         }
  63.     }
  64.     printf("\n");
  65.  
  66.     fclose(file);
  67.  
  68.     Multiplication(rowlength,collength,row2length,col2length,tab, tab1);
  69.  
  70.     return 0;
  71. }
  72.  
  73. int Multiplication(int rowlength, int collength, int row2length, int col2length, int tab[rowlength][collength],int tab1[row2length][col2length]) // MNOŻENIE MACIERZY
  74. {
  75.     int i,j,k;
  76.     int tab2[rowlength][col2length];
  77.     printf("Result of multiplication:nn");
  78.  
  79.     for(i=0; i<rowlength; i++)
  80.     {
  81.         for(j=0; j<col2length; j++)
  82.         {
  83.             tab2[i][j] = 0;
  84.             for(k=0; k<row2length; k++)
  85.             {
  86.  
  87.                 tab2[i][j] += tab[i][k] * tab1[k][j];
  88.  
  89.             }
  90.         }
  91.     }
  92.  
  93.     for(i=0; i<row2length; i++)
  94.     {
  95.         for(j=0; j<col2length; j++)
  96.         {
  97.  
  98.             printf("%d ", tab2[i][j]);
  99.  
  100.         }
  101.         printf("\n");
  102.     }
  103.     Save(row2length, col2length, tab2);
  104. }
  105. void Save(int row2length, int col2length, int tab2[row2length][col2length]) // ZAPISYWANIE MACIERZY WYNIKOWEJ DO PLIKU
  106. {
  107.     int i,j;
  108.     FILE* file2;
  109.     file2 = fopen("wynik.txt", "w");
  110.     if(file2==NULL) printf("Can't open the file!");
  111.  
  112.     else
  113.     {
  114.         fprintf(file2,"The result: n");
  115.  
  116.         for(i=0; i<row2length; i++)
  117.         {
  118.             for(j=0; j<col2length; j++)
  119.             {
  120.  
  121.                 fprintf(file2,"%d ", tab2[i][j]);
  122.  
  123.             }
  124.  
  125.             fprintf(file2,"\n");
  126.         }
  127.  
  128.         fclose(file2);
  129.  
  130.     }
  131. }
  132.