C++ Write and test a function void sort2num(int *p1, int *p2), the result is tha
ID: 3844409 • Letter: C
Question
C++ Write and test a function void sort2num(int *p1, int *p2), the result is that p1 points to the larger number of the two, and p2 points to the smaller number of the two. C++ Write and test a function void sort2num(int *p1, int *p2), the result is that p1 points to the larger number of the two, and p2 points to the smaller number of the two. C++ Write and test a function void sort2num(int *p1, int *p2), the result is that p1 points to the larger number of the two, and p2 points to the smaller number of the two.Explanation / Answer
below is the c++ code, which sorts the numbers by pointing larger number to p1 and smaller number to p2, it swaps the value.
COde:
#include <iostream>
using namespace std;
void sort2num(int *p1,int *p2)
{ // below is swap code
if (*p1 < *p2){
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
}
int main() {
// your code goes here
int x = 5;
int y = 10;
int *p1 = &x;
int *p2 = &y;
cout << "before" << " ";
cout << "p1:" << *p1 <<" ";
cout << "p2:" << *p2 << " ";
sort2num(p1,p2);
cout << "after" << " ";
cout << "p1:" << *p1 <<" ";
cout << "p2:" << *p2 << " ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.