80 lines
1.6 KiB
C++
80 lines
1.6 KiB
C++
//Cristian Ronzoni 3Ain
|
|
/*Fare un algoritmo che calcoli la media
|
|
e rilevi i numeri maggiori e minori alla media stessa*/
|
|
#include <iostream>
|
|
#include <ctime>
|
|
#include <cstdlib>
|
|
#include <iomanip>
|
|
#define NUM 7
|
|
using namespace std;
|
|
void riempiMatrice(int arr[NUM][NUM]);
|
|
void stampaMatrice(int arr[NUM][NUM]);
|
|
void maxMatrice(int arr[NUM][NUM]);
|
|
void minMatrice(int arr[NUM][NUM]);
|
|
|
|
|
|
void calcolaMediaMatrice(int arr[NUM][NUM]){
|
|
float media = 0;
|
|
for(size_t i = 0; i<NUM; ++i){
|
|
for(size_t j = 0; j<NUM; ++j){
|
|
media += arr[i][j];
|
|
}
|
|
}
|
|
media = media / (NUM*NUM);
|
|
cout << "La media della matrice e':"<< media;
|
|
}
|
|
int main(void){
|
|
srand(time(NULL));
|
|
int bobby[NUM][NUM];
|
|
riempiMatrice(bobby);
|
|
stampaMatrice(bobby);
|
|
calcolaMediaMatrice(bobby);
|
|
maxMatrice(bobby);
|
|
minMatrice(bobby);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void riempiMatrice(int arr[NUM][NUM]){
|
|
for(size_t i = 0; i<NUM; ++i){
|
|
for(size_t j = 0; j<NUM; ++j){
|
|
arr[i][j] = rand() % 40;
|
|
}
|
|
}
|
|
}
|
|
void stampaMatrice(int arr[NUM][NUM]){
|
|
for(size_t i = 0; i<NUM; ++i){
|
|
for(size_t j = 0; j<NUM; ++j){
|
|
cout << setw(3) << arr[i][j];
|
|
}
|
|
cout << endl;
|
|
}
|
|
}
|
|
|
|
void maxMatrice(int arr[NUM][NUM]){
|
|
int max = arr[0][0];
|
|
for(size_t i = 0; i<NUM ; ++i){
|
|
for(size_t j = 0; j<NUM; ++j){
|
|
if(arr[i][j] > max)
|
|
max = arr[i][j];
|
|
}
|
|
}
|
|
cout << endl << "Il numero più grande della matrice e':"<<max;
|
|
|
|
}
|
|
|
|
|
|
void minMatrice(int arr[NUM][NUM]){
|
|
int min = arr[0][0];
|
|
for(size_t i = 0; i<NUM; ++i){
|
|
for(size_t j = 0; j<NUM; ++j){
|
|
if(arr[i][j] < min){
|
|
min = arr[i][j];
|
|
}
|
|
}
|
|
}
|
|
cout << endl << "Il numero più piccolo della matrice e':" << min;
|
|
}
|