From 3045cc5b6b137f94e18ecf591d704e341d39f6d5 Mon Sep 17 00:00:00 2001 From: Misha Date: Tue, 23 Sep 2025 20:06:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20maxElem.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maxElem.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 maxElem.cpp diff --git a/maxElem.cpp b/maxElem.cpp new file mode 100644 index 0000000..f9e218b --- /dev/null +++ b/maxElem.cpp @@ -0,0 +1,100 @@ +#include +#include +#define N 5 +#define B 5 +using namespace std; + +int maxElem(int x, int y) { + int max = (x > y) ? x : y; + return max; +} + +int maxElem(int x, int y, int z) { + int max = (x > y) ? x : y; + max = (max > z) ? max : z; + return max; +} + +int maxElem(int a[], int n) { + int max = a[0]; + for (int i = 1; i < n; i++) { + if (a[i] > max) { + max = a[i]; + } + } + return max; +} + +int maxElem(int a[][N], int n) { + int max = a[0][0]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < N; j++) { + if (a[i][j] > max) { + max = a[i][j]; + } + } + } + return max; +} + +int maxElem(int a[][N][B], int n) { + int max = a[0][0][0]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < B; k++) { + if (a[i][j][k] > max) { + max = a[i][j][k]; + } + } + } + } + return max; +} + +int main() { + srand(time(0)); + setlocale(0, ""); + + int x = rand() % 100; + int y = rand() % 100; + int z = rand() % 100; + cout << "Случайные числа: x = " << x << ", y = " << y << ", z = " << z << endl; + cout << "max(2): " << maxElem(x, y) << endl; + cout << "max(3): " << maxElem(x, y, z) << endl << endl; + + int arr1[5]; + cout << "Одномерный массив: "; + for (int i = 0; i < 5; i++) { + arr1[i] = rand() % 100; + cout << arr1[i] << " "; + } + cout << "\nmax(1D): " << maxElem(arr1, 5) << endl << endl; + + int arr2[2][N]; + cout << "Двумерный массив:\n"; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < N; j++) { + arr2[i][j] = rand() % 100; + cout << arr2[i][j] << "\t"; + } + cout << endl; + } + cout << "max(2D): " << maxElem(arr2, 2) << endl << endl; + + int arr3[2][N][B]; + cout << "Трехмерный массив:\n"; + for (int i = 0; i < 2; i++) { + cout << "Плоскость " << i << ":\n"; + for (int j = 0; j < N; j++) { + for (int k = 0; k < B; k++) { + arr3[i][j][k] = rand() % 100; + cout << arr3[i][j][k] << "\t"; + } + cout << endl; + } + cout << endl; + } + cout << "max(3D): " << maxElem(arr3, 2) << endl; + + return 0; +} \ No newline at end of file