Thread: Finding the Minimum using a function

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    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. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.
    Last edited by Memloop; 02-28-2010 at 12:07 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    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. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by rodrigorules View Post
    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...
    you should start with INT_MAX (or with the first entered number)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or rather, you should write down your algorithm on paper and analyze it. See if you can spot a problem in it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    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. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Try printing find_min instead of new_min.

  8. #8
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    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;
    }
    I am new to C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM