0
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
//Function to calculate largest column
void largestInColumn(int mat[][MAX], int rows, int cols)
{
    for (int i = 0; i < cols; i++) {
// initialize the maximum element with 0
        int maxm = mat[0][i];
// Run the inner loop for rows
        for (int j = 1; j < rows; j++) {
// check if any element is greater than the maximum element of the column and replace it   

            if (mat[j][i] > maxm)
                maxm = mat[j][i];
        }
       
        cout << maxm << endl;
    }
}
// Driver code
int main()
{
    int n , m ;
   cin>>n>>m;
   int mat[n][m];
   for(int i=0;i<n;i++)
   {
   for(int j=0;j<m;j++)
   {
    cin>>mat[i][j];
   }    
   }
    largestInColumn(mat, n, m);
    return 0;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You seem to be getting started on the wrong foot. See: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096) To answer your question, declare an array with the same number of elements you have columns and initialize each to `INT_MIN`. The loop over your 2D array comparing the value in each column against the value in your 1D column array. If the value in the 2D array is larger than what you have in your 1D array for that column, set the element in your 1D array to that value. – David C. Rankin Jun 28 '20 at 07:27
  • `cin>>n>>m; int mat[n][m];` -- This is not legal C++ syntax. Arrays in C++ must use compile-time constants, not variables to denote the number of entries. If you used `std::vector> mat(n, std::vector(m))` and passed that instead, you would have a better chance of getting passed the compiler error. And since that syntax is not legal, I am assuming that the error is because of you using the non-standard syntax. – PaulMcKenzie Jun 28 '20 at 08:13

2 Answers2

1

I will answer the question using valid C++ statements. There are no VLAs in C++. I will use a std::vector instead.

There is also no need to store any value of the matrix for this task. You can make the decision already during reading of the values.

#include <iostream>
#include <vector>
#include <limits>

int main() {

    // Read number of rows and columns
    if (size_t numberOfRows{}, numberOfColumns{}; std::cin >> numberOfRows >> numberOfColumns) {

        // Here we will store the max values per column, so, the result
        std::vector<int> maxColumnValue(numberOfColumns, std::numeric_limits<int>::lowest());

        // Read all rows and columns
        for (size_t row{}; row < numberOfRows; ++row)
            for (size_t col{}; col < numberOfColumns; ++col)

                // If the current value of the column is greater than the current max value, then use new value instead
                if (int value{ std::numeric_limits<int>::lowest() }; std::cin >> value)
                    if (value > maxColumnValue[col]) maxColumnValue[col] = value;

        // Show result to the user
        for (const int m : maxColumnValue) std::cout << m << '\n';
    }
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
0

The cause of the error is due to you trying to pass a variable-length-array to a function that requires a standard 2D array.

First, variable-length-arrays (VLA's) are not part of standard C++. Arrays in C++ require that the sizes of the array are known at compile-time, not runtime. So pretend they don't exist, because technically, they do not exist in standard C++.

Thus you have two choices:

  1. Declare a non-variable-size 2D array and use that, or
  2. Use a container that is built to have dynamic size, such as std::vector.

Since you did not specify how large n could be, then solution 2 is safer.

Given that, here is your code using std::vector:

#include <vector>
#include <iostream>

using Int1D = std::vector<int>;
using Int2D = std::vector<Int1D>;

//Function to calculate largest column
void largestInColumn(Int2D& mat, int rows, int cols)
{
    for (int i = 0; i < cols; i++) 
    {
        int maxm = mat[0][i];
        for (int j = 1; j < rows; j++) 
        {
            if (mat[j][i] > maxm)
                maxm = mat[j][i];
        }
        std::cout << maxm << std::endl;
    }
}

int main()
{
   int n , m ;
   std::cin >> n >> m;
   Int2D mat(n, Int1D(m));
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<m;j++)
       {
         std::cin >> mat[i][j];
       }    
   }
   largestInColumn(mat, n, m);
}

Using the input:

3 3 
1 2 3
1 4 9
76 34 21

The output is:

76
34
21
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45