mundo Posted July 8, 2012 Report Share Posted July 8, 2012 Bom dia, estou aqui a resolver um teste e nao estou a conseguir fazer um exercicio, envolvendo uma struct. tenho que fazer uma função int maisFreq(Pauta p, int n) que calcula a nota mais frequente entre os n primeiros alunos da lista p. O codigo que tenho é o seguinte, com bastantes asneiras, mas nao estou a conseguir resolver, se alguem me puder ajudar agradecia: #include <stdio.h> #include <stdlib.h> #define MAX 300 typedef struct linha{ int numero; char nome[100]; int nota; }Pauta[MAX]; int contaElem(Pauta p[MAX], int n){ int x; //contador int i,j;//indices para percorrer o array int flag; //determina se foi encontrado for(i = 0, j = i+1; i < (n-1); i++, j++){ if(p->nota[i] == p->nota[j]){ if(flag == 0){ x++; flag = 1; } } else{ flag = 0; } } return x; } int maisFreq(Pauta p[MAX], int n){ int t, flag; int *moda; //sera o que retornará o valor da nota mais frequente int i, j, k; t = contaElem(p,n); moda = (int *) malloc (t * sizeof(Pauta)); k = flag = 0; for(i = 0, j = i+1; i < (n-1); i++, j++){ if(p->nota[i] == p->nota[j]){ if(flag == 0){ moda[k++] = p->nota[j]; flag = 1; } } else{ flag = 0; } } return moda; } Link to comment Share on other sites More sharing options...
renafi Posted July 8, 2012 Report Share Posted July 8, 2012 Qual é o erro que te dá? Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist Link to comment Share on other sites More sharing options...
mundo Posted July 8, 2012 Author Report Share Posted July 8, 2012 In function 'contaElem': ex1.c:25: error: request for member 'nota' in something not a structure or union ex1.c:25: error: request for member 'nota' in something not a structure or union ex1.c: In function 'maisFreq': ex1.c:50: error: request for member 'nota' in something not a structure or union ex1.c:50: error: request for member 'nota' in something not a structure or union ex1.c:52: error: request for member 'nota' in something not a structure or union ex1.c:60: warning: return makes integer from pointer without a cast Link to comment Share on other sites More sharing options...
Guest Posted July 8, 2012 Report Share Posted July 8, 2012 (edited) if(p->nota[i] == p->nota[j]){ typedef struct linha{ int numero; char nome[100]; int nota; }Pauta[MAX]; Como podes ver nota não é um array de inteiros mas sim somente um inteiro. Edited July 8, 2012 by Guest Link to comment Share on other sites More sharing options...
mundo Posted July 8, 2012 Author Report Share Posted July 8, 2012 Sim mas como vou diferenciar as notas entao? Link to comment Share on other sites More sharing options...
Guest Posted July 8, 2012 Report Share Posted July 8, 2012 Não percebi a tua pergunta, mas tu tens 1 nota pot linha por isso se quiseres mais notas por linha tens que criar um array de int's Link to comment Share on other sites More sharing options...
Happening Posted July 8, 2012 Report Share Posted July 8, 2012 Sugestão: não uses os teus for's para ver se a nota existe. já uqe crias-te uma struct usa-a para ires dessa struct para a próxima e assim sucessivamente até o teu apontador te apontar para null. O apontador estará na própria struct. Link to comment Share on other sites More sharing options...
Rui Carlos Posted July 8, 2012 Report Share Posted July 8, 2012 Possivelmente o que pretendes é p->nota, e não p->nota. Rui Carlos Gonçalves 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