简体   繁体   中英

Function + Reference Variables - Programming Assignment - C++

Been working on this all day and can't figure out what I'm missing. Did not want to resort to coming here for help but I have to.

The exercise is supposed to take three int parameters by reference and sort their values into ascending order, so that the first parameter has the lowest value, the second parameter the middle value, and the third parameter has the highest value.

The code compiles fine, but the numbers are not in order. It skips my function all together and just displays the variables straight from main.

#include <iostream>

using namespace std;

//Function prototype
void smallSort(int&, int&, int&);

//Main
int main()
{
    int a = 100, b = 25, c = 20;

    smallSort(a, b, c);

    cout << a << endl;
    cout << b << endl;
    cout << c << endl;

    return 0;
}

void smallSort(int &valu1, int &valu2, int &valu3)
{
    int tempVal1 = 0, tempVal2 = 0, tempVal3 = 0;

    if (valu1 > valu2 && valu1 > valu3)
    {
        tempVal1 = valu1;
        valu1 = tempVal1;
        if (valu2 > valu3)
        {
            tempVal2 = valu2;
            tempVal3 = valu3;
            valu2 = tempVal2;
            valu3 = tempVal3;
        }

        else if (valu3 > valu2)
        {
            tempVal2 = valu3;
            tempVal3 = valu2;
            valu2 = tempVal3;
            valu3 = tempVal2;
        }
    }

    else if (valu2 > valu1 && valu2 > valu3)
    {
        tempVal1 = valu2;
        valu2 = tempVal1;
        if (valu1 > valu3)
        {
            tempVal2 = valu1;
            tempVal3 = valu3;
            valu1 = tempVal2;
            valu3 = tempVal3;
        }

        else if (valu3 > valu1)
        {
            tempVal2 = valu3;
            tempVal3 = valu1;
            valu3 = tempVal2;
            valu1 = tempVal3;
        }

        else if (valu3 > valu1 && valu3 > valu2)
        {
            tempVal1 = valu3;
            valu3 = tempVal1;
            if (valu2 > valu1)
            {
                tempVal2 = valu2;
                tempVal3 = valu1;
                valu2 = tempVal2;
                valu1 = tempVal3;
            }

            else if (valu1 > valu2)
            {
                tempVal2 = valu1;
                tempVal3 = valu2;
                valu1 = tempVal2;
                valu2 = tempVal3;
            }

        }

    }
}

Your function is too compilcated. Also it is wrong because at least it does not take into account that variables can be equal each other.

It can look the following way as it is shown in this demonstrative program. It uses the bubble sort method .

#include <iostream>

void smallSort( int &valu1, int &valu2, int &valu3 )
{
    if ( valu2 < valu1 )
    {
        int tmp = valu1;
        valu1 = valu2;
        valu2 = tmp;
    }

    if ( valu3 < valu2 )
    {
        int tmp = valu2;
        valu2 = valu3;
        valu3 = tmp;
    }

    if ( valu2 < valu1 )
    {
        int tmp = valu1;
        valu1 = valu2;
        valu2 = tmp;
    }
}

int main()
{
    int a = 2, b = 3, c = 1;

    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;

    smallSort( a, b, c );

    std::cout << "a = " << a << ", b = " << b << ", c = " << c << std::endl;
}

The program output is

a = 2, b = 3, c = 1
a = 1, b = 2, c = 3

Also instead of code blocks like this

    if ( valu2 < valu1 )
    {
        int tmp = valu1;
        valu1 = valu2;
        valu2 = tmp;
    }

you could use standard function std::swap . For example

    if ( valu2 < valu1 ) std::swap( valu2, valu1 );

The function would look in this case like

#include <iostream>
#include <utility>

void smallSort( int &valu1, int &valu2, int &valu3 )
{
    if ( valu2 < valu1 ) std::swap( valu2, valu1 );

    if ( valu3 < valu2 ) std::swap( valu3, valu2 );

    if ( valu2 < valu1 ) std::swap( valu2, valu1 );
}

//...

I think its fair to say you have over complicated it a bit, but that's okay.

I think it might help you condense the code if you only compared two integers at a time, this way you don't have to hard-code in as much logic. I did a quick google search for a source code, and found this: https://codereview.stackexchange.com/questions/64758/sort-three-input-values-by-order Make sure to look at the comments on that post, because they should also help you.

Also, its incredibly easy to find source code with google, it'll help you out a lot, especially when you're still learning!

A C++11 alternative

#include <iostream>
#include <array>
#include <algorithm>
using namespace std;

int main() {
    array<int, 3> arr = { 100, 25, 20 };
    sort(arr.begin(), arr.end());
    for (int & i : arr) cout << i << endl;
}

Output

20
25
100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM