Advertisement
FoxTuGa

M e M, structs [Exercicio]

Oct 11th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. // Leandro Soares, i100067
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6.  
  7. #define MaxNotas 19
  8. #define TRUE 1
  9.  
  10. struct tagAlunos {
  11.     char nome[128];
  12.     int numero;
  13.     int notaspsi[MaxNotas];
  14. };
  15.  
  16. void IniArray(int arry[], int Max, int x);
  17. void LerNotas(int arry[], int Max);
  18. int FMedia(int arry[], int Max);
  19. int FMediana(int arry[], int Max);
  20. void cpyarray(int arry_dest[], int arry_org[], int Max);
  21. int compfunc(const void * a, const void * b);
  22.  
  23. int main() {
  24.     int quant, Media, Mediana;
  25.     struct tagAlunos Aluno;
  26.  
  27.     quant = Media = Mediana = 0;
  28.     IniArray(Aluno.notaspsi, MaxNotas, -1);
  29.  
  30.     printf("\nNome: ");
  31.     gets(Aluno.nome);
  32.     system("cls");
  33.  
  34.     printf("\nNumero: ");
  35.     scanf("%d", &Aluno.numero);
  36.     system("cls");
  37.  
  38.     printf("\nIntroduza a quantidade de modulos: ");
  39.     scanf("%d", &quant);
  40.     system("cls");
  41.  
  42.     if(quant > 0 && quant < 20) {
  43.         LerNotas(Aluno.notaspsi, quant);
  44.         Media = FMedia(Aluno.notaspsi, MaxNotas);
  45.         Mediana = FMediana(Aluno.notaspsi, quant);
  46.     }
  47.  
  48.     system("cls");
  49.  
  50.     printf("Nome: %d", Aluno.nome);
  51.     printf("\nNumero: %d", Aluno.numero);
  52.     printf("\nMedia: %d", Media);
  53.     printf("\nMediana: %d", Mediana);
  54.     printf("\n\n");
  55.  
  56.     return 0;
  57. }
  58.  
  59. void IniArray(int arry[], int Max, int x) {
  60.     int idx;
  61.     for(idx=0; idx != Max; idx++) {
  62.         arry[idx] = x;
  63.     }
  64. }
  65.  
  66. void LerNotas(int arry[], int Max) {
  67.     int i;
  68.     while(Max-- != 0) {
  69.         system("cls");
  70.         printf("\nModulo: ");
  71.         scanf("%d", &i);
  72.  
  73.         if( i > 0 && i < 20 ) {
  74.             while(TRUE) {
  75.                 printf("\nNota: ");
  76.                 scanf("%d", &arry[i-1]);
  77.                 if( arry[i-1] >= 0 && arry[i-1] <= 20 )
  78.                     break;
  79.                 else {
  80.                     Max++;
  81.                     printf("\n ERRO (clique numa tecla para voltar a intruduzir)\n");
  82.                     getch();
  83.                 }
  84.             }
  85.         }
  86.         else {
  87.             Max++;
  88.             printf("\n ERRO (clique numa tecla para voltar a intruduzir)\n");
  89.             getch();
  90.         }
  91.         system("cls");
  92.     }
  93.  
  94. }
  95.  
  96. int FMedia(int arry[], int Max) {
  97.     int Media, idx, num;
  98.     Media = num = 0;
  99.  
  100.     for(idx = 0; idx < Max; idx++) {
  101.         if( arry[idx] != -1 ) {
  102.             num++;
  103.             Media += arry[idx];
  104.         }
  105.     }
  106.  
  107.     if(Media != 0)
  108.         Media /= num;
  109.     else
  110.         Media = -1;
  111.     return Media;
  112. }
  113.  
  114. int FMediana(int arry[], int Max) {
  115.     int arry2[MaxNotas];
  116.     int Mediana, idx;
  117.     cpyarray(arry2, arry, MaxNotas);
  118.     qsort(arry2, MaxNotas, sizeof(int), compfunc);
  119.  
  120.     if( Max % 2 == 0 )
  121.         Mediana = ( arry2[(Max/2)-1] + arry2[(Max/2)] ) / 2;
  122.     else
  123.         Mediana = arry2[Max/2];
  124.  
  125.     return Mediana;
  126. }
  127.  
  128. void cpyarray(int arry_dest[], int arry_org[], int Max) {
  129.     int idx;
  130.     for(idx = 0; idx < Max; idx++) {
  131.         arry_dest[idx] = arry_org[idx];
  132.     }
  133. }
  134.  
  135. int compfunc(const void * a, const void * b) {
  136.     int *x = (int*) a;
  137.     int *y = (int*) b;
  138.  
  139.     return *y - *x;
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement