Thread: find the smallest number:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    find the smallest number:

    My output is always 0 and when I debug it my function works so it has to be the print function.

    Another thing that is bugging me is that I was asked to use the function double smaller(double x, double y) but I had an issue with it. If I was using two numbers that would make sense but with 15 why?

    My const say 4 at the moment because I got tired of entering in 15 numbers to see if it was working.

    Code:
     #include <iostream>
    
    const int arraySize=4;
    using namespace std;
    
    void user_Prompt();
    int smaller(int []);
    void print_smallest();
    
    void user_Prompt()
    {
        int values[arraySize];
        
        for ( int i = 0; i < arraySize; i++ )
        {
            cout << "Enter value " << i << ": ";
            cin >> values[i];
        }
        smaller(values);
    }
    
    int smaller(int values[])
    {
        int small;
        small=values[0];
    
        for (int i = 0; i < arraySize; i++)
        {
            if(values[i]<small)
            {
                small=values[i];
            }
        }
        
        return small;
    }
    void print_smallest()
    {
        int num;
        int small=smaller(&num);
    
        cout << "The smallest number is "<< small<< endl;
    }
    
    int main ()
    {
        int num[arraySize];
        
        user_Prompt();
        smaller(num);
        print_smallest();
        
        
        
        
        return 0;
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    your print_smallest function passes the address of an uninitialised local integer 'num' to the function smallest().

    You Enter the function user_prompt(), and declare a LOCAL array called values - I dont think you are clear on scope or passing arrays or variables to functions.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    1. You should prefer std::vector over arrays.
    2. Even if you use arrays (e.g., because the teacher wants so), you should pass to the function not only a pointer to the array itself, but also the array's size (the number of its elements). Notice that currently the user may pass only up to 4 values.

    I examined your code and placed comments to give you some hints.

    Code:
    #include <iostream>
     
    const int arraySize=4;
    using namespace std;
     
    void user_Prompt();
    int smaller(int []); // let's ignore these ugly arrays for now...
    void print_smallest();
     
    void user_Prompt() // what does it really do?
    {
        int values[arraySize];
         
        for ( int i = 0; i < arraySize; i++ )
        {
            cout << "Enter value " << i << ": ";
            cin >> values[i];
        }
        smaller(values); // what is that call for?
    }
     
    int smaller(int values[])
    {
        int small;
        small=values[0]; // can we be sure that there is at least one number in the array?
     
        for (int i = 0; i < arraySize; i++) // could we start from 1?
        {
            if(values[i]<small)
            {
                small=values[i];
            }
        }
         
        return small;
    }
    void print_smallest()
    {
        int num;
        int small=smaller(&num); // what is passed here?
     
        cout << "The smallest number is "<< small<< endl;
    }
     
    int main ()
    {
        int num[arraySize];
    
        user_Prompt(); // what it returns and where is the result stored?
        smaller(num); // what is that call for?
        print_smallest();
    
        return 0;
    }

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    for (int i = 0; i < arraySize; i++) // could we start from 1?
    As far as that goes I would rather see this op init the int 'small' to 0 to get into a habit and then discard the pointless initialisation using the array element[0], then starting the loop from 0 to properly get the idea that arrays are indexed from 0. as I feel some trad fundamentals are required learning for the op
    Last edited by rogster001; 10-03-2013 at 01:58 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find smallest non-value in an 2-d array !! need help :D
    By newmem011 in forum C Programming
    Replies: 7
    Last Post: 07-10-2012, 10:37 PM
  2. How to find the smallest number. (Writing a subprogram)
    By november1992 in forum C Programming
    Replies: 13
    Last Post: 05-03-2012, 03:32 PM
  3. Replies: 6
    Last Post: 09-23-2010, 10:12 PM
  4. need to find smallest int
    By lankeveil in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 05:48 AM
  5. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM