Create a new project with one function. (You may create one or more helper funct
ID: 3559369 • Letter: C
Question
Create a new project with one function. (You may create one or more helper functions if you want, but it is not necessary.) Name the function putinOrder. Hie function takes two pointers to integers. In its implementation, the function checks the values of the two inputs and, if needed, rearranges them so that the first is the smaller and the second is larger. Use the main function below to call putlnOrder. In order to make this function work, it must take pointers to variables as input, and you must use pointers when passing the x andy variables in the call. Note that if the values are in the correct order, putinOrder should not change anything. However, if the functions are not in order, the putinOrder function should like a "swap" function. I recommend that you try to under stand how to implement this function by watching my April 3 video, and the Bucky Roberts videos on pointers and looking at the pointer chapter (10) in the book. If you are still confused, and only at that point, you may search the web for examples of a C + + swap function. Turn in just the cpp file. Remember to put a full header comment section at the beginning of the file, and use your name in the name of the cpp file (e.g. in my case vanliilstOrder.cpp).Explanation / Answer
#include<iostream>
using namespace std;
void putInOrder(int *a,int *b)
{
int t;
if(*a>*b)
{
t=*a;
*a=*b;
*b=t;
}
}
int main()
{
int x,y,*w,*l;
cin>>x>>y;
w=&x;
l=&y;
putInOrder(w,l);
cout<<x<<" "<<y;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.