Can anyone tell me how to do this program in C to display the vector in which I typed the one number that was stored in it?

Asked

Viewed 40 times

-1

#include <stdio.h>

int main()
{
       
float vet[6];
int i, x;

    for (i=0;i<6;i++){
    scanf("%f", &vet[i]);
    }
    for(x=0;x<1;x++){
    scanf("%d",&x);
    if(x<=vet[5]){
    printf("Numero %d encontrado na posicao %.0f", x, vet[i]);
    }
    else
    {
      printf("Posicao do vetor nao encontrada!");  
    }
    }
    return 0;
}
  • This: for(x=0;x<1;x++){ scanf("%d",&x); if(x<=vet[5]){ is wrong, must be: scanf("%d",&x); i=0; while (i<6 && x != vet[i]) {i++;} if (x <= 5) {.

2 answers

0

#include <stdio.h>

main() {

    int i, x, posicao = 0;
    float vet[6]; 
    
    for (i=0; i<6; i++){
        
        printf("%da. posicao: ", i+1);
        scanf("%f", &vet[i]);
    
    }
    
    printf("\nDigite um numero: ");
    scanf("%d", &x);
    
    for(i=0; i<6; i++) {
        
        if(x == vet[i])  {
            
            posicao = i;
            
            if (posicao == 0){
            
                posicao = 1;
                printf("\nO numero %d esta na posicao %d do vetor.", x, posicao);
            
            } else {
                
                printf("\nO numero %d esta na posicao %d do vetor.", x, posicao+1);
                
            }
            
        }
        
    }
    
    if (posicao == 0) {  
            
        printf("\nO numero %d nao existe no vetor.", x);
        
    }
    
}

-1

#include <stdio.h>

main() {

int i, x, posicao = 0;
float vet[6]; 

for (i=0; i<6; i++){
    
    printf("%da. posicao: ", i+1);
    scanf("%f", &vet[i]);

}

printf("\nDigite um numero: ");
scanf("%d", &x);

for(i=0; i<6; i++) {
    
    if(x == vet[i]) {
        
        posicao = i;
        
    }

}

if (posicao) {  
    
    printf("\nO numero %d esta na posicao %d do vetor.", x, posicao+1);
    
} else {
    
    printf("\nO numero %d nao existe no vetor.", x);
    
}

}

  • There is an error there. Note that if the searched element is the first of the vector (index 0) you will inform that it does not exist in the vector.

  • because it is it does not find the vector 0

Browser other questions tagged

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