trungnt
trungnt

Reputation: 1111

C++ function to find maximum element of 3 arrays

I'm currently learning arrays and how they work. I'm given the following function that is used to find the maximum elements in three different arrays, A, B and C:

void findMax(int A[], int B[], int C[])
{
    int maxA = A[0], maxB = B[0], maxC = C[0];
    for (int i = 1; i < MAX_LEN; i++)
    {
        if(maxA < A[i]) maxA = A[i];
        if(maxB < B[i]) maxB = B[i];
        if(maxC < C[i]) maxC = C[i];
    }
}

My goal is to figure out how to return all three values (maxA, maxB, maxC) without adding extra arguments. I'm allowed to change the return type and I can write code outside the function.

My first thought was to change void into int and create a new array that has those three max values. However, I think I need pointers to return arrays and we haven't learned that yet.

Upvotes: 1

Views: 1529

Answers (2)

Milan Patel
Milan Patel

Reputation: 422

There are two things, you can do:

The first is declare maxA, maxB, maxC out side the function (globally). Then access them in your findMax function and main function.

int maxA, maxB, maxC;  
void findMax(int A[], int B[], int C[])
{
    maxA = A[0], maxB = B[0], maxC = C[0];
    for (int i = 1; i < MAX_LEN; i++)
    {
        if(maxA < A[i]) maxA = A[i];
        if(maxB < B[i]) maxB = B[i];
        if(maxC < C[i]) maxC = C[i];
    }
}

The second is use structure.

    struct node
    {
        int maxA,maxB,maxC;
    };

    struct node findMax(int A[], int B[], int C[])
    {
        struct node Max_node;
        Max_node.maxA = A[0], Max_node.maxB = B[0], Max_node.maxC = C[0];
        for (int i = 1; i < MAX_LEN; i++)
        {
            if(Max_node.maxA < A[i]) Max_node.maxA = A[i];
            if(Max_node.maxB < B[i]) Max_node.maxB = B[i];
            if(Max_node.maxC < C[i]) Max_node.maxC = C[i];
        }
        return Max_node;
    }

P.S: you also could use pointers, but as you said you are unfamiliar with them, I used other ways.

Upvotes: 0

Silvio Mayolo
Silvio Mayolo

Reputation: 70307

There are a few ways to approach this.

The Traditional Approach (Arrays)

The easiest way is to make the function return an int[]. This gives back all of the values easily and in a very concise format. However, the int[] type cannot guarantee that its size is 3 or that it contains what you claim it contains, so this is not a very type-friendly approach.

The OO Approach (Structs)

Most JAVA enthusiasts will tell you to make a struct or a class that encapsulates the idea, such as this.

struct MaxVals {
    int maxA;
    int maxB;
    int maxC;
    // Possibly a constructor here
};

This is still a very memory-efficient solution and is much cleaner than the array approach. It also has the benefit of being more type-safe; you can't make a MaxVals with four or two ints now; it's guaranteed at compile-time to have 3. However, this is bulky. It requires you to define a new type, completely outside of your function, that will only ever be used in this one case.

The Modern Approach (Tuples)

This is a new feature of C++, adapted from Python and Haskell, so it's only available in C++11, which has limited support across compilers. Tuples are a new data structure in the tuple header which can guarantee heterogeneous, fixed-size data structures of any types you specify at compile-time. You would set your return type to std::tuple<int, int, int> and initialize the structure with std::make_tuple(maxA, maxB, maxC).

This approach has the same memory advantages and guarantees as the struct approach but without all the overhead and boilerplate of declaring a one-use type. This would be the ideal approach for a problem like this, if your compiler supports it.

Upvotes: 2

Related Questions