how to determine the largest of 4 ints?

Hello,
I need for my program to determine whitch one of four ints is the largest. I need the name, not the value. eg:
1
2
3
4
5
int a = rand()%9; int a1=1; // a factor corresponding to a, but it's not 
int b = rand()%9; int b1=3; // determined by relation
int c = rand()%9; int c1=2; //
int d = rand()%9; int d1=7; // all this can be easyly done as a matrix, if it helps
int target;


The best I can think of, is to code something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ( a > b )
  if ( a > c )
    if ( a > d ) // a is the largest
       target = a1;
    else // d is the largest
       target = d1;
  else
    {
    if ( c > d )
      ...
    else
      ...
    {
....

NOTE: It's not of distinct importance the case if two vars are equal.
Is there a easyer, more inteligible and not so error prone way to determine this?

Thank you for your help!
Using std::max():
1
2
3
int max_val = std::max(a, b);
max_val = std::max(max_val, c);
max_val = std::max(max_val, d);
as the amount of numbers you needed to evaluate grew this approach would result in an infeasible number of variable names.

Make a vector of ints and another int to store the index of the largest element. Set the 'highest' index to the first element then loop through the vector checking each element to see if it is higher than the previously stored highest.
Idk if there is a better way but you could condense it to this:
1
2
3
4
if(a > b && a > c && a > d)
       target = al;
if(b > a && b > c && b > d)
       target = bl;


and keep doing that for the other two variables.
Put the values in an array.

Then:
1
2
3
4
5
6
int largest = array[0];
for(int i = 0; i<ARRAY_SIZE; i++)
{
    if(array[i]>largest)
       largest=array[i];
}

There's always room for one more :) This is just Coder777's one, but on one line.

1
2
using std::max;
int largest = max(a,max(b,max(c,d)));
Last edited on
closed account (z05DSL3A)
the max of the rand vars is used to assign the corresponding int to target...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>

using namespace std;

typedef pair<int,int> intPair;

int main()
{
    std::vector<intPair> pairs;
    vector<intPair>::iterator it;

    pairs.push_back(intPair((rand()%9), 1));
    pairs.push_back(intPair((rand()%9), 3));
    pairs.push_back(intPair((rand()%9), 2));
    pairs.push_back(intPair((rand()%9), 7));

    for (it=pairs.begin(); it!=pairs.end(); ++it)
        cout << (*it).first << ":" << (*it).second << endl;
    cout << endl;

    sort(pairs.begin(),pairs.end());

    int target = pairs.back().second;


    for (it=pairs.begin(); it!=pairs.end(); ++it)
        cout << (*it).first << ":" << (*it).second << endl;
    cout << "target = " << target << endl;

    //.....
    return 0;
}


a bit of overkill never hurt anyone
Last edited on
Hello again,

Thank you all for your replyes, but I think I didn't explain my problem clearly.
I want to make a program inspired by a chess game with the main difference that I want the pieces to choose their next move.
this is part of the move() member of the rook class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
		// find direct targets
		int target_dw, target_up, target_le, target_ri; // this is suposed to be a list of targets in the four directions the rook can move.
		int row_dw = row, row_up= row, col_le = col, col_ri = col; // these are the coordinates where the rook will move
// row, col -- the rook's current position
// row_up, row_dw, col_le, col_ri -- the rook's possible   destinations
		//1. down
		while ( row_dw > 0 && row_dw < BOARDSIZE )
		{
			if ( mask1[row_dw+1][col] == 254 ) // unsigned char mask[][] is a representation of the board
                                                   // for the rook to evaluate it's circumstances
				row_dw = row_dw+1; // mask[x][y] == 254 means the square is empty and it should read the next square
			else if ( mask1[row_dw+1][col] < 254 ) // if mask[x][y] < 254, means that there is an oposing piece on that square
				target_dw = mask1[row_dw+1][col]; // and it should determine how valuable it is
			else if ( mask1[row_dw+1][col] == 255) // mask[x][y] == 255 means that there's a piece from the same army 
                                                                                               //on the square, and it should not pass
				target_dw = -1; 
		}
               //2. up
              while (row_up > 0 && row_up < BOARDSIZE)
              { ... target_up = the rank of the piece at coordinates row_up, col }
               //3. le
              while (col_le > 0 && col_le < BOARDSIZE)
              { ... target_le = the rank of the piece at coordinates row, col_le }
               //4. ri
              while (col_ri > 0 && col_ri < BOARDSIZE)
              { ... target_ri = the rank of the piece at coordinates row, col_ri }


Now I need to write the code to establish whitch one of the targets is more valuable, and based on that, to know what set of coordinates will be the destination for the movement:
1
2
3
4
target_up => attack(row_up, col);
target_down => attack(row_dw, col);
target_left => attack(row, col_le);
tartget_right => attack (row, col_ri);


So it doesn't suffice to know the numeric largest value of the four targets.
I know it's stupid, but I don't know how else to do this...
Hi,
Thank you all for your help.
In the end, I made a 2x4 matrix with the targets and ordinates/coordinates. So, if the index of the largest target is <=1, then movement should be on the same column. else movement should be on the row.
I am posting the sollution if it may be usefull for somebody.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
		int targets[2][4] = { {target_dw, target_up, target_le, target_ri},
								{ row_dw, row_up, col_le, col_ri }};
		int i, j, targ_max , i_tar_max;
		targ_max = -2;
		for ( i = 0; i < 4; i++)
		{
			for ( j = 0; j < 4; j++)
			{
				if ( targets[0][i] <= targets[0][j] )
				{
					targ_max = targets[0][j];
					i_tar_max = j;
				}
			}
		}

		if ( targ_max > -1 )
		{ // this is not finished
			if ( i_tar_max <=1 ) // then movement should be on the same column.
			{
				cout << "\n CRook::move()  on column ";
				board1.killpiece(rowd, col); // kill the oposing piece
				board1.liftpiece(row, col); // take this piece off the board
				board1.addpiece(rowd, col, (*this)); // place the piece in place of the attacked piece.
				return true;
			}
			else // movement should be on the same row.
			{
				cout << "\n CRook::move()  on row ";
				board1.killpiece(row, cold); // kill the oposing piece
				board1.liftpiece(row, col); // take this piece off the board
				board1.addpiece(row, cold, (*this)); // place the piece in place of the attacked piece.
				return true;
			}
		}
Topic archived. No new replies allowed.