Ask Question

Assume that a function named swapdoubles has been defined and is available for use in this exercise: that function receives two variables of type double and exchanges their values. Write the definition of a function named sort3 that is passed three double variables. The function returns nothing but modifies the values of these variables so they are in sorted order. So, if a, b and c have (respectively) the values 3.14, 2.71, and 3.04, and the invocation sort3 (a, b, c) is made, then upon return, the values of a, b and c will be 2.71, 3.04, and 3.14 respectively.

+4
Answers (1)
  1. 29 March, 09:46
    0
    C+ + Code:

    void sort3 (double &a, double &b, double &c)

    {

    if (a > b)

    swapdoubles (a, b);

    if (b > c)

    swapdoubles (b, c);

    if (a > b)

    swapdoubles (a, b);

    }

    Explanation:

    To change the values of a, b, c within the function, we pass the values by reference. Let us assume that number a = 3.14, b = 2.71, c = 3.04. Since a > b, values of a and b will be swapped. Now a = 2.71 and b = 3.14. Similariy, since b > c, they will be swapped. This way, we move the largest number to its correct position in the first two steps. If there are only three numbers, and the largest number is in its correct position, then for the two remaining numbers, we will only need atmost one swap to exchange their positions. hence, we perform a comparison of a > b once again to see if the b is smaller than a. if its not, then all a, b, c are in sorted order.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Assume that a function named swapdoubles has been defined and is available for use in this exercise: that function receives two variables ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers