Vector returns address at the time of "printar"

-2
* Código antigo    
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
main(){
    setlocale(LC_ALL, "portuguese");
    int n1[4], n2[4], ni[4], nd[4];
    printf("1° pessoa\n");
    for(int i=0; i<=4;i++){
        printf("Digite o %d número: ", i+1);
        scanf("%d", &n1[i]);
    }
    printf("2° pessoa\n");
    for(int i=0; i<=4;i++){
        printf("Digite o %d número: ", i+1);
        scanf("%d", &n2[i]);
    }
    for(int i2=0; i2<=4; i2++){
        for(int i=0; i<=4;i++)
        if(n1[i]==n2[i2]){
            ni[i]=n1[i];
        }else if(!(n1[i]==n2[i2])){
            nd[i]=n1[i];
        }
    }
    printf("Números iguais: \n");
    for(int i=0; i<=4;i++){
        printf("%d\n", ni[i]);
    }
    for(int i=0; i<=4;i++){
        printf("%d\n", nd[i]);
    }
    system("pause");
}

*NewCode

include

include

include

main(){    setlocale(LC_ALL,"Portuguese");     int n1 [5] = {0}, n2 [5] = {0}, ni [5] = {0}, nd [10] = {0};     / * Data entry * /     printf ("1st person \ n");     for (int i = 0; i

asked by anonymous 30.10.2018 / 02:48

1 answer

0

A friend, so I understand you wanted to return which numbers entered by the 1st and 2nd person would be the same? I made a change in the for to go comparing with the value of each vector, I'm still a beginner but the first way I came to a solution for your problem was thus, you were also using the for with < = 4, the correct would be & lt ; 4, I used only one vector to hold the same positions.

#include<stdio.h>#include<stdlib.h>#include<locale.h>main(){setlocale(LC_ALL,"portuguese");

 int n1[4], n2[4], ni[4], y=0;


printf("1° pessoa\n");
for(int i=0; i < 4;i++){
    printf("Digite o %d número: ", i+1);
    scanf("%d", &n1[i]);
}
printf("2° pessoa\n");
for(int i=0; i < 4;i++){
    printf("Digite o %d número: ", i+1);
    scanf("%d", &n2[i]);
}

for(int i=0; i < 4; i++){
    if(n1[i] == n2[0]){
        ni[y] = n1[i];
        y++;    
    }
}
for(int i=0; i < 4; i++){
    if(n1[i] == n2[1]){
        ni[y] = n1[i];
        y++;
    }
}
    for(int i=0; i < 4; i++){
    if(n1[i] == n2[2]){
        ni[y] = n1[i];
        y++;
    }
}
    for(int i=0; i < 4; i++){
    if(n1[i] == n2[3]){
        ni[y] = n1[i];
        y++;
    }
}


printf("\n\nExistem %d números iguais", y);
for(int i=0; i < y; i++){
    printf("\n\n%d", ni[i]);
}

}

    
30.10.2018 / 20:38