Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, 

I am trying to find the largest value in a 2D array. The 2D array has been populated with numbers that has been entered by users. Each row represents a 'topic'. The function below should loop through the array starting with its row and 10 columns and totalling the result. 

Please note: I am going through an old C++ book that uses arrays as opposed to improved containers.

Thank you in advance!! :)


What I have tried:

int getHighest(int arr[][10])
{
    int total = 0;
    int index;
    int largest = 0;
    
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (responses[i][j] >= 1)
            {
                index = j + 1;
                total += (index * (responses[i][j]));
            }
        }
        if (total > largest)
        {
            largest = total;
        }
    }

    return largest;
}
Posted
Updated 1-Nov-17 2:27am
v2

1 solution

Move your if condition into the inner loop, and change it so it compares and uses the array cell instead of total.
Think about it: unless your array contains a significant number of negative numbers, the total will always be the same or bigger than the largest value...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900