简体   繁体   中英

How can I use a condition result to tell the program which 2D array to address to?

excuse my english

How can I use a condition result to tell the program which array to refer to within a function ?

I would like to take just one certain array at a time, to match its values with a main array, and so far I cannot manage to come up with an idea on how to do such stuff.

example: if condition result came up "0", refer to the first array (playerA), if it came up "1" refer the other one (playerB), and so on. the program will match the CORRECT array values of the corresponding player with the array of "data" which is the main one.

I would have just used switch and let it go, but I try to avoid it as I will have alot of code to duplicate in each case, and alot of cases, so this is silly.

For all it matters, "players" arrays are all 2x2, and will be matched against 2x2 of data array (it's actually has more rows, but i choose specific 2x2 in it to match). and i need all 4 values in the 2x2 to be matched against the 4 corresponding values of given data-array 2x2

How do i do something like this without a switch or a temporary array inside the funcion ? A buddy of mine told me I should use a pointer but couldn't show me an example, so I looked it up here and I just don't get it.

I'm a beginner at this as you might have guessed. keep this in mind please.

and thnx in advance

.

int data[2][2] = {{5,5},{5,5}} ;

int playerA[2][2] = {{5,6},{7,8}} ;
int playerB[2][2] = {{1,2},{3,4}} ;

main () {

function(player) ;

}

void function (int activePlayer) {

   // this condition is just so you'd understand what i want
   // because i dont know how to code this part  
    if (activePlayer == 0)
       { pickTheRightArray = playerA } ;
    else if (activePlayer == 1)
       { pickTheRightArray = playerB } ;

    bigger = 0;
    for (x=0 ; x<number ; x++)  {
        if ( pickTheRightArray[x][0] > data [x][0] )
            bigger++;
    };
//etc..etc..etc... do some stuff with this info

There are a number of ways to handle a situation such as this, others have mentioned using a 3-D array. This also looks like something that would lend itself to a more modular style of coding:

int data[2][2] = {{5,5},{5,5}} ;

int playerA[2][2] = {{5,6},{7,8}} ;
int playerB[2][2] = {{1,2},{3,4}} ;

void function (int activePlayer);

main () {
   int player = 0;
   function(player) ;
}

void processPlayer(int player[2][2])
{
    int number = 2;
    int bigger = 0;
    for (int x=0 ; x<number ; x++)  {
        if ( player[x][0] > data [x][0] )
            bigger++;
    }
}

void function (int activePlayer) {
   // this condition is just so you'd understand what i want
   // because i dont know how to code this part  

    if (activePlayer == 0)
       { processPlayer(playerA); }
    else if (activePlayer == 1)
       { processPlayer(playerB); }
}

Being able to assign a pointer to a static multi-dimensional array is going to cause you heartache down the road. I would encourage you to avoid the use of globals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM