Find Max of 2D Array: Finding value and index of largest element

I have a problem in my Intro to C++ study guide for my final that asks for a user-defined function to find the largest value and its index in a 2D int array. I've managed to write the code to create the array using a random number from 50-100 (yay) but I'm struggling with applying what I know about sorting 1D arrays to a 2D array. I have included the code that I have so far, but I'm not really sure if I'm on the right track. Currently I'm trying to swap the array values until the [0][0] value is the max, but I have no idea if this is the most efficient way to find the max value, and there's a chance it wouldn't be an acceptable solution, as I believe the wording in the study guide says to display the index value of the max value, meaning the original array should probably remain unchanged to provide the correct index.
Any help would be greatly appreciated. I'm a noob at this, so please forgive me if I haven't organized the question or code appropriately.





Incomplete code:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cmath>

using namespace std;

void findMax (int arr[3][5], int i, int j) //
{ 
    int rows = 3;
    int columns = 5;
    int Max;
    int k, m;



    string maxIndex;

    Max = arr[0][0];
    maxIndex = {0,0};

    for(i=0; i<rows; i++)
    {
        for (k=i+1; k < rows; k++)
        {
             if (arr[k][j]>arr[i][j])
                {
                    swap (arr[k][j], arr[i][j]);
                }
        }
   
    for (j=0; j<columns; j++)
    {
       for (m=j+1; m < columns; m++)
       {
           if ((arr[i][m])>(arr[i][j]))
           {
               swap (arr[i][m],arr[i][j]);
           }
            cout << arr[i][j]<<" ";
        }

    }


}
cout<<endl;
}

int main()
{
    int arr[3][5];

    int rows = 3;
    int columns = 5;
    int i, j;

    for (i=0; i<rows; i++)
    {
        for (j=0; j<columns; j++)
            {
            arr[i][j] = (50 + rand()%50);

            cout << arr[i][j] << " ";
            }
        cout << endl;
    }

    cout << endl;

    cout << arr[i][j];

    findMax(arr, i, j);

}




Last edited on
I have a problem in my Intro to C++ study guide for my final that asks for a user-defined function to find the largest value and its index in a 2D int array.

Okay, how are you trying to compute the index of the 2D array?

but I'm struggling with applying what I know about sorting 1D arrays to a 2D array.

Okay, but first why are you trying to sort the array? Why not just iterate though the array and find the "index" of the maximum value?

there's a chance it wouldn't be an acceptable solution, as I believe the wording in the study guide says to display the index value of the max value, meaning the original array should probably remain unchanged to provide the correct index.

Most likely.

Why are you passing those two ints into the function but never really using the values being passed? What exactly are you trying to pass into that function with those parameters? I would think that you may want to think about passing the sizes of the arrays into the function.

Also this:
1
2
3
4
5
6
int main()
{
    int arr[3][5];

    int rows = 3;
    int columns = 5;

Should really be more like:

1
2
3
4
5
6
int main()
{
    const int rows = 3;
    const int columns = 5;

    int arr[rows][columns];


Lastly for now, you really really need to start using meaningful variable and function names, this will make following the logic much simpler.



I really appreciate the hints. I was feeling pretty lost. Sorry about the confusing var/function names, I'll try to clean that up. Thanks for pointing me in the right direction!
Also, since you're always treating that array as a multidimensional array remember that the index will have multiple values, one for each dimension.

I think I'm starting to pick up what you're putting down lol.

For the user function, is this more in the right ballpark? I know the code is incomplete, I'm just curious if I'm approaching it from a better angle than before.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void findMaxandIndex (int arr[3][5], const int rows, const int columns)
{
    
    for (int row_index=0; row_index<rows; row_index++)
    {
        for (int column_index=0; column_index<columns; column_index++)
        {
            int maxnum= arr[row_index][column_index];

            int nextrow=row_index+1;

            if (arr[nextrow][column_index]>arr[row_index][column_index])
             {
                 maxnum= arr[nextrow][column_index];
             }
        }
        
    cout << maxnum;
    }
}
It looks like your cout is probably at the wrong place, and it is only attempting to print the maximum value, not the "index".

Also the maxnum initialization seem to be in the wrong place. It probably should be placed before the loops, and remember that arrays start at zero and stop at size - 1. You're trying to access the array at size, not size -1.


The "index" will be the index locations where the maximum value is located.

Also don't be afraid of a little consistent white space in your calculations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void findMaxandIndex (int arr[3][5], const int rows, const int columns)
{
    
    for (int row_index = 0; row_index < rows; row_index++)
    {
        for (int column_index = 0; column_index < columns; column_index++)
        {
            int maxnum = arr[row_index][column_index]; 

            int nextrow = row_index + 1; // Watch out will cause out of bounds access of the array.

            if (arr[nextrow][column_index] > arr[row_index][column_index])
             {
                 maxnum = arr[nextrow][column_index];
             }
        }
        
    cout << maxnum;
    }
}
Last edited on
I'm trying to account for the out of bounds possibility by placing a couple of for loops within the function. The output is definitely wrong for what I want, am I getting closer or is there a more efficient way to ensure the appropriate variables are accessed?





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
37
38
39
40
41
void findMaxandIndex (int arr[3][5], const int rows, const int columns)
{
    int row_index=0;
    int column_index=0;

    int maxnum= arr[row_index][column_index];

    for (int row_index=0; row_index<rows; row_index++)
    {

        for (int column_index=0; column_index<columns; column_index++)
        {
            int nextrow=row_index+1;
            int nextcolumn = row_index+1;

            for (nextrow; nextrow < rows; nextrow++)// as long as next row is less than size
            {
                if (arr[nextrow][column_index]>arr[row_index][column_index])
                    {
                        maxnum= arr[nextrow][column_index];
                        row_index=nextrow;
                     }
            }
            for (nextcolumn; nextcolumn < columns; nextcolumn++)// same but for columns
             {
                 if (nextcolumn<columns && arr[row_index][nextcolumn]>arr[row_index][column_index])
                    {
                        maxnum = arr[row_index][nextcolumn];
                        column_index=nextcolumn;
                    }


             }

        }
        cout << maxnum<<endl;
        cout << row_index<<endl;
        cout << column_index<<endl;

}
}
Last edited on
Again your printout appears to be in the wrong place, it should be after all the loops end. Also you should only need two for() loops to accomplish this objective.

Something more like:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void findMaxandIndex (int arr[3][5], const int rows, const int columns)
{
    int max_row_index = 0;
    int max_column_index = 0;
    int max_num = arr[max_row_index][max_column_index];
    
    for (int row_index = 0; row_index < rows; row_index++)
    {
        for (int column_index = 0; column_index < columns; column_index++)
        {
            if(max_num < arr[row_index][column_index])
            {
                max_num = arr[row_index][column_index];
                max_row_index = row_index;
                max_column_index = column_index;
            }
        }
    }
    
    std::cout << "Maximum value: " << max_num << " index[" << max_row_index << "][" << max_column_index << "]\n";
}


But note I would think about returning the "index" to the calling function instead of printing it out inside the function.

Last edited on
I really appreciate it, that makes a lot more sense than what I was trying to do. For clarity, are you suggesting I print the maxnum within the void function still and return the index value for it to the main function for printing, or return both (as a string maybe?) to the main?
Last edited on
No, I'm saying return the "index" to the calling function and do any printing there. The idea is to have your function do one thing, in this case find the maximum value and return the "index" let "someone" else do whatever needs to be done with that "index".
Topic archived. No new replies allowed.