Jump to content

[duvida]Exercicio de exame


Recommended Posts

Posted

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;


}
Posted

Qual é o erro que te dá?

Oracle Certified Professional - AdministraçãoOracle Certified Professional - Pl/sqlMCPD - Microsoft Certified Professional DeveloperMCTS - Microsoft Certified Technology Specialist

Posted

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

Posted (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 by Guest
Posted

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

Posted

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.

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.