"roll the dice" challenge C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

"roll the dice" challenge C++

Hi there smart people! So I have this practice challenge and I'm lost on the return part (and some other parts as well lol). What exactly do I have to return and should I use the "if else" sequence in the main function? Or what? I don't get it plz send help >.< *** While playing a board game, you and your friend roll the dice and the person who rolled the higher number moves forward. Write a function that takes two numbers as arguments and returns the higher number of the two. Print the result. If numbers are equal function must return that number. *** #include <iostream> using namespace std; int max(int num1, int num2) { //complete the function } int main() { //getting inputs int first; cin >> first; int second; cin >> second; //call the function and print result return 0; } https://www.sololearn.com/learning/1051/1637/2903/1

28th Jan 2021, 4:35 PM
Valerie S
Valerie S - avatar
6 Answers
+ 6
Returning a value means returning it to the caller, i.e. the returned value becomes the value of the function call in its context. That is what makes something like int foo() { return 1; } ... int i = foo(); // i = 1 possible. A function to return the greater of two values is only slightly more complex. Since you have to make a decision, you need a conditional, just as you said, but in the function, not in main(). Here is an example using the ternary operator (which is nothing but syntactical sugar for an if-else statement): int max( int n1, int n2 ) { return ( n1 < n2 ) ? n2 : n1; } This translates to: If n1 is smaller than n2, return n2, else n1. All that is left is to print the value returned by max(), i.e cout << max( first, second ); Again, this works because max() returns its value, which is then inserted into the call to std::cout.
28th Jan 2021, 5:08 PM
Shadow
Shadow - avatar
+ 2
#include <iostream> using namespace std; int max(int num1, int num2) { //complete the function return (num1<num2)?num2:num1; } int main() { //getting inputs int first; cin >> first; int second; cin >> second; //call the function and print result cout << max(first,second) << endl; return 0; }
8th Feb 2021, 10:23 PM
Omid Bafkar
Omid Bafkar - avatar
+ 2
#include <iostream> using namespace std; int max(int num1, int num2){ //complete the function if (num1 > num2){ return num 1; } else if (num1 < num2){ return num2; } else { return num1 } } int main(){ //getting inputs int first; cin >> first; int second; cin >> second; //call the function and print the result cout << max(first,second) <<endl; return 0; }
6th Jul 2021, 9:52 AM
Agon Istrefi
0
Thank you so much, didn't know about the ternary operator or maybe didn't get there yet.
28th Jan 2021, 5:19 PM
Valerie S
Valerie S - avatar
0
#include <iostream> using namespace std; int max(int num1, int num2) { //complete the function return (num1<num2)?num2:num1; } int main() { //getting inputs int first; cin >> first; int second; cin >> second; //call the function and print result cout << max(first,second) << endl; return 0; } Good Luck
25th Jan 2022, 5:05 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
0
#include <iostream> using namespace std; int maxd(int a, int b){ return (a>b ? a:b); } int main() { int f, s; cin >> f >> s; cout << maxd(f, s); return 0; }
26th Aug 2022, 8:51 AM
the Monu
the Monu - avatar