# Finding the Minimum using a function

• 02-28-2010
BB89
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; }```

Quote:

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.
• 02-28-2010
Memloop
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.
• 02-28-2010
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; }```
• 02-28-2010
vart
Quote:

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...
• 02-28-2010
Elysia
Or rather, you should write down your algorithm on paper and analyze it. See if you can spot a problem in it.
• 02-28-2010
BB89
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
• 02-28-2010
Memloop
Try printing find_min instead of new_min.
• 02-28-2010
BB89
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; }```