Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a function called arrangeThree that swaps the values of three integers,

ID: 3665666 • Letter: 1

Question

1. Write a function called arrangeThree that swaps the values of three integers, given as three separate parameters. This function should not have any return value, but, like swap() in the lecture notes, moves the values from one place to another. When the algorithm returns, the smallest value should be in the first parameter, and the largest is in the last (according to the order of your parameter list).

For example, suppose you have three references whose values are in the following order: 3,2,5. When your function finishes, the references will have values in increasing order: 2,3,5. The following code might help explain it:

1

// in main() somewhere int a = 3;

int b = 2;

int c = 5;

cout << a << ’ ’ << b << ’ ’ << c << endl; // displays 3 2 5

arrangeThree(&a, &b, &c); // organize!

cout << a << ’ ’ << b << ’ ’ << c << endl; // displays 2 3 5

You may implement other functions to be used to solve this problem. Hint: You can use swap() to help here.

A variant of swap() that swaps conditionally might also be helpful. Note that your implementation should include an algorithm header (pre, post and return) and the function body. If you use other procedures or functions, they should all have complete headers and bodies.

Test this function and any others that you created thoroughly in main(). Note: Do not solve this problem using arrays. That’s not what you’re here to learn. Practice using references and pointers! Otherwise, this is just a useless exercise.

2.. Now implement a function called arrangeFour, that behaves like arrangeThree, except is has 4 reference parameters instead of three. For example, suppose you have three references whose values are in the following order: 3,2,5,1.

When your function finishes, the references will have values in increasing order: 1,2,3,5. You may use arrangeThree in your solution. In fact, that would be ideal.

Your problem is to figure out how to use it! Your main function only needs to contain testing for these two functions, and a couple (2) of test cases (like the above code). Every function that you write must be tested thoroughly! The testing output must be readable by a third party. As in A1, you should implement the solution one function at a time, and test each function separately. Do not go on to the next function until you’ve tested the current one thoroughly.

Testing must remain active while you are programming other functions, because you may create bugs that break functions you thought were working!

//Explaining step by step would be advisable since I am trying to understand. Thank you.

Explanation / Answer

#include<iostream>
using namespace std;
void arrangeThree(int *a,int *b,int *c);
int main(){
int a,b,c;
cout << "Enter value of a, b and c respectively: ";
cin >> a >> b >> c;
cout << "Value before swapping: " << endl;
cout << "a, b and c respectively are: " << a << ", " << b << ", " << c << endl;
arrangeThree(&a,&b,&c);
cout << "Value after swapping numbers in cycle: " << endl;
cout << "a, b and c respectively are: " << a << ", " << b << ", " << c << endl;
cout << "Ordered Values ";
if (a < b && b < c)
{
cout<<a<<b<<c;
}
else if (a > b && b > c)
{
cout<<c<<b<<a;
}

else if (a < b && b > c)
{
cout<<a<<c<<b;
}

}
void arrangeThree(int *a,int *b,int *c)

{
int temp;
temp=*b;
*b=*a;
*a=*c;
*c=temp;
}