Equal elements between vectors - C

Asked

Viewed 460 times

1

I’ve been trying to solve a problem since early on:

A school wants to know if there are students attending simultaneously disciplines Logic and and Programming Language. Put the numbers of the enrollments of students who study Logic in a vector, at most of 5 students. Enter the enrollment numbers of the students who attend Language in another vector, maximum of 5 students. Show the number of number plate that appears on the two vectors.

It’s even running but when I put numbers out of order it’s bugging. If you can help me thank you, because I am breaking my head on this issue and it is holding me to finish the rest of the others, seems to be lazy to think, but I am early caught in this issue.

Thank you in advance

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

int i, a;
int mat1[6], mat2[6];

int main(void){

    setlocale(LC_ALL, "Portuguese");

    for(i=0 ; i<5; i++){

        printf("DIGITE AS MATRÍCULAS DOS ALUNOS DE LÓGICA DE PROGRAMAÇÃO: ");
        scanf("%d", &mat1[i]);
    }

    printf("\n");

    for(i=0; i<5; i++){

        printf("DIGITE AS MATRÍCULAS DOS ALUNOS DE LINGUAGEM DE PROGRAMAÇÃO: ");
        scanf("%d", &mat2[i]);
    }

    printf("\n");

    for(i=0; i<5; i++){

        for(a=5; a>=0; a--)
        {
            if(mat1[i] == mat2[a] || mat1[a] == mat2[i]) printf("AS MATRÍCULAS IGUAIS SÃO: %d\n", mat1[i]);
        }
    }

return 0;
}

Running the program:

inserir a descrição da imagem aqui

  • you need to "sort" or "sort" the arrays before making the comparison...how to make the comparison ? there is a library function called "qsort" that does this...if you find it very difficult to use "qsort", do the sorting manually, using for example the algorithm "Bubble Sort"...and where to find information about "qsort" and "Bubble Sort"? google...

1 answer

1


You have several small errors in your code, which make it impossible for you to get the result you expected. Note that in the second for who thinks the repeated enrollment the amount of elements is not the same:

for(i=0; i<5; i++){    
//    ^----^ de 0 a 4 são 5 elementos
    for(a=5; a>=0; a--)
    //    ^-----^--- de 5 a 0 são 6 elementos

Here you see that a for runs through 5 elements and another 6. In addition to accessing 6 elements you are accessing more elements than you filled in, and only you are not doing an invalid access in memory because you declared the vectors with 6 houses, when you only needed 5. This is also something you should avoid doing.

You also have no need to travel in a case with i++ and another decreasing with a--. Simpler is to make both normally of 0 to 4.

The condition you have if(mat1[i] == mat2[a] || mat1[a] == mat2[i]) also not what you need because you only need to validate if the element given by the first for in the vector mat1 is the same as in the vector mat2 with the variable of the second for, that is to say, if(mat1[i] == mat2[a]).

Correcting the block of fors final result already gives the result you expect:

for(i=0; i<5; i++){
    for(a=0; a<5; a++){
        if(mat1[i] == mat2[a]) printf("AS MATRÍCULAS IGUAIS SÃO: %d\n", mat1[i]);
    }
}

Watch it work on Ideone

  • Thank you so much for the help friend, I’m breaking my head on this part of vectors, even Rigadão.

Browser other questions tagged

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