2d array row search

I know how to find the largest number but I how do I find the largest number in each row in a 2d array and store it so I can add them together later on.

This is the function I have been trying to work with but I cannot figure out how to store the largest number in each row and add them. But I would want to output the number so I know that the function is working properly
1
2
3
4
5
6
7
8
9
10
11
  void findMax(int** arr, int size, int size2){
    int temp = 0;
    for (int row = 0; row < size; row++){
        for (int col = 0; col < size2; col++){ 
         if(arr[row][col]>temp){
            temp=arr[row][col];
          }  
      }
        cout<<"Largest in each row: "<<temp<<endl;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void findMax(int** arr, int size, int size2){
    
    int max_row_sum = 0; //new

    for (int row = 0; row < size; row++){
        int temp = 0;
        for (int col = 0; col < size2; col++){ 
         if(arr[row][col]>temp){
            temp=arr[row][col];
          } 
         cout<<"Largest in each row: "<<temp<<endl;
         max_row_sum += temp; //new
      }
      cout<<"sum of largest numbers is "<<max_row_sum<<endl;
}
Last edited on
Thanks so much, just one modification I made below so it outputs the correct sum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void findMax(int** arr, int size, int size2){
    
    int max_row_sum = 0; //new

    for (int row = 0; row < size; row++){
        int temp = 0;
        for (int col = 0; col < size2; col++){ 
         if(arr[row][col]>temp){
            temp=arr[row][col];
          }    
      }
        cout<<"Largest in each row: "<<temp<<endl;//swapped with brace above
         max_row_sum += temp; //new
    }
       cout<<"sum of largest numbers is "<<max_row_sum<<endl;
}
Last edited on
Oh sorry. I personally dont indent like that coz i find it difficult to see the opening brace.
Topic archived. No new replies allowed.