Question: Hi, I was wondering if could get some help with this code I can\'t und
ID: 3749531 • Letter: Q
Question
Question: Hi, I was wondering if could get some help with this code I can't understand the practice exam.
the download is: https://drive.google.com/file/d/1H3__dN24HeqkiaWotRMfRTNR9QSpyJhL/view
The code for p2.cpp
/**
* @file p2.cpp
*
* CS 150 PE02: INTERMEDIATE SELECTION/LOGIC
*
* You have been dealt two cards, a and b.
* Each card has a value 0-100. Set the variable
* nearest to whichever value is nearest to 21
* without going over.
* Do not use any library functions.
*/
int nearest(int a, int b)
{
int result;
// Your code goes here
return result;
}
////////////////// STUDENT TESTS ///////////////////////
#include
using namespace std;
void studentTests()
{
cout << "Add your own code here" << endl;
}
2 @file p2.cpp 4 * CS 150 PE02: INTERMEDIATE SELECTION/ LOGIC 6 You have been dealt two cards, a and b. 7 Each card has a value 0-100. Set the variable 8 nearest to whichever value is nearest to 21 9 without going over. e Do not use any library functions. 2 int nearest(int a, int b) 13 4 int result; 6 I Your code goes here 8 return result; 9 2 #include iostream» 3 using namespace std; 4 void studentTests) 25 6 coutExplanation / Answer
#include <iostream>
using namespace std;
int nearest(int a, int b)
{
int result;
int temp1, temp2;
temp1 = (a > 21) ? a - 21 : 21 - a;
temp2 = (b > 21) ? b - 21 : 21 - b;
result = (temp1 > temp2) ? b : a;
//cout << "temp1 = " << temp1 << " temp2 = " << temp2 << " result = " << result << endl;
return result;
}
void studentTests()
{
cout << "Add your own code here" << endl;
int result, a, b;
cout << "Enter the values of Card A and Card B" << endl;
cin >> a >> b;
result = nearest(a, b);
cout << "The number nearest to 21 is " << result << endl;
}
int main ()
{
studentTests();
}
/************* OUTPUT OF PROGRAM **************
>> ./a.out
Add your own code here
Enter the values of Card A and Card B
30 16
The number nearest to 21 is 16
>> ./a.out
Add your own code here
Enter the values of Card A and Card B
5 4
The number nearest to 21 is 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.