I'm still doing the university exercises and I've come to this one:
You want to implement an array type. The implementation is intended to be in dynamic memory and in which each element can connect with the next one in the row (right), the previous one in the row (left), the next one in the column (bottom) and the previous one in the column. (up).
It is requested to implement a procedure createMatriz10x15 that, given a variable of type Matrix, creates the necessary dynamic memory and establishes the necessary pointers in memory to store a matrix of 10 rows and 15 columns. All nodes must be connected by pointers to their corresponding elements (when they exist) in the top row, bottom row, right column, and left column. When, for example, a node is in the first row, its up pointer will be equal to nullptr (since there is no row above it), something similar will happen with the down pointer in the last row, the left pointer in the first column and the right one in the last column.
I have this code at the moment:
In the header:
#ifndef CABECERA_H
#define CABECERA_H
struct Nodo
{
float dato;
Nodo* derecha; Nodo* izquierda;
Nodo* abajo; Nodo* arriba;
};
struct Matriz
{
Nodo* primer_elemento;//Puntero a (1,1) de la matriz
};
void crearMatriz10x15(Matriz& m);
//POST: 'm' es una Matriz 10x15 con conexiones de punteros entre filas y columnas
//adyacentes
#endif // CABECERA_H
In the main.cpp:
#include <iostream>
#include <cabecera.h>
using namespace std;
int main()
{
Matriz matriz10x15;
return 0;
}
void crearMatriz(Matriz& m)
{
Matriz* p=new Matriz[10][15];
p=&m;
for(int i=0; i<10; i++)
{
for(int j=0; j<15; j++)
m.dato[i][j]=0;
}
}
My question is the following: How do I assign the dynamic memory to the array m[10][15], and what type would the allocation pointer be? I can't understand how to start the exercise, I tried with:
Nodo* p= new Nodo[10][15];
//y con:
Matriz* p=new Matriz[10][15];
Both give me an error.
Thanks in advance.
What can be done is a list that contains lists, in order to simulate the matrix I made an example that can help you, I hope it helps.
Dont do it. You are confusing
Matriz
withNodo
.Understanding that it
Nodo *Matriz::primer_elemento
is the element in the upper left corner, if you want a 10 × 15 matrix you will need to request 150 (ten times fifteen) nodes:I don't know how you are supposed to free the memory of
Nodo *Matriz::primer_elemento
, I would advise you to add a destructor toMatriz
:Note that if you don't initialize type elements
Matriz
nothing will happen when applyingdelete
onnullptr
.Regarding binding the pointers, we know that for a 10 × 15 matrix:
derecha
: Index + 1 whenever the modulus of the index with the width does not match the width.izquierda
: Index - 1 as long as the modulus of the index with the width is not 0.abajo
: Index + width as long as the modulus of the index with the height does not match the height.arriba
: Index - width as long as the modulus of the index with the height is not 0.So that:
Therefore, you can use the function like this:
a 10x15 array has, by definition, 150 elements... but nowhere does it say that you have to create those 150 elements individually... they just require you to use dynamic memory.
Creating the array is then as simple as making a single reservation with room for 150 nodes:
To map the nodes of the matrix you can act in various ways... in this case, since the nodes are contiguous in memory, I prefer to choose to take advantage of this feature together with a bit of pointer arithmetic:
The mappings of
arriba
andabajo
are easy to explain: As all the nodes are consecutive in memory and each row is made up of 15 columns... to go to the node that is right above it is enough to go back 15 positions and do the same but move forward to find the node just below. In other words:To free the memory, as only a memory reservation has been made, it is enough to use the operator
delete[]
: