-2

I am new at programming, and have some trouble with my code. Here I need to find out the row in which total number of positive elements is bigger with using function. Here is my code:

using namespace std;

void function (int n, int m, int **table, int numb_posit[]) {
  int count = 0;
  int index;
  int max = -1000;
  
  for (int i = 0; i < n; i++){
    for (int j = 0; j < m; j++) {
      if (table[j][i] > 0) {
        index = i;
      }
    }
    numb_posit[i] = index;
  }

  for (int i = 0; i < n; i++) {
    if (numb_posit[i] == numb_posit[i + 1]) {
      count++;
    } else if (numb_posit[i] > max) {
      max = numb_posit[i];
    }
  }

  if (count == n) {
    cout << "Numbers are equal";
  } else {
    cout << max;
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  int table[n][m];
  int numb_posit[n];
  
  int** table = new int*[n];
  for (int i=0; i<n; i++)
  {
      int table[i] = new int[m];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> table[i][j];
    }
  }

  function (n, m, table, numb_posit);

  return 0;
}

Here is errors:

main.cpp:41:9: error: conflicting declaration ‘int** table’
   41 |   int** table = new int*[n];
      |         ^~~~~
main.cpp:38:7: note: previous declaration as ‘int table [n][m]’
   38 |   int table[n][m];
      |       ^~~~~
main.cpp:44:22: error: array must be initialized with a brace-enclosed initializer
   44 |       int table[i] = new int[m];
      |                      ^~~~~~~~~~
main.cpp:52:19: error: cannot convert ‘int (*)[m]’ to ‘int**’
   52 |   function (n, m, table, numb_posit);
      |                   ^~~~~
      |                   |
      |                   int (*)[m]
main.cpp:5:36: note:   initializing argument 3 of ‘void function(int, int, int**, int*)’
    5 | void function (int n, int m, int **table, int numb_posit[]) {
      |  

                        ~~~~~~^~~~~

If you have opportunity, I would be grateful. My main problem - I didn't get how to pass 2d arrays to function

  • 1
    May I suggest using `std::array` or `std::vector` rather than C-style arrays. – Jesper Juhl Oct 16 '21 at 15:11
  • 1
    @Someprogrammerdude When will the teachers ever catch up I wonder. They should not be teaching new/delete/arrays (specialy mutlidimensional ones) before std::array, std::vector. Dias you may tell your teacher that too :) – Pepijn Kramer Oct 16 '21 at 15:11
  • On a personal note, the code seems inspired or for a so-called "competition" or "online judge" site. Don't use such sites as learning or teaching resource, because no such site is that. Most examples and solutions on such sites seems to be either really badly written code, invalid code, or a combination of both. While learning programming and programming languages, stay as far away from such sites as is possible. – Some programmer dude Oct 16 '21 at 15:21

1 Answers1

1

Your problem:

int table[n][m];

(which isn't standard C++)

followed by

int** table = ...;

You define the same variable two times in the same scope, and both declarations are different.

Then you again create a third (and again invalid) definition with

int table[i] = ...;

and because it's a non-standard variable-length array extension it can't be initialized.

This is what the compiler is telling you.

To solve part of your problem, you need to define the variable table once only.

To solve your problem with the non-standard variable-length array, use std::vector instead:

std::vector<std::vector<int>> table(n, std::vector<int>(m));
std::vector<int> numb_posit(n);

Then change your function:

void function (std::vector<std::vector<int>> const& table, std::vector<int> const& numb_posit);

Note that the sizes aren't passed anymore, since they are available from the vectors themselves.

The allocation loop

for (int i=0; i<n; i++)
{
    int table[i] = new int[m];
}

isn't needed at all anymore.

Call as:

function (table, numb_posit);

On another note, you can use range for loops to initialize the vectors:

for (auto& vec : table) {
    for (auto& elem : vec) {
        std::cin >> elem;
    }
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621