# Thread: Finding the Minimum using a function

1. ## Finding the Minimum using a function

I need some help using functions. I wrote this code as if it was a regular program.

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

int main()
{

int num, count = 0, flag = 10, MIN = 0;

while( count < flag )
{
cout << "Enter a number: " << endl;
cin >> num;

if( num < MIN )

MIN = num;

count++;
}

cout << "The smallest number is " << MIN << endl;

}```

Write a program that will ask for the 10 numbers. The program will use a function to find the minimum number.

This is what I am supposed to do. Not sure how to start doing it.

2. Create a function called "min" or something like that, that takes two numbers as parameters. Then have it return the smallest number of the two parameters.

Use a loop that gets input and compares the input with the currently stored minimum (use the min function for this). Exit the loop when you have compared ten numbers.

3. Code:
```int num,MIN=0;
for(int count=0; count < 10; count++)
{
cout << "Enter a number: " << endl;
cin >> num;

if( num < MIN )

MIN = num;
}```

4. Originally Posted by rodrigorules
Code:
```int num,MIN=0;
for(int count=0; count < 10; count++)
{
cout << "Enter a number: " << endl;
cin >> num;

if( num < MIN )

MIN = num;
}```
if you initialize MIN to 0 - you never got the positive result...

5. Or rather, you should write down your algorithm on paper and analyze it. See if you can spot a problem in it.

6. I think I might be on the right track.

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

int min(int find);

int main()
{
int find_min;
int new_min;

cout << "Enter a number: " << endl;
cin >> find_min;

find_min = min(new_min);

cout << "The smallest number is " << new_min << endl;

}
int min(int find)
{
int num, count = 0, flag = 10, MIN = 0;

while( count < flag )
{
cout << "Enter a number: " << endl;
cin >> num;

if( num < MIN )

MIN = num;

count++;
}

return MIN;
}```
The only problem I am having is when it prints out the answer it says "The smallest number is 2147332096

7. Try printing find_min instead of new_min.

8. It works but am I supposed to have two
Code:
```cout << "Enter a number: " << endl;
cin >> find_min;```
and
Code:
```    cout << "Enter a number: " << endl;
cin >> num;```

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

int min(int find);

int main()
{
int find_min;
int new_min;

cout << "Enter a number: " << endl;
cin >> find_min;

find_min = min(new_min);

cout << "The smallest number is " << find_min << endl;

}
int min(int find)
{
int num, count = 0, flag = 10, MIN = 0;

while( count < flag )
{
cout << "Enter a number: " << endl;
cin >> num;

if( num < MIN )

MIN = num;

count++;
}

return MIN;
}```