Advertisement Remove all ads

Write a Definition for a Function Summidcol(Int Matrix [10], Int N, Int M) in C++, Which Finds the Sum of the Middle Column'S Elements of the Matrix (Assuming N Represents a Number of Rows and M Represents the Number of Columns, Which is an Odd Integer). - Computer Science (C++)

Write a definition for a function SUMMIDCOL(int MATRIX [10], int N, int M) in C++, which finds the sum of the middle column's elements of the MATRIX (Assuming N represents a number of rows and M represents the number of columns, which is an odd integer). 
Example: If the content of array MATRIX having N as 5 and M as 3 is as follows

1 2 1
2 1 4
3 4 5
4 5 3
5 3 2

The function should calculate the sum and display the following :

Sum of Middle Column: 15

Advertisement Remove all ads

Solution

void SUMMIDCOL (int MATRIX[5][3], int N, int M)
{
   int i, j;
   int sum = 0;
   cout << "The array is \ n";
   for(i = 0; i < N; i ++)
   {
       for (j = 0 ; j < M ; j + +)
       cout << MATRIX[i][j] << "\t";
       cout << endl;
   }
   for(i = 1; i < 2; i ++)
   { 
      for (j = 0; j < N; j++)
      {
          sum = sum + MATRIX[j][i];
      }
      cout << \n sum of middle column :" << sum;
   }
}
Concept: Pointers and Arrays - Array of Pointers, Pointer to an Array (1 Dimensional Array), Function Returning a Pointer, Reference Variables and Use of Alias
  Is there an error in this question or solution?
Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×