// definizione funzioni per manipolare Array #include using std::cout; using std::endl; #include using std::setw; #include "sortArray.h" double media( int answer[], int arraySize ) { int total = 0; // total response values for ( int i = 0; i < arraySize; i++ ) total += answer[ i ]; return static_cast< double >( total ) / arraySize; } // end function media int mediana( int answer[], int size ) { bubbleSort( answer, size ); // sort array return answer[ size / 2 ]; } // end function median // determine most frequent response int moda( int freq[], int answer[], int size, int highestValue ) { int largest = 0; // represents largest frequency int modeValue = 0; // represents most frequent response // initialize frequencies to 0 for ( int i = 0; i <= highestValue; i++ ) freq[ i ] = 0; // summarize frequencies for ( int j = 0; j < size; j++ ) ++freq[ answer[ j ] ]; // output results for ( int rating = 1; rating <= highestValue ; rating++ ) { // keep track of mode value and largest fequency value if ( freq[ rating ] > largest ) { largest = freq[ rating ]; modeValue = rating; } // end if } // end for return modeValue; } // end function mode // function that sorts an array with bubble sort algorithm void bubbleSort( int a[], int size ) { int hold; // temporary location used to swap elements // loop to control number of passes for ( int pass = 1; pass < size; pass++ ) // loop to control number of comparisons per pass for ( int j = 0; j < size - 1; j++ ) // swap elements if out of order if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } // end if } // end function bubbleSort // output array contents (20 values per row) void printArray( int a[], int size ) { cout << " Ecco la matrice: " << endl; for ( int i = 0; i < size; i++ ) { if ( i % 20 == 0 ) // begin new line every 20 values cout << endl; cout << setw( 4 ) << a[ i ]; } // end for cout << endl; } // end function printArray // funzione per stampare istogramma con elementi di una matrice void printHisto ( int a[], int size) { for ( int i = 0; i < size ; i++ ) { // output histogram bar representing frequency value cout << setw(2) << i << ' '; for ( int k = 1; k <= a[ i ]; k++ ) cout << '*'; cout << '\n'; // begin new line of output } // end outer for }