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