Thread: three numbers in descending order

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    three numbers in descending order

    Hi

    The following code is incomplete.

    1: I'm getting this error: undefined reference to `swapthree(float&, float&, float&)'| . I have been unable to understand the reason for this error and don't know what to fix. Please help me

    2: Could you please tell me some general thumb rule to know how many if's, else if's I would need in such situation? Thanks.


    Code:
    // swap3numbers.cpp
    // swap three number so that they appear in descending order
    // it is assumed entered numbers are distinct
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int swapthree(float& dummya, float& dummyb, float& dummyc);
    
    int main()
    {
        float a, b, c;
    
        cout << "enter a: "; cin >> a;
        cout << "enter b: "; cin >> b;
        cout << "enter c: "; cin >> c;
    
        if ( swapthree(a, b, c) )
    
            {
            cout << "the entered numbers in descending order: " << a << " " << b
        << " " << c << endl;
            }
    
        else
        {
            cout << "some error\n";
        }
    
        system("pause");
    
        return 0;
    }
    
    //-----------------------------------------------------------------
    // swapthree() definition
    
    int swapthree(float& dummya, float dummyb, float dummyc)
    {
    
        if (dummya > dummyb > dummyc)
        {
            return 1;
        }
    
        else if (dummya > dummyb && dummyb < dummyc)
        {
            float temp = dummyb;
            dummyb = dummyc;
            dummyc = temp;
    
            return 1;
        }
    
        else if (dummyb > dummya > dummyc)
        {
            float temp = dummya;
            dummya = dummyb;
            dummyb = temp;
    
            return 1;
        }
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at your function prototype and function definition are they identical? The prototype, definition, and function call must all agree on the type and number of parameters.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Sorry, I don't know how I missed to see the obvious mistake! Thanks for pointing this out.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    Do you think the following code rightly address the problem of arranging the three entered numbers in descending order? Please let me know. Thanks.

    Code:
    // swap3numbers.cpp
    // swap three number so that they appear in descending order
    // it is assumed entered numbers are distinct
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int swapthree(float& dummya, float& dummyb, float& dummyc);
    
    int main()
    {
        float a, b, c;
    
        cout << "enter a: "; cin >> a;
        cout << "enter b: "; cin >> b;
        cout << "enter c: "; cin >> c;
    
        if ( swapthree(a, b, c) )
    
            {
            cout << "the entered numbers in descending order: " << a << " " << b
        << " " << c << endl;
            }
    
        else if ( !swapthree(a, b, c) )
        {
            cout << "some error\n";
        }
    
        system("pause");
    
        return 0;
    }
    
    //-----------------------------------------------------------------
    // swapthree() definition
    
    int swapthree(float& dummya, float& dummyb, float& dummyc)
    {
    
        if ( (dummya > dummyb) && (dummyb > dummyc) )
        {
            return 1;
        }
    
    
        else if ( (dummya > dummyc) && (dummyc > dummyb) )
    
        {
            float temp = dummyb;
            dummyb = dummyc;
            dummyc = temp;
    
            return 1;
        }
    
        else if ( (dummyb > dummya) && (dummya > dummyc) )
        {
            float temp = dummya;
            dummya = dummyb;
            dummyb = temp;
    
            return 1;
        }
    
    
        else if ( (dummyb > dummyc) && (dummyc > dummya) )
        {
            float temp = dummya;
            dummya = dummyb;
            dummyb = dummyc;
            dummyc = temp;
    
            return 1;
        }
    
    
        else if ( (dummyc > dummya) && (dummya > dummyb) )
        {
            float temp = dummya;
            dummya = dummyc;
            dummyc = dummyb;
            dummyb = temp;
    
            return 1;
        }
    
    
        else if ( (dummyc > dummyb) && (dummyb > dummya) )
        {
            float temp = dummyc;
            dummyc = dummya;
            dummya = temp;
    
            return 1;
        }
    
    
    	else
    	   {
    	       return 0;
    	   }
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. ascending and descending order
    By ssk in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 08:03 PM
  3. bubble sort array in acending and descending order
    By kelvin2355 in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2010, 12:23 PM
  4. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM
  5. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM