Добавить maxElem.cpp
This commit is contained in:
100
maxElem.cpp
Normal file
100
maxElem.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user