JFsat Posted November 24, 2007 at 12:06 PM Report Share #149891 Posted November 24, 2007 at 12:06 PM #include <stdio.h> #define NUMEROS_TOTOLOTO 6 #define MIN_TOTOLOTO 1 #define MAX_TOTOLOTO 49 void limpa_chave (int chave[NUMEROS_TOTOLOTO]) { int i; for (i= 0; i<NUMEROS_TOTOLOTO; i++) chave[i]= 0; /* 0 é um número inválido que significa */ /* que a chave está livre */ } void escreve_chave(int chave[NUMEROS_TOTOLOTO]) { int i; for (i= 0; i<NUMEROS_TOTOLOTO; i++) printf("%s%d", (i==0 ? "" : ", "), chave[i]); } int le_numero(int min, int max) { int n= min-1; /* Número não válido */ do { if (scanf(" %d", &n) != 1) { /* Se falhou leitura de número retira um carácter */ getchar(); /* e tenta ler um inteiro novamente */ } } while ((n>max) || (n<min)); /* Se está fora de [min, max] */ return n; } int numero_existe(int chave[NUMEROS_TOTOLOTO], int numero) { int i; for ( i=0 ; i<NUMEROS_TOTOLOTO;i++); { if (chave[i] == numero) return 1; } return 0; } void le_uma_aposta(int aposta[NUMEROS_TOTOLOTO]) { limpa_chave(aposta); int i,numero; for (i=0 ;i<NUMEROS_TOTOLOTO;i++) { numero = scanf(" %d",&numero); aposta[i] = le_numero_unico(numero); } printf("Leu: "); escreve_chave(aposta); putchar('\n'); } int le_numero_unico(int chave[NUMEROS_TOTOLOTO], int min, int max) { int n; do { /* Repete até que o número não exista na chave */ n= le_numero(min, max); /* Lê número entre min e max */ } while (numero_existe(chave, n)); return n; } main() { int aposta[NUMEROS_TOTOLOTO]; int chave[NUMEROS_TOTOLOTO]; int acertou; printf("Introduza os %d números da sua aposta do totoloto\n", NUMEROS_TOTOLOTO); le_uma_aposta(aposta); } Ao correr o programa que supostamente só lê a aposta, so me aparece o printf para colocar os numeros, quando os coloco aparece : Segmentation Fault (core dumped), o erro deve tar na função le_uma_aposta.Mas não percebo qual é! cumps Link to comment Share on other sites More sharing options...
mogers Posted November 24, 2007 at 12:32 PM Report Share #149899 Posted November 24, 2007 at 12:32 PM usa a ferramente GeSHi para postar codigo: edit: o [i ] não aparecia... mesmo assim tás a chamar a função só com um parametro em vez de 3 ? O Rui Carlos colocou o post direito... para que é k voltaste a colocar isso assim ? :s "What we do for ourselves dies with us. What we do for others and the world, remains and is immortal.", Albert Pine Blog pessoal : contém alguns puzzles, algoritmos e problemas para se resolver com programação. Link to comment Share on other sites More sharing options...
JFsat Posted November 24, 2007 at 12:45 PM Author Report Share #149904 Posted November 24, 2007 at 12:45 PM Desculpem tive que alterar o código,e não reparei que já estava com a tal ferramenta! Pois,tambem nao me parece muito bem mas eu o que percebo e que esses parametros vao ser usados nas sub-funções,na função le_numero_unico ja necessitava de ter os dados tratados e apenas colocar um numero que iria ser "Trabalhado" pelas outras funções. Mas assim, le_numero_unico(chave,1,49) seria correcto? obrigado Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now