# Algorithm To Find The Minimum Value

Here is a basic algorithm for finding the lowest value of a number in a data set.

```//Full Working Program
#include <iostream>

using namespace std;

int main()
{
int k,min; //delcare a variable 'k' that holds integer values
int my_array ;//an array that will hold 10 integers

for (k=0; k<10; k++)
{
cout<<"Enter an integer"<<endl;
cin>>my_array[k];//user will input ten values
}

min=my_array;//assign min a value to avoid garbage

for (k=0; k<10; k++)
{
//if 'min' is more than my_array[k] then assign it that value
if (min > my_array[k])
{
min=my_array[k];
}
}

cout<<"The lowest value is "<<min<<endl;

return 0;
}
```

## -HOW IT WORKS-

The first loop is strictly just to recieve data from the user.

```for (k=0; k<10; k++)
{
cout<<"Enter an integer"<<endl;
cin>>my_array[k];//user will input ten values
}
```

When the loop first starts 'k' is 0 so when the cin statement executes it's like coding "cin>>ages>>endl;" 'k' then increments by one and 'k' now equals 1 so now the cin statement looks like this "cin>>ages>>endl;"...etc

```min=my_array;//assign min a value to avoid garbage
```

Here is a part that seems weird at first but it's absolutely necessary! Our variable that is going to hold the lowest value is 'min', in this specific loop it is going to be compared to 10 values to check whether or not they are lower than 'min' well if we don't assign min a value it will hold garbage. Remember garbage can be any number typically a ridiculous one. So say we didn't assign 'min' a value and allowed it to hold garbage and say in this case the garbage value was -2,250. Now say that when the user is finished inputing all 10 values that the lowest value they entered in 'my_array' is 12. When the if condition checks to see if 'min' is more than 'my_array[k]' it will never happen. The lowest value in 'my_array' is 12 but 'min' has the value of -2,250, in what world is -2,250 more than 12? Not this one. So if we assign 'min' the value of the first array 'my_array' like we did above, we are ensuring that 'min' will never be lower than the lowest value inside the data set in this case 'my_array'.

```for (k=0; k<10; k++)
{
//if 'min' is more than my_array[k] then assign it that value
if (min > my_array[k])
{
min=my_array[k];
}
}
```

The next for loop is what actually cotains the algorithm used with an if condition. In this case it will run 10 times each loop checking to see if 'min' is more than 'my_array[k]' if it is more than, then 'min' will be assigned the value of 'my_array[k]' if it is not then nothing will happen. After the loop has ran 10 times and checked all 10 values the variable'min' is left with the lowest value of the data set.