Pass data from one array to another

Asked

Viewed 403 times

0

How can I pass some data from one vector to another? I’m doing an exercise and I need to take several data from several students at struct and place in a vector. Then I need to check if they were approved or failed and divide into other 2 vectors.

I need a for to go through the main vector and check the notes, but how do I move other vectors and allocate the data without leaving blank spaces?

void leitura (struct medialunos vet[tf], int vet1[tf], int vet2[tf]){
    int i, j; 
    for (i=0;i<tf;i++){
        for (j=0; j<tf;j++){
            printf ("\nEntre com a matricula: ");
            scanf ("%d", &vet[i].matricula);

            printf ("\nEntre com o nome: ");
            fflush (stdin);
            fgets (vet[i].nome,20,stdin);

            printf ("\nEntre com a media final: ");
            scanf ("%lf", &vet[i].mediafinal);

            printf ("\n_____________________________________________\n");

            if (vet[i].mediafinal >= 6){
                vet1[j] = vet[i];

            else 
            vet2[j] = vet[i];
            }
        }
    }
}
  • Set your function to: void reading (struct medialunos vet[], int vet1[], int vet2[], int Tf){. It doesn’t make much sense for this two for command, how do you want to assign an element of an int vector an element of a medial struct vector? For vet1 and vet2 vectors use different indexes for each of them, initialized with zero and incremented with each new element added

1 answer

0


You don’t have to make another loop to control that, loops are used to keep repeats (people don’t understand their use, for some reason they think it has to do with variables), you just need to have a loop and already a loop for it. You need to have two extra variables to control the progress of using indexes in each array. Note that I gave better names and organization in the code. This tf does not please me, I hope it is not what I am thinking (global variable).

void leitura(struct medialunos alunos[tf], int aprovados[tf], int reprovados[tf]) {
    for (int i = 0, aprovado = 0, reprovado = 0; i < tf; i++) {
        printf("\nEntre com a matricula: ");
        scanf("%d", &alunos[i].matricula);
        printf("\nEntre com o nome: ");
        fgets(alunos[i].nome, 20, stdin);
        printf("\nEntre com a media final: ");
        scanf("%lf", &alunos[i].mediafinal);
        printf("\n_____________________________________________\n");
        if (alunos[i].mediafinal >= 6) aprovados[aprovado++] = alunos[i];
        else reprovados[reprovado++] = alunos[i];
    }
}

I put in the Github for future reference.

  • Dear friend @Maniero I improved on your answer in the matter of structuring the writing, and in the conditional operator I changed to a ternary conditional operator, I hope you like it.. I would also like to know what you meant by "I hope it’s not what I’m thinking.", it would be good to add description to your answer, apart from this I gave you score too.

  • 2

    @THIAGODEBONIS Someone rightly rejected the edition. I wouldn’t make conditional operator in this case, what I tried to write in my reply (so I rejected it) would make it wrong (at least in the secondary point you added), and the answer would have your text style and not mine, It doesn’t make sense for you to impose your style on other people’s text,s I hope you’re not doing this with other texts. You can fix formatting, grammar, spelling, typos, organize something that is very confusing, but cannot change the central content q the person wrote, interpret what the person said

  • I hope he didn’t use a global variable to avoid having a parameter.

Browser other questions tagged

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