Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib> // serve per RAND_MAX
- // #include <ctime> // serve per srand
- #include <cmath> //serve per fabs
- using namespace std;
- const int N=6,M=6;
- double matrice[N][M];
- double randMToN(double M, double N);
- double prodotto(double matrice[][M],int N);
- int main()
- { //srand(time(0)); // non sono sicuro che serva veramente in questa posizione, qualcuno si puo esprimere a riguardo?
- int i=0;
- while (i<N)
- {
- int k=0;
- while (k<M)
- {
- matrice[i][k]=randMToN(-9,9);
- cout << matrice[i][k] << " ";
- k++;
- }
- cout << endl;
- i++;
- }
- cout<<prodotto(matrice,N);
- return 0;
- }
- double randMToN(double M, double N)
- {
- return M + (rand() / ( RAND_MAX / (N-M) ) ) ;
- }
- double prodotto(double matrice[][M],int N)
- {
- int i=0,k,a=0,b=1; //inizializzo a,b e min con i primi 2 valori della matrice
- double min,temp;
- min=fabs(matrice[a][2]-matrice[b][2]);
- while (i<N)
- {
- k=i+1;
- while (k<N)
- {
- temp=fabs(matrice[i][2]-matrice[k][2]);
- if (temp<min)
- {
- min=temp;
- a=i;
- b=k;
- }
- k++;
- }
- i++;
- }
- cout<<"Il prodotto dei due numeri piu' vicini e' "<<matrice[a][2]<<"*"<<matrice[b][2]<<":";
- return matrice[a][2]*matrice[b][2];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement