Advertisement
Emanuele_Bruno

Esercizio 1 del 03/02/2015

Dec 14th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib> // serve per RAND_MAX
  3. // #include <ctime> // serve per srand
  4. #include <cmath> //serve per fabs
  5.  
  6. using namespace std;
  7. const int N=6,M=6;
  8.  
  9. double matrice[N][M];
  10.  
  11. double randMToN(double M, double N);
  12. double prodotto(double matrice[][M],int N);
  13.  
  14. int main()
  15. {   //srand(time(0)); // non sono sicuro che serva veramente in questa posizione, qualcuno si puo esprimere a riguardo?
  16.     int i=0;
  17.     while (i<N)
  18.     {
  19.         int k=0;
  20.         while (k<M)
  21.         {
  22.             matrice[i][k]=randMToN(-9,9);
  23.             cout << matrice[i][k] << " ";
  24.             k++;
  25.         }
  26.         cout << endl;
  27.         i++;
  28.     }
  29.     cout<<prodotto(matrice,N);
  30.     return 0;
  31. }
  32.  
  33. double randMToN(double M, double N)
  34. {
  35.     return M + (rand() / ( RAND_MAX / (N-M) ) ) ;
  36. }
  37.  
  38. double prodotto(double matrice[][M],int N)
  39. {
  40.     int i=0,k,a=0,b=1; //inizializzo a,b e min con i primi 2 valori della matrice
  41.     double min,temp;
  42.     min=fabs(matrice[a][2]-matrice[b][2]);
  43.     while (i<N)
  44.     {
  45.         k=i+1;
  46.         while (k<N)
  47.         {
  48.             temp=fabs(matrice[i][2]-matrice[k][2]);
  49.             if (temp<min)
  50.             {
  51.                 min=temp;
  52.                 a=i;
  53.                 b=k;
  54.             }
  55.             k++;
  56.         }
  57.         i++;
  58.     }
  59.     cout<<"Il prodotto dei due numeri piu' vicini e' "<<matrice[a][2]<<"*"<<matrice[b][2]<<":";
  60.     return matrice[a][2]*matrice[b][2];
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement