Skip to content

Commit 9cddb6e

Browse files
authored
Create find_max_min.cpp
This adds a C++ program to find the maximum and minimum element in an array. - File: arrays/find_max_min.cpp - Uses dynamic array input - Iterates through elements to compute max and min - Includes proper cleanup with delete[]
1 parent 3aafade commit 9cddb6e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

arrays/find_max_min.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file
3+
* @brief Program to find maximum and minimum in an array
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
int main() {
10+
int n;
11+
cout << "Enter size of array: ";
12+
cin >> n;
13+
14+
int* arr = new int[n];
15+
16+
cout << "Enter " << n << " elements: ";
17+
for (int i = 0; i < n; i++) {
18+
cin >> arr[i];
19+
}
20+
21+
int maxVal = arr[0];
22+
int minVal = arr[0];
23+
24+
for (int i = 1; i < n; i++) {
25+
if (arr[i] > maxVal) maxVal = arr[i];
26+
if (arr[i] < minVal) minVal = arr[i];
27+
}
28+
29+
cout << "Maximum: " << maxVal << endl;
30+
cout << "Minimum: " << minVal << endl;
31+
32+
delete[] arr;
33+
arr = nullptr;
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)