Facebook
From Round Hummingbird, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 278
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int rowlength,collength;
  7.     int row=0,col=0;
  8.     FILE* file;
  9.     file = fopen("macierz.txt", "r");
  10.     printf("Podaj ilosc wierszy ");
  11.     scanf("%d",&rowlength);
  12.     printf("Podaj ilosc kolumn ");
  13.     scanf("%d",&collength);
  14.  
  15.     int   tab[rowlength][collength],  tab1[rowlength][collength];
  16.  
  17.     printf("Elementy w pliku\n\n");
  18.  
  19.     while( fscanf( file, "%d,", &tab[row][col] ) != EOF )
  20.     {
  21.         printf("%d ", tab[row][col]);
  22.         col++;
  23.  
  24.         if(col==3 && row==2)   // Ten warunek == wypelnieniem calej tabeli wartoœciami z 1 macierzy.
  25.         {
  26.             printf("\n\n");
  27.             row=0;
  28.             col=0;
  29.             while( fscanf( file, "%d,", &tab1[row][col] ) != EOF )
  30.             {
  31.                 printf("%d ", tab1[row][col]);
  32.                 col++;
  33.                 if(col==3)
  34.                 {
  35.                     printf("\n");
  36.                     col=0;
  37.                     row++;
  38.                 }
  39.             }
  40.         }
  41.         if(col==3)
  42.         {
  43.             printf("\n");
  44.             col=0;
  45.             row++;
  46.         }
  47.     }
  48.     printf("\n");
  49.     fclose(file);
  50.  
  51.     mnozeniemacierzy(3,3,tab, tab1);
  52.  
  53.     return 0;
  54. }
  55.  
  56. int mnozeniemacierzy(int n, int m , int tab[n][m],int tab1[n][m])
  57.  
  58. {
  59.  
  60.     int i,j;
  61.     int tab2[n][m];
  62.     printf("Wynik mnozenia pierwszej macierzy przez druga:\n\n");
  63.  
  64.     for(i=0; i<3; i++)
  65.     {
  66.  
  67.         tab2[0][i] = tab[0][0]*tab1[0][i]+tab[0][1]*tab1[1][i]+tab[0][2]*tab1[2][i];
  68.  
  69.     }
  70.     printf("\n");
  71.     for(i=0; i<3; i++)
  72.     {
  73.  
  74.         tab2[1][i] = tab[1][0]*tab1[0][i]+tab[1][1]*tab1[1][i]+tab[1][2]*tab1[2][i];
  75.  
  76.     }
  77.     printf("\n");
  78.     for(i=0; i<3; i++)
  79.     {
  80.  
  81.         tab2[2][i] = tab[2][0]*tab1[0][i]+tab[2][1]*tab1[1][i]+tab[2][2]*tab1[2][i];
  82.  
  83.     }
  84.     for(i=0; i<3; i++)
  85.     {
  86.         for(j=0; j<3; j++)
  87.         {
  88.             printf("%d ", tab2[i][j]);
  89.         }
  90.         printf("\n");
  91.     }
  92.     zapisywanie(tab2);
  93. }
  94. void zapisywanie (int tab2[3][3])
  95. {
  96.     int i,j;
  97.     FILE* file2;
  98.     file2 = fopen("wynik.txt", "w");
  99.     if(file2==NULL) printf("Nie moge otworzyc!");            //zapisanie do pliku
  100.     else
  101.     {
  102.         fprintf(file2,"Wynik to: \n");
  103.         for(i=0; i<3; i++)
  104.         {
  105.             for(j=0; j<3; j++)
  106.             {
  107.                 fprintf(file2,"%d ", tab2[i][j]);
  108.             }
  109.             fprintf(file2,"\n");
  110.         }
  111.  
  112.         fclose(file2);
  113.  
  114.     }
  115. }
  116.