I am unable to print the notes, which would be n1 and N2 of the list, I do not know if the parameter sent to the function is correct

Asked

Viewed 68 times

-1

#include <stdio.h>
#include <stdlib.h>

typedef struct nota n;

struct nota
{
    float n1;
    float n2;
    float mf;

    struct nota *prox;

};

void imprime(n *le)
{
    while(le != NULL)
    {
        printf("%.0f",le -> n1); 
        printf("%.0f\n",le -> n2);
        le = le -> prox;
    }
    printf("\nFIM");
}

int main()
{
    n *paux;

    int qt;

    printf("Quantidade de notas: ");
    scanf("%d",&qt);

    paux = (n*)malloc(sizeof(n));

    for(int i=0;i<qt;i++)
    {
        printf("Nota 1: ");
        scanf("%f",&paux -> n1);

        printf("Nota 2: ");
        scanf("%f",&paux -> n2);

        paux -> mf = (paux -> n1 + paux -> n2)/2;

        paux -> prox;

        printf("\n");
    }

    imprime(paux);

    return 0;
}
  • Low quality question. You want to print each note?

  • Exactly, that in case it would be n1 and N2.

1 answer

1


The problem I’m seeing is that your auxiliary pointer is always the same. To create a list, with each iteration of your for, you should allocate a new space in memory and assign this space to your auxiliary pointer

For example:

int main()
{
    // Crio o nodo "inicial" apenas para manter registro da posição inicial da lista
    n *inicial = malloc(sizeof(n));
    n *paux = inicial;

    int qt;

    printf("Quantidade de notas: ");
    scanf("%d", &qt);

    for (int i = 0; i < qt; i++)
    {
        printf("Nota 1: ");
        scanf("%f", &paux->n1);

        printf("Nota 2: ");
        scanf("%f", &paux->n2);

        paux->mf = (paux->n1 + paux->n2) / 2;

        if (i + 1 == qt) 
        {
            // Se for o ultimo item da lista, atribuo NULL para o ponteiro do próximo nodo
            paux->prox = NULL;
        }
        else
        {
            // Caso contrário, aloco um novo espaço na memória para o próximo nodo, e movo o ponteiro auxiliar para essa posição
            paux->prox = malloc(sizeof(n));
            paux = paux->prox;
        }

        printf("\n");
    }

    // Envio o nodo com a posição inicial da lista
    imprime(inicial);

    return 0;
}

With the help of pointers pointing to pointers, it is also possible to get rid of the condition if. Note that the result of this code is the same, but a code capable of treating the exception as standard is generally considered more elegant.

int main()
{
    n *inicial = NULL;
    n *aux;
    n **paux = &inicial;

    int qt;

    printf("Quantidade de notas: ");
    scanf("%d", &qt);

    for (int i = 0; i < qt; i++)
    {
        (*paux) = malloc(sizeof(n));
        aux = *paux;

        printf("Nota 1: ");
        scanf("%f", &aux->n1);

        printf("Nota 2: ");
        scanf("%f", &aux->n2);

        aux->mf = (aux->n1 + aux->n2) / 2;
        aux->prox = NULL;

        paux = &aux->prox;

        printf("\n");
    }

    imprime(inicial);

    return 0;
}
  • Thank you so much !! It is not a complex issue but I was having a lot of difficulty, anyway thank you so much for the help again.

Browser other questions tagged

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