Write a C++ program that takes 2 numbers of int type from the user and determine
ID: 3568638 • Letter: W
Question
Write a C++ program that takes 2 numbers of int type from the user and determines the
larger one between them using the pointer type shown in class. The two numbers MUST be
passed to a function in the form of pointer type to determine the larger one. The following shows
a sample output of the program.
Enter two numbers: 38 -61
The larger one between 38 and -61 is 38
Your program should get two numbers from the keyboard in the main() function. It then
passes the two numbers to a function to determine and return the larger one. You MUST use the
pointer type shown in class to declare the parameters for the two numbers in the function
prototype and heading.
Explanation / Answer
#include <iostream>
using namespace std;
int* compare(int *x,int *y){
if (*x > *y){
return x;
}
else{
return y;
}
}
int main() {
cout << "Enter Two Numbers " << endl;
int x, y;
cin >> x >> y;
int *res = compare(&x,&y);
cout << "larger number is " << *res << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.