E
E
Egor2016-01-31 12:48:47
C++ / C#
Egor, 2016-01-31 12:48:47

How to pass a 2D array to a function?

Hello. Can't get it to work. I am getting errors:

project_two.cpp:5:44: error: ‘sizeMatrix’ was not declared in this scope
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm);
                                            ^
project_two.cpp:5:55: error: expected ‘)’ before ‘,’ token
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm);
                                                       ^
project_two.cpp:5:57: error: expected unqualified-id before ‘int’
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm);
                                                         ^
project_two.cpp:18:44: error: ‘sizeMatrix’ was not declared in this scope
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm){
                                            ^
project_two.cpp:18:55: error: expected ‘)’ before ‘,’ token
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm){
                                                       ^
project_two.cpp:18:57: error: expected unqualified-id before ‘int’
 void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm){
                                                                                           ^

Here is the code:
#include <iostream>
#include <cstdlib>
using namespace std;

void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm);

int main (void){
  const int sizeMatrix = 10;
  int matrix[sizeMatrix][sizeMatrix];
  int minElementMatrix = 0;
  int maxElementMatrix = 0;
  
  fillingAndShowElementsMatrix (matrix, sizeMatrix);
 
  return 0; 
}

void fillingAndShowElementsMatrix (int m[][sizeMatrix], int sm){
  for (int i = 0; i < sm; i++){
    for (int j = 0; j < sm; j++){
      m[i][j] = rand () % 31 + 30;
      cout << m[i][j] << endl;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2016-01-31
@Nemovok

And what is sizeMatrix for the compiler in line
Variables, even constants, cannot be in such a definition.
Determine at the beginning

#define SIZEMATRIX     10

void fillingAndShowElementsMatrix (int m[][SIZEMATRIX] ...);

V
Vitaly, 2016-01-31
@vt4a2h

Above the answer, or in the comments on the fact about different methods, I’ll add on my own that it’s better to use std::vector<std::vector<T>>or something like boost::numeric::ublas::matrix. It depends of course on the goals.

E
Egor, 2016-01-31
@Nemovok

Thank you kind person!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question