# Find the highest and lowest value in C++ without the use of an array or function.

## Hello Community! 👋

Greetings folks, I came across a question the other day that asked to write an algorithm that returns the highest and lowest values without an array or function.

There are many different approaches to solving this question and I decided to share my solution.

Replacing the need for an array

• An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.

• To replace the array that would store the number of elements to be entered, we would use a N digit.

``````#include <iostream>
using namespace std;

int main() {

int i;
int N;
int highest;
int lowest;
int num;

i = 1;

cout << "Enter the amount of numbers in the series: " ;
cin >> N;

``````

Getting the first number

• At the start of writing this algorithm, an integer "i" is declared and given the initial value of 1. "i" will act as the counter of the loop and in order to return the highest and lowest number from the series, a number must first be entered before entering the loop.

• The value of this number is then assigned to the variable highest and the variable lowest. This is done so that inside the loop when other values are entered, those values can be compared against the values stored in highest and lowest.

``````#include <iostream>
using namespace std;

int main() {

int i;
int N;
int highest;
int lowest;
int num;

i = 1;

cout << "Enter the amount of numbers in the series: " ;
cin >> N;

cout << "Enter a number: ";
cin >> num;
highest =  num;
lowest = num;
``````

Running the loop

• The loop is ran based off a counter, and that counter each time the loop is incremented checks to see if it is still less than the value stored in N which is the amount of numbers to be entered in the series.

• The value stored in "num" is compared to the value stored in highest. If the value is greater than the value stored in highest, highest therefore gets the value stored in num.

• The value stored in "num" is also compared to the value stored in lowest. If the value is less than the value stored in lowest, lowest therefore gets the value stored in num.

• A number is then asked to be entered once again to exit the loop, and the counter for the loop is incremented.

``````#include <iostream>
using namespace std;

int main() {

int i;
int N;
int highest;
int lowest;
int num;

i = 1;

cout << "Enter the amount of numbers in the series: " ;
cin >> N;

cout << "Enter a number: ";
cin >> num;
highest =  num;
lowest = num;

while ( i < N ){

if (num > highest ) {
highest = num;
}

if (num < lowest ) {
lowest = num;
}

cout << "Enter a number: ";
cin >> num;
i++;
}
``````

Outside the loop

• The value stored in "num" is compared to the value stored in highest. If the value is greater than the value stored in highest, highest therefore gets the value stored in num.

• This is repeated because once the loop ends, if this condition was not present the second highest number would be stored in "num" and not the first highest.

• A message is printed indicating that the program has ended. Another message is printed indicating the highest and lowest numbers from the series.

``````    cout << "The program has ended" << endl;
cout << "The highest number from the series of numbers is: " << highest << endl;
cout << "The lowest number from the series of numbers is: " << lowest;
``````

Full Code

``````
#include <iostream>
using namespace std;

int main() {

int i;
int N;
int highest;
int lowest;
int num;

i = 1;

cout << "Enter the amount of numbers in the series: " ;
cin >> N;

cout << "Enter a number: ";
cin >> num;
highest =  num;
lowest = num;

while ( i < N ){

if (num > highest ) {
highest = num;
}

if (num < lowest ) {
lowest = num;
}

cout << "Enter a number: ";
cin >> num;
i++;
}
if (num > highest ) {
highest = num;
}

cout << "The program has ended" << endl;
cout << "The highest number from the series of numbers is: " << highest << endl;
cout << "The lowest number from the series of numbers is: " << lowest;

}
``````

Conclusion

That's it, thank you for reading along I hoped there were some takeaways from this post and that it was a good read.