Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <random>
- #include <ctime>
- typedef int myInt;
- using namespace std;
- int action(int* arrayA, int* arrayB, int sizeA, int sizeB, int(*func[3])(int*, int)) {
- cout << "Enter the array you are going to work with(1 - arrayA or 2 - arrayB): ";
- int numArray;
- cin >> numArray;
- cout << "Enter the action which you want to perform with the array: " << endl;
- cout << "1 - to find maximum" << endl;
- cout << "2 - to find minimum" << endl;
- cout << "3 - to find average" << endl;
- int numAct;
- cin >> numAct;
- switch (numArray) {
- case 1: {
- switch (numAct) {
- case 1: return (*func[0])(arrayA, sizeA); break;
- case 2: return (*func[1])(arrayA, sizeA); break;
- case 3: return (*func[2])(arrayA, sizeA); break;
- }
- break;
- }
- case 2: {
- switch (numAct) {
- case 1: return (*func[0])(arrayB, sizeB); break;
- case 2: return (*func[1])(arrayB, sizeB); break;
- case 3: return (*func[2])(arrayB, sizeB); break;
- }
- break;
- }
- }
- }
- int max(int* array, int size) {
- int max = INT_MIN;
- for (int i = 0; i < size; ++i) {
- if (array[i] > max) max = array[i];
- }
- cout << "Max = " << max;
- return max;
- }
- int min(int* array, int size) {
- int min = INT_MAX;
- for (int i = 0; i < size; ++i) {
- if (array[i] < min) min = array[i];
- }
- cout << "Min = " << min;
- return min;
- }
- int avg(int* array, int size) {
- double sum = 0.0;
- for (int i = 0; i < size; ++i) {
- sum += array[i];
- }
- double avg = sum / size;
- cout << "Avg = " << fixed << setprecision(2) << avg;
- return avg;
- }
- void print(int* array, int size) {
- for (int i = 0; i < size; ++i) {
- cout << array[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- random_device rand;
- mt19937 gen(time(NULL));
- uniform_int_distribution<> result(1, 100);
- const int sizeA = 10;
- int arrayA[sizeA] = {};
- for (int i = 0; i < sizeA; ++i) {
- arrayA[i] = result(gen);
- }
- const int sizeB = 15;
- int arrayB[sizeB] = {};
- for (int i = 0; i < sizeB; ++i) {
- arrayB[i] = result(gen);
- }
- print(arrayA, sizeA);
- print(arrayB, sizeB);
- int(*func[3])(int*, int) = { max, min, avg };
- action(arrayA, arrayB, sizeA, sizeB, func);
- return 0;
- }
Add Comment
Please, Sign In to add comment