I have a function that needs to be passed a multidimensional (a x * m array to be more with specific), and print all the elements of the array. I have everything figured out except how to pass the actual multidimensional array itself.
My whole code looks like this so far:
/**
3.Elabore un programa que lea una matriz de orden m x n y la escriba poniendo las columnas como renglones y los renglones como columnas.
Por ejemplo, si la matriz que da el usuario es:
4 7 1 3 5
2 0 6 9 7
3 1 2 6 4
entonces el programa debe escribir la matriz transpuesta:
4 2 3
7 0 1
1 6 2
3 9 6
5 7 4
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void imprimirMatriz(int mat[][], int filas, int columnas)
{
for(int i = 0; i < filas; i++)
{
for(int j = 0; j < columnas; j++)
{
cout << mat[i][j] << ' ';
}
cout << endl;
}
}
int main()
{
srand(time(NULL));
int n, m;
cin >> n >> m;
int mat[n][m];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
mat[n][m] = rand() % (9 - 0 + 1) + 0;
}
}
cout << endl;
imprimirMatriz(mat, n, m);
return 0;
}
When I attempt to compile that, I get the following errors:
Quote
3.cpp|25|error: expected ')' before ',' token
3.cpp|25|error: expected unqualified-id before 'int'
At first, I had no idea of what was going on. My main concern is the first error, because I know why I am getting the other two. Now I have googled a bit about this (how to pass a multidimensional array to a function) in Google, but everyone suggests that I specify a constant number for the second part of the array in the signature of the function itself, a like this:
void func(int x[][2]);
Apparently, this solution works for most people, but I have two problems with it.
1) It's not exactly a solution for me since I am generation both sides of the array dynamically without constant values.
2) Even if I try to compile with the following function:
void imprimirMatriz(int mat[][5], int filas, int columnas)
{
for(int i = 0; i < filas; i++)
{
for(int j = 0; j < columnas; j++)
{
cout << mat[i][j] << ' ';
}
cout << endl;
}
}
I get an error I have never seen in 5 years I have been programming with C++:
Quote
So can someone please aid me with this? Everything I want to do is to pass a multidimensional array with sizes created dynamically.
This post has been edited by Crimson Wings: 30 September 2011 - 08:35 PM

New Topic/Question
Reply




MultiQuote








|