Giving Segmentation Failure error (recorded core image)

Asked

Viewed 4,711 times

0

Exercise = Perform a procedure that receives, per parameter, 2 vectors of 10 elements integers and that calculates and returns, also by parameter, the intersection vector of the two first.

My code:

#include <stdio.h>
#include <stdlib.h>
#define TAM 10

void interseccao(int vetor1[], int vetor2[], int vetor_interseccao[], int *x) {

int i, j;
int aux[50], x_aux = -1;


for (i = 0; i < TAM; i++) {
    for (j = 0; j < TAM; j++) {
        if (vetor1[i] == vetor2[j]) {
            x_aux++;
            aux[x_aux] = vetor1[i];
        }
    }
}


if (x_aux != -1) {
    x_aux++;
    for (i = x_aux-1; i > - 1; i--) {
        for (j = 0; i < x_aux; j++) {
            if (i != j) {
                if (aux[i] == aux[j]) {
                    aux[i] = 0;
                }
            }
        }
    }

    for (i = 0; i < x_aux; i++) {
        if (aux[i] != 0) {
            *x = *x +1;
            vetor_interseccao[*x] = aux[i];
        }
    }

}

}
int main () {

    int vetor1[TAM], vetor2[TAM], vetor_interseccao[TAM];
    int cont, x = -1;

    for (cont = 0; cont < TAM; cont++) {
        printf("Informe o valor do vetor1[%d]: ",cont+1);
        scanf("%d",&vetor1[cont]);
    }

    system("clear");

    for (cont = 0; cont < TAM; cont++) {
        printf("Informe o valor do vetor2[%d]: ",cont+1);
        scanf("%d",&vetor2[cont]);
    }

    system("clear");

    printf("---------------------------------------------------\n");
    printf("Valores do vetor1:\n\n");
    for (cont = 0; cont < TAM; cont++) {
        printf("vetor1[%d] = %d\n",cont+1,vetor1[cont]);
    }

    printf("\n---------------------------------------------------\n");
    printf("Valores do vetor2:\n\n");
    for (cont = 0; cont < TAM; cont++) {
        printf("vetor2[%d] = %d\n",cont+1,vetor2[cont]);
    }

    interseccao(vetor1,vetor2,vetor_interseccao,&x);
    printf("\n---------------------------------------------------\n");
    printf("Valores do vetor_interseccao:\n\n");
    if (x != -1) {
        x++;
        for (cont = 0; cont < x; cont++) {
            printf("vetor_interseccao[%d] = %d\n",cont+1,vetor_interseccao[cont]);
        }
    } else {
        printf("Não há valores de intersecção entre o vetor1 e o vetor2.\n");
    }

    printf("\n");

    return 0;
}
  • Dude, are you doing this on linux? This is usually a permission error. This has already happened to me trying to do certain things by the terminal on the stage pc.

  • What do you mean, man? 'Cause there’s this mistake ?

  • If you are doing this on a computer that is not the owner, it may be a permission error to run what you want. Do you understand? I’m basing myself on my experience with this mistake.

1 answer

0


Good afternoon,

"Debugging" your code I noticed a silly error in a loop within the interseccal procedure where you switched the comparison variable from "J" to "I" by mistake. Follow the corrected code snippet:

for (i = x_aux-1; i > - 1; i--) {
    for (j = 0; j < x_aux; j++) { //A condição estava i < x_aux
        if (i != j) {
            if (aux[i] == aux[j]) {
                aux[i] = 0;
            }
        }
    }
}

I hope I’ve solved your problem.

Yours sincerely, Arthur Passos.

Browser other questions tagged

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