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