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

Replies to Untitled rss

Title Name Language When
Re: Untitled Colossal Tortoise c 6 Years ago.