-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpan of an Array
More file actions
39 lines (33 loc) · 759 Bytes
/
Span of an Array
File metadata and controls
39 lines (33 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
int main()
{
// Creating the array and nth value
int n;
cout << "Enter nth number of values: ";
cin >> n;
cout << "Enter the values:" << endl;
// Entering values into the array
int* arr = new int[n];
for (int i = 0; i < n; i++){
cin >> arr[i];
}
// Maximum and Minimum values
int min = arr[0];
int max = arr[0];
// For-loop calculates maximum and minimum values of the array
for (int i = 1; i < n; i++){
if (arr[i] > max){
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
// Display maximum and minimum values
cout << "\nMax: "<< max << endl;
cout << "\nMin: " << min << endl;
// Display the span of the array
int span = max - min;
cout << "\nSpan: " << span << endl;
}