Knowee
Questions
Features
Study Tools

Write a function void sort2(int& a, int& b) that swaps the values of a and b if a isgreater than b and otherwise leaves a and b unchanged. For example,int u = 2;int v = 3;int w = 4;int x = 1;sort2(u, v); // u is still 2, v is still 3sort2(w, x); // w is now 1, x is now 4

Question

Write a function void sort2(int& a, int& b) that swaps the values of a and b if a isgreater than b and otherwise leaves a and b unchanged. For example,int u = 2;int v = 3;int w = 4;int x = 1;sort2(u, v); // u is still 2, v is still 3sort2(w, x); // w is now 1, x is now 4

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the function written in C++:

void sort2(int& a, int& b) {
    if (a > b) {
        int temp = a;
        a = b;
        b = temp;
    }
}

This function takes two integers by reference, a and b. If a is greater than b, it swaps their values. If a is not greater than b, it leaves them unchanged. The swapping is done by using a temporary variable temp to hold the value of a before assigning b's value to a, and then assigning temp's value to b.

This problem has been solved

Similar Questions

Write a function void sort2(int& a, int& b) that swaps the values of a and b if a isgreater than b and otherwise leaves a and b unchanged. For example,int u = 2;int v = 3;int w = 4;int x = 1;sort2(u, v); // u is still 2, v is still 3sort2(w, x); // w is now 1, x is now 4

Write a function void sort2(double* p, double* q) that receives two pointers and sorts the values to which they point. If you call sort2(&x, &y) then x <= y after the call.

Write a function sort3(int& a, int& b, int& c) that swaps its three arguments toarrange them in sorted order. For example,int v = 3;int w = 4;int x = 1;sort3(v, w, x)

Write a functional program that sorts 10 input integers (signed) using either bubble sort orselection sort algorithm. The output window must show both unsorted array of integers andsorted array of integers.

The basic operation in sorting problem is

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.