Jump to content

[Problema] Verificar se um numero existe num vector


JFsat

Recommended Posts

Olá a todos,este vai ser o meu primeiro post, e logo a começar com uma dúvida 🙂

é o seguinte eu quero fazer uma função para verificar se um numero está num vector,tentei fazer com este código

int numero_existe(int chave[6], int numero) 
{
  int i;
    for (i=0 ,i<6,i++) {
        if ( chave(i)=numero) 
        return 1
        else
        return 0 }
        
        }

para ver se um determinado numero está numa chave de totoloto.

se alguem puder dar uma dica!

cumps

Link to comment
Share on other sites

int numero_existe(int chave[6], int numero) 

{
  int i;
    for (i=0 ;i<6;i++) {
        if ( chave[i] == numero) 
        return 1
        else
        return 0 
     }
        
}

Tinhas dois pequenos erros, as chavetas em vez dos [] e o igual que tem de ser dois símbolos (==).

Para testes de igualdade tens de por dois == , para atribuição usas um.

EDIT: Nos ciclos for, é ; em vez de , 🙂

"Nunca discutas com um idiota. Eles arrastam-te até ao seu nível e depois ganham-te em experiência"

Link to comment
Share on other sites

Muito obrigado! agora testando a função o problema vem daqui :

main ()
{
     
     printf("Introduza o  número:");
     scanf ("%d",numero);
     if (numero_existe(chave(6),numero) == 1) 
     printf("o numero existe"\n);
     else
     printf("o numero existe"\n);
     
     
     }

quual será o problema?

Link to comment
Share on other sites

Desculpem, como o erro vinha daí só pus essa parte do código :s

#include <stdio.h>


int numero_existe(int chave[6], int numero) ; /* declaração da variavel*/

int numero_existe(int chave[6], int numero) 

{
  int i;
    for (i=0;i<6;i++) {
        if ( chave[i] == numero) 
        return 1;
        else 
        return 0; 
        }
        
        }
        
        
int numero;
int chave[6] = {1,2,3,4,5,6};

main ()

{
     
     printf("Introduza o  número:");
     scanf ("%d",numero);
     if (numero_existe(chave[6],numero) == 1) 
     printf("o numero existe"\n);
     else
     printf("o numero existe"\n);
     
     
     }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.