1 votes

To build arrays in c++

I am trying to create an array whose size depends on the value of a few parameters that I receive. The problem is that I asked that the matrix must have a few constant values to define its size.

Ex:

int main(){
int tam1,tam2;
cin >> tam1 >> tam2;
int matriz[tam1][tam2];
return 0;
}

How could I solve the problem?

PD: The matrix is intended to be 'STATIC' and do not vary in size over time.

3voto

eferion Points 31764

The static concept, in programming, means that the lifetime of the object is linked to the life of the application. That is to say, that the object will die when you leave the application.

On the other hand, in C++, the arrays of fixed size are structures whose number of elements is fixed, and is determined at compile time:

int vector[25];
char cadenaTipoC[200];

This is the type of structure that you are trying to create. The reason it won't work is that, as I said, the size of these elements is determined at compile-time... while in your case the size is determined at run time (that is a data entered by the user).

As well as the requirements prevent you from using this type of structure, so you have to resort to other alternatives:

You can, for example, to work with a pointer to a double. The disadvantage in this case is that the creation and release are a little longer:

int tam1,tam2;
cin >> tam1 >> tam2;
int** matriz = new int*[tam1];
for( int i=0; i<tam1; i++ )
  matriz[i] = new int[tam2];

// ...

for( int i=0; i<tam1; i++ )
  delete[] matriz[i];
delete[] matriz;

Of course, now is the time you could encapsulate this logic into a class, simplifying the code end.

Now, you say you want to create a static array, as we have said, to do this just to stay alive during the entire execution of the program.

To achieve this, you can choose to create a global variable:

// Variable global
static int ** matriz;

int main()
{
  // ...
}

Although it is not the option that I would recommend... it is almost better to create a static class or a namespace to store this object (for readability, access control, and those things):

Static class

static class CosasEstaticas
{
  static int** matriz = nullptr;

public:
  static void InicializarMatriz(int filas, int columnas)
  {
    if( matriz == nullptr )
    {
      matriz = new int*[filas];
      for( int i=0; i<filas; i++ )
        matriz[i] = new int[columnas];
    }
  }

  static int** Matriz()
  {
    return matriz;
  }
};

Namespace

Header: //CosasEstaticas.h namespace CosasEstaticas { static void InicializarMatriz(int rows, int columns); static int** Array(); }

Implementation: // CosasEstaticas.cpp namespace { int** array = nullptr; }

 void CosasEstaticas::InicializarMatriz(int filas, int columnas)
 {
   if( matriz == nullptr )
   {
     matriz = new int*[filas];
     for( int i=0; i<filas; i++ )
       matriz[i] = new int[columnas];
   }
 }

  int** CosasEstaticas::Matriz()
  {
    return matriz;
  }

Of course you can also pass the pointer to all functions that need it, which gives you more control over who gets access to the object:

void tratarMatriz(int**matriz);

int main()
{
  int**matriz;
  // inicializacion matriz
  // ...

  tratarMatriz(matriz);

  // ...
}

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X