1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillAndPrintArray( int [], int, int );
void statsRefParam( int [], int, int &, int &, double & );
void statsPtrParam( int [], int, int *, int *, double * );
void display(int, int, double );
int main()
{
int arraySize;
int max, min;
double mean;
const int MINIMUM = 1,
MAXLINE = 5;
int *data = nullptr;
cout << "Kamil Walicki" << endl;
cout << "Array size: ";
cin >> arraySize;
while (arraySize < MINIMUM)
{
cout << "Please enter a value greater than one" << endl;
cin >> arraySize;
}
data = new int[arraySize];
if (data == nullptr)
{
cout << "Out of memory" << endl;
exit(1);
}
fillAndPrintArray( data, arraySize, MAXLINE );
statsRefParam( data, arraySize, min, max, mean );
display(max, min, mean );
mean = max = min = 0;
statsPtrParam( data, arraySize, &min, &max, &mean );
display(max, min, mean);
delete[] data;
data = nullptr;
return 0;
}
void fillAndPrintArray(int numbers,int size, int max )
{
for (int i = 0; i < size; i++)
{
numbers = rand() % 100 + 1;
numbers++;
}
for (int x = 1, i = 0; i < size; i++)
{
cout << numbers << ' ';
x++;
numbers++;
if (x == max)
{
cout << endl;
x = 0;
}
}
}
void display( int maximum, int minimum, double average )
{
cout << endl << endl << "The largest value is: " << maximum << endl;
cout << "The smallest value is: " << minimum << endl;
cout << fixed << "The average is: " << setprecision(1) << average << endl << endl;
}
void statsRefParam( int numbers, int size, int &minimum, int &maximum, double &average )
{
int sum = 0;
minimum = maximum = numbers;
for (int i = 0; i < size; i++)
{
if (numbers < minimum)
minimum = numbers;
else if (numbers > maximum)
maximum = numbers;
sum += numbers;
}
average = static_cast<double>(sum) / size;
}
void statsPtrParam(int *numbers, int size, int *pMinimum, int *pMaximum, double *pAverage)
{
double sum = 0;
pMinimum = pMaximum = numbers;
for (int i = 0; i < size; i++)
{
if (numbers < pMinimum)
pMinimum = numbers;
else if (numbers > pMaximum)
pMaximum = numbers;
sum += *numbers;
}
*pAverage = (sum / size);
}
|