David Paris Vicente
asked on
How to create a multi-dimension array in C Plus Plus
I need some help to create a multi-dimension array in C Plus Plus, but the user have to input the size of the array n x n.
The array has to be in simple C Plus Plus code with no class creation.
I try to make some thing like this, but dont work.
int main ()
{
int a, b;
int matriz[a][b];
cout<<"Define the size of the matrix.\n";
cin >> a;
b=a;
}
But because i´m a noob on programming i can´t make it work. Please can you give me some help or directions.
Appreciated.
The array has to be in simple C Plus Plus code with no class creation.
I try to make some thing like this, but dont work.
int main ()
{
int a, b;
int matriz[a][b];
cout<<"Define the size of the matrix.\n";
cin >> a;
b=a;
}
But because i´m a noob on programming i can´t make it work. Please can you give me some help or directions.
Appreciated.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Right ... C++, not C ... Ignore my previous post please :)
>>>> Right ... C++, not C ... Ignore my previous post please :)
Most nowadays C++ compilers won't allow definition of an array with non-const size parameters (though a next standard might require that)
Most nowadays C++ compilers won't allow definition of an array with non-const size parameters (though a next standard might require that)
>> Most nowadays C++ compilers won't allow definition of an array with non-const size parameters
It seems I have been spoiled by g++ which supports it as an extension.
Variable length arrays are allowed in C99, but are not covered by the C++ standard. So, my initial response was correct after all (I should rely on my intuition a bit more lol).
You have to use dynamically allocated memory for variable length arrays in C++, unless you want to make use of a compiler extension like g++ has.
It seems I have been spoiled by g++ which supports it as an extension.
Variable length arrays are allowed in C99, but are not covered by the C++ standard. So, my initial response was correct after all (I should rely on my intuition a bit more lol).
You have to use dynamically allocated memory for variable length arrays in C++, unless you want to make use of a compiler extension like g++ has.
ASKER
Yes it´s in C++.
errang, that don´t work .
I´m trying to make the game Tic Tac Toe but the user have to input the size of the matrix.
If the matrix is static is easy but they want the matrix dynamic.
I made this when i saw some examples, but i don´t understand.
#include <iostream>
using namespace std;
int main ()
{
int i=0,a,b;
int **matrix;
cout<< "Insert the size of the Matrix.\n";
cin>>a;
b=a;
matrix= new int*[a];
for (i; i<a;i++)
{
matrix[i]= new int [b];
}
system ("PAUSE");
return 0;
}
And how can i send some information to inside, i cant visualize in my mind how this works.
Thank you all.
errang, that don´t work .
I´m trying to make the game Tic Tac Toe but the user have to input the size of the matrix.
If the matrix is static is easy but they want the matrix dynamic.
I made this when i saw some examples, but i don´t understand.
#include <iostream>
using namespace std;
int main ()
{
int i=0,a,b;
int **matrix;
cout<< "Insert the size of the Matrix.\n";
cin>>a;
b=a;
matrix= new int*[a];
for (i; i<a;i++)
{
matrix[i]= new int [b];
}
system ("PAUSE");
return 0;
}
And how can i send some information to inside, i cant visualize in my mind how this works.
Thank you all.
>> I made this when i saw some examples, but i don´t understand.
That looks ok. It creates a dynamically allocated 2-dimensional array. For more information, see the tutorial I mentioned earlier.
You can access the dynamically allocated array, in the same way as you access a normal array, ie. matrix[i][j] will get the value at row i, column j.
That looks ok. It creates a dynamically allocated 2-dimensional array. For more information, see the tutorial I mentioned earlier.
You can access the dynamically allocated array, in the same way as you access a normal array, ie. matrix[i][j] will get the value at row i, column j.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I´m gone try.
Thanks
Thanks
Another approach is to just allocate it in a linear 1 dimensional array, and write an "access(arr, row, col)" method. Arrays are laid out like that anyway.
int *matrix = new int[ ROWSIZE * COLSIZE ];
int access(int * matrix, int row, int col) {
return matrix[ ROWSIZE * row + col];
}
int *matrix = new int[ ROWSIZE * COLSIZE ];
int access(int * matrix, int row, int col) {
return matrix[ ROWSIZE * row + col];
}
ASKER
I believe is this that i want>
void matriz()
{
int a,b,c=1;
int **matrix;
cout<< "Insert the size of the Matrix.\n";
cin>>a;
b=a;
for (int i=0; i<a;++i)
{
matrix= new int*[a];
for (int j=0; j<a; ++j)
{
matrix[i]= new int [b];
matrix[i][j]= c;
c++;
cout <<matrix[i][j]<<" ";
}
cout << endl;
}
}
Maybe it´s not the write way but it work.
Can you give me your feedback please.
If you agree with this way i will close this question and give the points.
Thank you all
void matriz()
{
int a,b,c=1;
int **matrix;
cout<< "Insert the size of the Matrix.\n";
cin>>a;
b=a;
for (int i=0; i<a;++i)
{
matrix= new int*[a];
for (int j=0; j<a; ++j)
{
matrix[i]= new int [b];
matrix[i][j]= c;
c++;
cout <<matrix[i][j]<<" ";
}
cout << endl;
}
}
Maybe it´s not the write way but it work.
Can you give me your feedback please.
If you agree with this way i will close this question and give the points.
Thank you all
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I believe is this that i want>
As mrjoltcola already pointed out, there are a few problems with that code, memory leaks not being the least of it. But note that you posted correct code earlier here : http:#24059085. Why change that ?
As mrjoltcola already pointed out, there are a few problems with that code, memory leaks not being the least of it. But note that you posted correct code earlier here : http:#24059085. Why change that ?
ASKER
Thanks to you all.
Antoher Question it is possible to call another function inside of a function?
Antoher Question it is possible to call another function inside of a function?
int main (int argc, char **argv)
{
int a = atoi(argv[1])
int b = atoi(argv[2]);
int matriz[a][b];
}
With this you'd have to enter the dimensions of the array as a command line argument.