Answer the question
In order to leave comments, you need to log in
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){
^
#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
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] ...);
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question