1

I have to write a function that will reorder three variables from lowest to highest. The function name is void reorder and i was curious if there was anyway that i could call my max and min functions inside of this function in order to compress my code or if there is a way I write it not using IF statements. I have started it and I want to know what to do before i continue. Thank you

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

/*
 * 
 */

int min3int(int a, int b, int c)
// function returns the minimum of 3 integer inputs
//@
//@
{
    if(a<b)
    {
        if(a<c)
        {
             return a;
        }
    }
    else if(b<a)
    {
        if(b<c)
        {
            return b;
        }
    }
    else if (c<a)
    {
        if(c<b)
        {
            return c;
        }
    }
}
int max3int(int a, int b, int c)
// function returns the maximum of 3 integer inputs
//@
//@
{
    if(a>b)
    {
        if(a>c)
        {
             return a;
        }
    }
    else if(b>a)
    {
        if(b>c)
        {
            return b;
        }
    }
    else if (c>a)
    {
        if(c>b)
        {
            return c;
        }
    }
}
void reorder(int min,int med,int max);
// function reorders the 3 reference parameters so that they are in ascending order
//@
//@
int temp;

if(min<med)
{
    if(med<max)
    {
        if(min<max)
        {
             min=min;
             med=med;
             max=max;
        }
        else if(med<min)
        {
            if (med<max)
            {
                if (max<)
            }
        }

    }
}

bool isFactor(int num1,int num2);
// function checks if the first int is a factor of the second int.

int main(int argc, char** argv) {

        int dividend;
    int factor1, factor2, factor3;

    cout << "Enter the number to be divided: ";
    cin >> dividend;

    cout << endl << "Enter the first possible factor: ";
    cin >> factor1;

    cout << endl << "Enter the second possible factor: ";
    cin >> factor2;

    cout << endl << "Enter the third possible factor: ";
    cin >> factor3;

    cout << endl << "Possibilites entered are in the range of " << min3int(factor1,factor2,factor3)
         << " to " << max3int(factor1,factor2,factor3) << "." << endl;

    reorder(factor1,factor2,factor3);

    cout << endl << "The following numbers were determined to be factors of " << dividend << ": ";
    if(isFactor(factor1,dividend))
        cout << factor1 << " ";
    if(isFactor(factor2, dividend))
        cout << factor2 << " ";
    if(isFactor(factor3, dividend))
        cout << factor3;

    cout << endl << endl;


    return 0;
}
Lucas Brawdy
  • 43
  • 2
  • 8

2 Answers2

1

I'd put the three values in a vector and call the sort algorithm. This gives a neat an clean function with no if-statements

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void reorder(int &min, int &med, int &max)
{
    vector<int> vec;
    vec.push_back(min);
    vec.push_back(med);
    vec.push_back(max);

    sort(vec.begin(), vec.end());

    min = vec[0];
    med = vec[1];
    max = vec[2];
}

int main()
{
    int min = 1, med = 1, max = 3;
    cout << min << " " << med << " " << max << endl;
    reorder(min, med, max);
    cout << min << " " << med << " " << max << endl;
    system("pause");
    return 0;
}
Stanton
  • 904
  • 10
  • 25
0

I don't see an easy way to sort using your min3int() or max3int() since they don't tell you anything about the order of the other variables.

I suggest something simple to write and understand such as:

void reorder(int &a, int &b, int &c)
{
    if (b < a) std::swap(b, a);
    if (c < a) std::swap(c, a);
    if (c < b) std::swap(c, b);
}

It's possible to compare values without if but I prefer clear code unless measurement shows a need for heroic optimization. It's also possible to sort three variables with only two swaps (see this question).

Community
  • 1
  • 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70