Search code examples
carrayspoker

c function to compare highest values of two different arrays (applied to tiebreaking highcard hands in poker)


#include <stdio.h>

int highcard (int array[])
    {
        int i, j;
        int largest = array[0];
        for (i=0; i<5; i++)
            {
                if (largest < array[i])
                    {
                        largest = array[i];
                    }
            }

        printf("largest element is %d", largest);
        return largest;
    }




int main()
    {
        int hand1[5]={8, 10, 6 , 4, 2};
        int hand2[5]={13, 5, 3, 9, 12};
        int largest;

        highcard(hand1[5]);
        int largest1 = largest;
        return largest1;

        highcard(hand2[5]);
        int largest2 = largest;
        return largest2;

        if (largest1 < largest2)
            {
               printf("winner: hand2");
            }
        else
            {
                printf("winner: hand1");
            }
        return 0;
    }

I am trying to develop a program that tiebreaks two highcard hands in poker. For this I "translate" the card values to numbers (2=2 ... A=14), and for this specific case the maximum array size will always be 5 (5 cards). My problem is how do I compare the return values of the same function when applied to two different arrays?

What I mean is:

  • I have hand(array)1
  • I have hand2
  • I determine the largest number of hand1 and return it
  • I do the same thing for hand2
  • In main(), I want to compare the return (largest number) of highcard applied to hand1 and then hand2

But how do I specify in main() the specific return value of function highcard when applied to hand1 and then hand2 and keeping both returns separated?

This is what I have tried so far.

P.S.: This is for a university/college project, I have only started programming this semester, so any detailed explanation of my mistakes/errors and how to correct them would be very much appreciated :)


Solution

  • You have several misundertstandings.

    highcard(hand1[5]);
    

    Here, you want to call the function highcard and pass in the 5-element array hand1. To do that, do not specify the dimension in brackets. That works only when you declare the array. What happens instead is that you pass the sixth element of hand1 -- the first element has index 0 -- to the function. That element does not exist. You should also get a warning, because an integer is not an array; the argument types don't match.

    return largest1;
    

    Here, you probably want to store something in largest1, but what you actually do is to return from the function with the given value. That means you leave the current function. Leaving main means that you stop the program. You don't need a return here, just store the value.

    int largest1 = largest;
    

    Here, you try to access the variable largest from highcard, but thzat variable is local highcard and therefore is not visible to your main function. Rhe largest you look at is just an unrelated, uninitialized variable.

    You can access the value that is returned from a function by assigning the result of a function call like so. Your main function should look like this:

    int main(void)
    {
        int hand1[5] = {8, 10, 6 , 4, 2};
        int hand2[5] = {13, 5, 3, 9, 12};
    
        int largest1 = highcard(hand1);
        int largest2 = highcard(hand2);
    
        if (largest1 < largest2) {
            printf("winner: hand2");
        } else {
            printf("winner: hand1");
        }
    
        return 0;
    }