Average of three biggest parameters out of five without using sorting algorithm or vector/array I'm trying to create a function with has 5 input parameters and returns the average of the 3 biggest. However I'm not supposed to use a sorting algorithm, vector/arrays or function libraries and still try to develop the most efficient logic possible.

So far I've come up with the code below. Is it the most efficient possible considering the restrictions?

float avg3of5(float a, float b, float c, float d, float e) {                                          
    float min;
    float min2;

    min = a;
    if (b &lt; min) {
        min2 = min;
        min = b;
    }
    else {
    min2 = b;
    }

    if (c &lt; min) {
        min2 = min;
        min = c;
    }
    else {
        if (c &lt; min2) {
            min2 = c;
        }
    }

    if (d &lt; min) {
        min2 = min;
        min = d;
    }
    else {
        if (d &lt; min2) {
            min2 = d;
        }
    }

    if (e &lt; min) {
        min2 = min;
        min = e;
    }
    else {
        if (e &lt; min2) {
            min2 = e;
        }
    }

    Return = (a + b + c + d + e - min - min2) / 3
}
I would think, that you should try to divide and conquer rather than writing one long function. That means you should split your code into separable and reusable functions that do one thing. For example the individual minima you get by the following functions

float min2(const float&amp; a, const float&amp; b) {
    return a &lt; b ? a : b;
}

float min3(const float&amp; a, const float&amp; b, const float&amp; c) {
    const float temp = min2(a, b);
    return temp &lt; c ? temp : c;
}

float min4(const float&amp; a, const float&amp; b, const float&amp; c, const float&amp; d) {
    const float temp = min3(a, b, c);
    return temp &lt; d ? temp : d;
}   

float min5(const float&amp; a, const float&amp; b, const float&amp; c, const float&amp; d, const float&amp; e) {
    const float temp = min4(a, b, c, d);
    return temp &lt; e ? temp : e;
}


You might even put this into variadic templates and overload min.
Now you can get the minimum value and define a switch statement

float average3of3(const float&amp; a, const float&amp; b, const float&amp; c) {
    return (a + b + c)/3;
}

float average3of4(const float&amp; a, const float&amp; b, const float&amp; c, const float&amp; d) {
    const float min = min4(a, b, c, d);
    switch(min) {
        case a:
            return average(b, c, d);
        case b:
            return average(a, c, d);
        case c:
            return average(a, b, d);
        case d:
            return average(a, b, c);
    }
}

float average3of5(const float&amp; a, const float&amp; b, const float&amp; c, const float&amp; d, , const float&amp; e) {
    const float min = min5(a, b, c, d, e);
    switch(min) {
        case a:
            return average3of4(b, c, d, e);
        case b:
            return average3of4(a, c, d, e);
        case c:
            return average3of4(a, b, d, e);
        case d:
            return average3of4(a, b, c, e);
        case e:
            return average3of4(a, b, c, d);
    }
}