Trying to build a graph

Asked

Viewed 128 times

0

Well, folks, I suppose my problem is pretty simple, but I’m having a hard time, so if you could help me I’d appreciate it!

What I want is the following: create a vector that can allocate several characters referring to a graph, vector type Vertex[A,B,C,D,...,AXZ]

and then I’ll do the same for edge-like edges[(A,B),(C,B),(A,AZ),...,(AZS,AZZ)] and the like.

but at the time I was debugging I passed the integer value V to name the size of the vector, but at the time it passes to the function, the value 5 becomes 79 and there mess

inserir a descrição da imagem aqui

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <locale.h>
#include <ctype.h>

//esta função constrói os vértices do grafo
char graph_vertex(){
    int k;
    char *vertex;
    //variável para pegar a quantidade de vértices presentes no grafo em questão
    int vertice;
    //iniciando a variável
    k=0;
    //da começo à construção do vértice, a partir da aquisição da quantidade de vértices pertencentes ao grafo
    printf("Quantos vértices o grafo possui?\n");
    scanf("%d",&vertice);
    //cria a variável vertex para alocar os vértices do grafo
    vertex = (char*)malloc(vertice * sizeof(char)); 
    //aquisição de vértices
    printf("Digite os vértices pertencentes ao grafo.\n" );
    printf("Obs.: o grafo pode ser constituído por letras ou números. \n" );
    //entra num loop a fim de capturar todos os vértices
    while(vertice != k){
        //o caracter em questão é gravado na variável vertex
        scanf(" %c",&vertex[k]);
        //esta condição verifica se o caracter digitado é uma letra
        if (isalpha(vertex[k]))
        {
            //caso seja uma letra, esta mesma será convertida para letra maiúscula
            vertex[k] = toupper(vertex[k]);
            k++;
        }else{      
            k++;
        }       
    }
    //impressão do vetor [vertex]
    int i =0;
    while(vertice != i){
        printf("\n");
        printf("%c \n", vertex[i]);
        i++;
    }
}


int main(){
    setlocale(LC_ALL, "Portuguese");

    //chama a função responsável pelo vértice
    graph_vertex();
    return(0);

}
  • I made the changes, but for some, if I put the size of the amount of vertices 5, it only reads two vertices and makes the impression of 5 numbers. I believe it refers to the number of the vertex size

  • I edited my comment to answer this problem :)

  • 1

    I managed to, thank you very much!

1 answer

2


You stated V as a int but read it as a long, it would be right to read as a whole (scanf("%d", &V)) or declare V as a long (long V;)

Another point, you will probably have problems like Segmentation fault by using a parameter to set the vector size vertex. Instead of using char vertex[n]; use the malloc, as follows:

char *vertex = malloc(n * sizeof(char));

At the time of reading, as you are working with char, it ends up reading spaces and line breaks too, so you need to ignore them:

scanf(" %c", &vertex[k]); // foi adicionado um espaço antes do %c

At the time of the print there is also a little mistake. You are wanting to print the value of the vector at a given position, not its address, so you do not need to use the "&":

printf("%c\n", vertex[k]);

PS: When you finish using the vector, don’t forget to de-locate the memory using free(vertex)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.