Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct{
  5.  
  6.     int dim;
  7.     float* elementi;
  8.  
  9. }matrice;
  10.  
  11. // Funzioni sul tipo di dato matrice
  12.  
  13. // scrittura dimensione
  14. void ScriviDim( matrice* m1, int dim_val );
  15.  
  16. // lettura dimensione
  17. int LeggiDim( matrice m1 );
  18.  
  19. // scrittura elemento
  20. void ScriviElem( matrice* m1, int index, float value );
  21.  
  22. // lettura elemento
  23. float LeggiElem( matrice m1, int index );
  24.  
  25. // allocazione matrice
  26. matrice* AllocaMatrice( matrice* m1, int dim );
  27.  
  28. // deallocazione matrice
  29. void DeallocaMatrice( matrice* m1 );
  30.  
  31. // scalare di un numero reale per la matrice
  32. void scalare(matrice m1, int dim, float s );
  33.  
  34. // stampa della matrice
  35. void StampaMatrice( matrice m1, int dim );
  36.  
  37.  
  38.  
  39. int main()
  40. {
  41.  
  42.     matrice matrix;
  43.  
  44.     int righe = 2;
  45.     int colonne = 3;
  46.  
  47.     int dimensione = righe * colonne;
  48.  
  49.     // scrivo la dimensione
  50.  
  51.     ScriviDim(&matrix ,dimensione);
  52.  
  53.     // alloco la matrice
  54.  
  55.     //AllocaMatrice( &matrix, LeggiDim(matrix) );
  56.  
  57.     // scrivo gli elementi della matrice
  58.  
  59.     int x = 0;
  60.  
  61.     while( x < LeggiDim(matrix) )
  62.     {
  63.         ScriviElem(&matrix,x,2.5);
  64.         x = x + 1;
  65.     }
  66.  
  67.     scalare(matrix, LeggiDim(matrix), 2.2 );
  68.  
  69.     StampaMatrice( matrix, LeggiDim(matrix) );
  70.  
  71.     DeallocaMatrice(&matrix);
  72.  
  73.  
  74.  
  75. return 0;
  76. }
  77.  
  78. void ScriviDim(matrice* m1, int dim_val )
  79. {
  80.     m1->dim = dim_val;
  81. }
  82.  
  83. int LeggiDim( matrice m1 )
  84. {
  85.     return m1.dim;
  86. }
  87.  
  88. void ScriviElem( matrice* m1, int index, float value )
  89. {
  90.     m1->elementi[index] = value;
  91. }
  92.  
  93. float LeggiElem( matrice m1, int index )
  94. {
  95.     return m1.elementi[index];
  96. }
  97.  
  98. matrice* AllocaMatrice( matrice* m1, int dim )
  99. {
  100.  
  101.     m1 = (matrice*) calloc(1,sizeof(matrice));
  102.  
  103.     if( dim > 0 )
  104.     {
  105.         m1->elementi = (float*) calloc(dim,sizeof(float) );
  106.     }
  107.  
  108.     return m1;
  109.  
  110. }
  111.  
  112. void DeallocaMatrice( matrice* m1 )
  113. {
  114.  
  115.     free(m1->elementi);
  116.  
  117.     free(m1);
  118.  
  119. }
  120.  
  121. void scalare( matrice m1, int dim, float s )
  122. {
  123.     // dichiaro matrice del prodotto scalare
  124.     matrice* Mscalare;
  125.  
  126.     // alloco la matrice del prodotto scalare
  127.     AllocaMatrice( Mscalare, dim );
  128.  
  129.     int i = 0;
  130.  
  131.     while( i < dim )
  132.     {
  133.  
  134.         Mscalare->elementi[i] =  s * LeggiElem( m1, i );
  135.  
  136.         i = i + 1;
  137.  
  138.     }
  139. }
  140.  
  141. void StampaMatrice( matrice m1, int dim )
  142. {
  143.     int i = 0;
  144.  
  145.     while( i < dim )
  146.     {
  147.  
  148.         printf("%f \n" ,LeggiElem( m1, i) );
  149.  
  150.         i = i + 1;
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement