how to compare two max numbers(max, maxa)

Hi i am a beginner in c++ and i have trouble with comparing two of the biggest numbers out of four numbers. Im working on an assigment with a dice game where i need to tell the computer the following;

"if the biggest number out of dice_three and dice_four is the same number as the biggest of dice_two and dice_one, then loop1=true"

this is how i have been writing it so far ;


if

(MAXA(dice_three,dice_four) == MAX(dice_two,dice_one))

{

lopp 1=true;

}


(as seen) i am using:

#define MAX(dice_one,dice_two) ( (dice_one) > (dice_two) ? (dice_one) : (dice_two) )

#define MAXA(dice_three,dice_four) ( (dice_three) > (dice_four) ? (dice_three) : (dice_four) )

to calculate which one is the biggest number out of the two pares.


the code i have been writing so far seem to have the wrong syntax, anyone have an alternative right way to write this statement?

Thanks for help!!
Last edited on
defining macro's like that is horrible to be honest.

write a bubble sort:
http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm

then your two biggest numbers will be the first and second elements in your array.
A bubble sort seems a bit overkill when you can just do (example):
1
2
3
4
5
6
7
8
9
10
    if(dice[0] > dice[1])
        largest[0] = dice[0];
    else
        largest[0] = dice[1];
    if(dice[2] > dice[3])
        largest[1] = dice[2];
    else
        largest[1] = dice[3];
    if(largest[0] == largest[1])
        std::cout << "Loop is true:\n";
Last edited on
Your two macros can be replaced by one as they are identical apart from their names and parameter names.

So you could replace both with:

#define MAX(a,b) ((a) < (b) ? (b) : (a))

You could also use the max() contained in the algorithms of C++:
http://www.cplusplus.com/reference/algorithm/max/

1
2
3
4
if (max(dice_three,dice_four) == max(dice_two,dice_one))
  loop1=true;
else
  loop1=false;


or

 
loop1 = (max(dice_three,dice_four) == max(dice_two,dice_one)) ? true : false;

loop1 = (max(dice_three,dice_four) == max(dice_two,dice_one)) ? true : false;

is equivalent to:

loop1 = max(dice_three,dice_four) == max(dice_two,dice_one);
This is why you should use the code formatter :) The problem would have been instantly spotted in formatted code.

Your syntax is wrong not your logic. You used "lopp 1" when you meant to use "lopp1"

Listen to @mutexe, you shouldn't use these macros, I don't think bubble sort is overkill, but in any case, learning it is still a very good thing.

If you do choose to go the macro way, here are some tips:

You don't need 2 of them doing exactly the same thing. You could have done:

(MAX(dice_three,dice_four) == MAX(dice_two,dice_one))

You don't need "MAXA" at all. Also, use more generic names for the macro arguments so you don't tie them to your code. Instead of dice_one, dice_two, why not use (A and B ) for example?

As a final tip to a fellow beginner, learn the standard library, for example std::max does this comparison safely.

So, here're 2 versions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

#define MAX(A,B) ( (A) > (B) ? (A) : (B) )

int main()
{

int dice_one = 10, dice_two = 30, dice_three = 15, dice_four = 30;
bool lopp1 = false;
if 
(MAX(dice_three,dice_four) == MAX(dice_two,dice_one))

{

lopp1=true;

}

std::cout << std::boolalpha << lopp1 << std::endl;
return 0;
}


Or

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>

int main()
{
	int dice_one = 10, dice_two = 30, dice_three = 15, dice_four = 30;
	bool lopp1 = std::max(dice_three,dice_four) == std::max(dice_two,dice_one);
	std::cout << std::boolalpha << lopp1 << std::endl;
	return 0;
}
thanks all for the answers it helped me very much!
Topic archived. No new replies allowed.