Advertisement
Guest User

Matrice

a guest
Nov 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.98 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Esercitazione 7 Novembre
  5.  *
  6.  *  Author: Gramegna Simone
  7.  *
  8.  */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. // definisco le dimensioni massime di righe e colonne del tipo di dato matrice
  13. #define MAXRIGHE 200
  14. #define MAXCOLONNE 200
  15.  
  16. // definisco le dimensioni effettive delle due matrici
  17.  
  18. // numero righe e colonne prima matrice
  19. #define R1 2
  20. #define C1 3
  21. // numero righe e colonne seconda matrice
  22. #define R2 3
  23. #define C2 2
  24.  
  25. // definisco tipo di dato matrice
  26. typedef struct{
  27.  
  28.     int righe;
  29.     int colonne;
  30.  
  31.     float elementi[MAXRIGHE][MAXCOLONNE];
  32.  
  33. }matrice;
  34.  
  35. // funzione per leggere le righe
  36. int LeggiRighe( matrice m );
  37.  
  38. // funzione per scrivere le righe
  39. void ScriviRighe( matrice* m, int r_val );
  40.  
  41. // funzione per leggere le colonne
  42. int LeggiColonne( matrice m );
  43.  
  44. // funzione per scrivere le colonne
  45. void ScriviColonne( matrice* m, int c_val );
  46.  
  47. // funzione per leggere un elemento
  48. float LeggiElemento( matrice m, int riga, int colonna );
  49.  
  50. // funzione per scrivere un elemento
  51. void ScriviElemento( matrice* m, int riga, int colonna, float elem );
  52.  
  53. // funzione matrice trasposta
  54. void Trasposta( matrice* trasposta, matrice m, int r1, int c1 );
  55.  
  56. // funzione prodotto riga per colonna
  57. float ProdottoRigaColonna( matrice m1, matrice m2, int riga, int colonna );
  58.  
  59. // funzione prodotto tra due matrici
  60. void Prodotto( matrice* prodotto, matrice m1, matrice m2, int r1, int c1, int r2, int c2 );
  61.  
  62. // funzione che stampa una matrice
  63. void StampaMatrice( matrice m_stampa, int righe, int colonne );
  64.  
  65. int main()
  66. {
  67.  
  68.     matrice m1;
  69.     matrice m2;
  70.     matrice m_trasposta;
  71.     matrice m_prodotto;
  72.  
  73.     ScriviRighe(&m1,R1);
  74.     ScriviColonne(&m1,C1);
  75.  
  76.     // assegno valori alla prima matrice
  77.     ScriviElemento(&m1,0,0,8.1);
  78.     ScriviElemento(&m1,0,1,5.4);
  79.     ScriviElemento(&m1,0,2,2.1);
  80.     ScriviElemento(&m1,1,0,1.1);
  81.     ScriviElemento(&m1,1,1,6.1);
  82.     ScriviElemento(&m1,1,2,4.1);
  83.  
  84.  
  85.     ScriviRighe(&m2,R2);
  86.     ScriviColonne(&m2,C2);
  87.  
  88.     // assegno valori alla seconda matrice
  89.  
  90.     ScriviElemento(&m2,0,0,5.1);
  91.     ScriviElemento(&m2,0,1,2.2);
  92.     ScriviElemento(&m2,1,0,7.1);
  93.     ScriviElemento(&m2,1,1,9.4);
  94.     ScriviElemento(&m2,2,0,3.1);
  95.     ScriviElemento(&m2,2,1,1.7);
  96.  
  97.     // calcolo il prodotto di m1 * m2
  98.     Prodotto(&m_prodotto,m1,m2,R1,C1,R2,C2);
  99.  
  100.     // calcolo la matrice trasposta di m1
  101.     Trasposta(&m_trasposta,m1,R1,C1);
  102.  
  103.  
  104.     // stampo i risultati ottenuti
  105.  
  106.     printf("La matrice trasposta di m1 e': \n");
  107.         StampaMatrice( m_trasposta, LeggiRighe(m_trasposta), LeggiColonne(m_trasposta) );
  108.  
  109.     printf("\n Il prodotto delle due matrici m1 ed m2 e'\n");
  110.         StampaMatrice(m_prodotto,LeggiRighe(m_prodotto), LeggiColonne(m_prodotto) );
  111.  
  112.     system("pause");
  113. return 0;
  114. }
  115.  
  116. int LeggiRighe( matrice m )
  117. {
  118.     return   m.righe;
  119. }
  120.  
  121. void ScriviRighe( matrice* m, int r_val )
  122. {
  123.     m->righe = r_val;
  124. }
  125.  
  126. int LeggiColonne( matrice m )
  127. {
  128.     return  m.colonne;
  129. }
  130.  
  131. void ScriviColonne( matrice* m, int c_val )
  132. {
  133.     m->colonne = c_val;
  134. }
  135.  
  136. float LeggiElemento( matrice m, int riga, int colonna )
  137. {
  138.     return  m.elementi[riga][colonna];
  139. }
  140.  
  141. void ScriviElemento( matrice* m, int riga, int colonna, float elem )
  142. {
  143.     m->elementi[riga][colonna] =  elem;
  144. }
  145.  
  146. void Trasposta( matrice* trasposta, matrice m, int r1, int c1 )
  147. {
  148.  
  149.     ScriviRighe(trasposta, c1);
  150.     ScriviColonne(trasposta, r1);
  151.  
  152.     int i = 0;
  153.  
  154.     while( i < LeggiRighe((*trasposta)) )
  155.     {
  156.         int j = 0;
  157.  
  158.         while( j < LeggiColonne((*trasposta)) )
  159.         {
  160.  
  161.             float elem = LeggiElemento(m, j, i);
  162.             ScriviElemento(trasposta,i ,j ,elem);
  163.  
  164.             j = j + 1;
  165.  
  166.         }
  167.         i = i + 1;
  168.     }
  169. }
  170.  
  171. // funzione prodotto riga per colonna
  172. float ProdottoRigaColonna( matrice m1, matrice m2, int riga, int colonna )
  173. {
  174.  
  175.     float somma = 0;
  176.     int i = 0;
  177.  
  178.     while( i < LeggiColonne(m1) )
  179.     {
  180.  
  181.         float prodotto = LeggiElemento(m1, riga, i) * LeggiElemento(m2, i, colonna);
  182.         somma = somma + prodotto;
  183.  
  184.         i = i + 1;
  185.     }
  186.  
  187.     return somma;
  188. }
  189.  
  190. void Prodotto( matrice* prodotto, matrice m1, matrice m2, int r1, int c1, int r2, int c2 )
  191. {
  192.  
  193.     if( (c1 == r2) && (r1 == c2) )
  194.     {
  195.  
  196.         ScriviRighe(prodotto, r1);
  197.         ScriviColonne(prodotto, c2);
  198.  
  199.         int i = 0;
  200.  
  201.         while( i < LeggiRighe((*prodotto)) )
  202.         {
  203.  
  204.             int j = 0;
  205.  
  206.             while( j < LeggiColonne((*prodotto)) )
  207.             {
  208.  
  209.                 float prc = ProdottoRigaColonna(m1, m2, i, j);
  210.                 ScriviElemento(prodotto, i, j, prc );
  211.  
  212.                 j = j + 1;
  213.             }
  214.  
  215.             i = i + 1;
  216.         }
  217.     }
  218. }
  219.  
  220. void StampaMatrice( matrice m_stampa, int righe, int colonne )
  221. {
  222.  
  223.     int i = 0;
  224.  
  225.     while( i < righe )
  226.     {
  227.  
  228.         int j = 0;
  229.  
  230.         while( j < colonne )
  231.         {
  232.  
  233.             printf(" %.2f ", LeggiElemento(m_stampa, i, j) );
  234.  
  235.             j = j + 1;
  236.         }
  237.  
  238.        i = i + 1;
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement