Skip to content

Commit a948df3

Browse files
authored
Merge pull request #491 from gpytak/master
Span of an Array
2 parents cddfb9e + 4f88a5e commit a948df3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Diff for: Span of an Array

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
// Creating the array and nth value
7+
int n;
8+
cout << "Enter nth number of values: ";
9+
cin >> n;
10+
cout << "Enter the values:" << endl;
11+
12+
// Entering values into the array
13+
int* arr = new int[n];
14+
for (int i = 0; i < n; i++){
15+
cin >> arr[i];
16+
}
17+
18+
// Maximum and Minimum values
19+
int min = arr[0];
20+
int max = arr[0];
21+
22+
// For-loop calculates maximum and minimum values of the array
23+
for (int i = 1; i < n; i++){
24+
if (arr[i] > max){
25+
max = arr[i];
26+
}
27+
if (arr[i] < min) {
28+
min = arr[i];
29+
}
30+
}
31+
32+
// Display maximum and minimum values
33+
cout << "\nMax: "<< max << endl;
34+
cout << "\nMin: " << min << endl;
35+
36+
// Display the span of the array
37+
int span = max - min;
38+
cout << "\nSpan: " << span << endl;
39+
}

0 commit comments

Comments
 (0)