Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created March 7, 2025 09:29
Show Gist options
  • Select an option

  • Save vlaleli/f3fe1b93ece3ef06d3ceb2d5a71960f5 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/f3fe1b93ece3ef06d3ceb2d5a71960f5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <limits>
using namespace std;
int max_value(int a, int b) {
return (a > b) ? a : b;
}
int max_value(int a, int b, int c) {
return max_value(max_value(a, b), c);
}
int max_value(int arr[], int size) {
int maxVal = numeric_limits<int>::min();
for (int i = 0; i < size; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
int max_value(int arr[][3], int rows, int cols) {
int maxVal = numeric_limits<int>::min();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > maxVal) {
maxVal = arr[i][j];
}
}
}
return maxVal;
}
int max_value(int arr[][3][3], int depth, int rows, int cols) {
int maxVal = numeric_limits<int>::min();
for (int i = 0; i < depth; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < cols; k++) {
if (arr[i][j][k] > maxVal) {
maxVal = arr[i][j][k];
}
}
}
}
return maxVal;
}
int main() {
int a = 10, b = 20, c = 15;
int arr1D[] = {1, 5, 9, 12, 7};
int arr2D[2][3] = {{3, 8, 2}, {4, 6, 10}};
int arr3D[2][3][3] = {{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}};
cout << "Max of " << a << " and " << b << " is: " << max_value(a, b) << endl;
cout << "Max of " << a << ", " << b << " and " << c << " is: " << max_value(a, b, c) << endl;
cout << "Max in 1D array: " << max_value(arr1D, 5) << endl;
cout << "Max in 2D array: " << max_value(arr2D, 2, 3) << endl;
cout << "Max in 3D array: " << max_value(arr3D, 2, 3, 3) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment