Question 4(10pt): sum of the max value found in each row Write a function maxSum that takes two parameters; a 2D integer array with 10 columns, and the number of rows. The function must return the sum of the maximum value from each row.

Respuesta :

Answer:

Following is the program in c++ language  

#include <iostream> // header file

using namespace std; // namespace

int maxSum(int ar[][10],int r) // function definition of  maxsum

{

  int s=0; // varaible declaration

  for(int k=0;k<r;k++)// iterating the loop

  {

      int maximum=ar[k][0]; //finding the maximum value

      for(int k2=0;k2<10;k2++)// iterating the loop

      {

          if(maximum<ar[k][k2]) // check the condition

          {

            maximum=ar[k][k2];   // storing the value in the maximum variable

          }

           }

       s=s+maximum;//adding maximum value to the sum

  }

  return s; // return the sum

}

int main() // main function

{

  int ar[][10]={{5,2,8,-8,9,9,8,-5,-1,-5},{4,1,8,0,10,7,6,1,8,-5}}; // initilaized array

  cout<<"sum of maximum value:";

  int t=maxSum(ar,2); // function calling

  cout<<t; // display the maximum value

}

Output:

sum of maximum value:19

Explanation:

Following are the description of Program  

  • The main method declared and initialized an array ar with the column value 10.
  • After that call the method maxsum .The control will move to the function definition of maxsum.
  • In the function header of maxisum there are two value i.e array "ar" and "r".
  • In this function definition, we adding the maximum value to the sum and return the variable "s".
  • In the main function, we print the value.