In an attempt to implement an adjacent array graph, I created the createVertice function in which a new "array" would be created and the contents of the graph would be copied to it, adding the new positions. However, when calling this function and printing the new graph, the output contains several memory garbage. Where is the error?
class GrafosMatriz{
public:
int *grafo;
int tam;
GrafosMatriz(int tam){
this->tam = tam;
grafo = new int[tam*tam];
for(int i = 0 ; i < tam*tam; i++)
grafo[i] = 0;
}
void criarVertice(){
tam++;
int *aux = new int[tam*tam]; //matriz aux declarada com n+1
for(int i = 0 ; i < tam*tam; i++){
aux[i] = 0;
}
for(int i = 0, j = 0; i < tam*tam - tam; i++){ //Passar pela matriz nova de tamanho n+1, sem passar na ultima linha
if(i%tam != 0){ // pular a ultima coluna da matriz
j++;
aux[i] = grafo[j];
}
}
grafo = aux;
delete aux;
}