School-Coding-Cpp/sfusi/numero occorrenze.cpp

55 lines
1.1 KiB
C++

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#define MAX 10
#define MIN 0
using namespace std;
void riempiMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
for(size_t i = 0; i < RIGHE; i++)
{
for(size_t j = 0; j < COLONNE; j++){
matrice[i][j]=rand()%(MAX-MIN+1)+MIN;
cout << setw(6) << matrice[i][j];
}
cout << endl;
}
cout << endl << endl << endl;
}
void contaOccorrenze(int matrice[][3], size_t RIGHE, size_t COLONNE, size_t valore){
int conteggio = 0;
for(size_t k = 0; k < RIGHE; k++)
{
for(size_t l = 0; l < COLONNE; l++)
{
if(matrice[k][l] == valore)
conteggio++;
}
}
cout << "il numero e' presente: " << conteggio;
}
int main(void){
srand(time(NULL));
int const RIGHE = 3;
int const COLONNE = 3;
int matrice[RIGHE][COLONNE];
int valore;
riempiMatrice(matrice, RIGHE, COLONNE);
cout << endl << endl;
cout << "inserisci il valore di cui contare le occorrenze nella matrice" << endl << "-> ";
cin >> valore;
contaOccorrenze(matrice, RIGHE, COLONNE, valore);
return 0;
}