Kimossab

Exemplo Exame-0 IP

Jan 9th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. //Exercicio 1
  2. //
  3. //i x   j
  4. //0 10  0
  5. //3 10  0
  6. //6 10  2
  7. //...   ... ...
  8. //15    10  8
  9. //15+1  10  8
  10. //
  11. //Resposta : 16
  12.  
  13. //Exercicio 2 a)
  14. typedef float Matriz
  15.  
  16. //Exercicio 2 b)
  17. bool MatrizSimetrica(Matriz *M, int nl, int nc)
  18. {
  19.         int i, j;
  20.         if(nl == nc)
  21.         {
  22.                 for(i=0; i<nl; i++)
  23.                         for(j = i+1; j<nc; j++)
  24.                                 if(*(M+i*nc+j) == *(M+j*nc+i))
  25.                                         return true;
  26.         }
  27.         return false;
  28. }
  29.  
  30. //Exercicio 3 a)
  31. typedef struct
  32. {
  33.     float altura;
  34.     float massa;
  35.     int genero; //eu usei bool primeiro, tipo 1 (true) = macho e 0 (false) = fémea xD mas esqueçam este comentário
  36.     struct
  37.     {
  38.         int ano;
  39.         int mes;
  40.         int dia;
  41.     } //pode-se fazer sem este struct, mas assim realça-se a data xD
  42. }Individuo;
  43.  
  44. //Exercicio 3 b)
  45. int Saudaveis(Individuo *vect_ind, int n_ind)
  46. {
  47.     float IMC=0;
  48.     int i, a=0;
  49.     for(i=0; i<n_ind; i++)
  50.     {
  51.         IMC = (vect_ind+i)->massa/(vect_ind+i)->altura * (vect_ind+i)->altura;
  52.         if(IMC >= 18.5 && IMC <= 24.9)
  53.             a++;
  54.     }
  55.     return a;
  56. }
  57.  
  58. //Exercicio 3 c)
  59. float MediaSecPassado(Individuo *vect_ind, int n_ind)
  60. {
  61.     int i, a=0;
  62.     float massasoma;
  63.     for(i=0; i<n_ind; i++);
  64.         if((vect_ind+i)->ano < 2000)
  65.         {
  66.             massasoma = (vect_ind+i)->massa;
  67.             a++;
  68.         }
  69.     return massasoma/a;
  70. }
  71.  
  72. //Exercicio 4 a)
  73. float SomaIt(int N)
  74. {
  75.     int i=1, a=1;
  76.     long fact;
  77.     float soma=0;
  78.     for(i; i<=N; i++)
  79.     {
  80.         fact=a*i;
  81.         a=fact;
  82.         soma += (1.0f/fact)+fact;
  83.     }
  84.     return soma;
  85. }
  86.  
  87. //Exercicio 4 b)
  88. float SomaRec(int N, int i)
  89. {
  90.     long a;
  91.     long fact = 1;
  92.     for(a=i; a>0; a--)
  93.         fact=fact*a;
  94.     if(i < N)
  95.         return (1.0f/fact)+fact+SomaRec(N, ++i);
  96.     else return (1.0f/fact)+fact;
  97. }
  98.  
  99. //Exercicio 5
  100. void EscreveDivisores(int N)
  101. {
  102.     int i;
  103.     printf("Divisores:\n");
  104.     for(i=1.0f; i<=N; i++)
  105.         if(N%i == 0)
  106.             printf("%d\t", i);
  107.     printf("\n");
  108. }
  109.  
  110. //Exercicio 6
  111. #include <stdio.h>
  112. #include <malloc.h>
  113.  
  114. int Fib(int N)
  115. {
  116.     if(N == 0 || N == 1)
  117.         return N;
  118.     else return Fib(N-1)+Fib(N-2);
  119. }
  120.  
  121. void LerVector(int *Vec, int n)
  122. {
  123.     int i;
  124.     for(i = 0; i<n; i++)
  125.     {
  126.         printf("Qual o valor da posicao [%d]:", i);
  127.         scanf("%d", Vec++);
  128.     }
  129. }
  130.  
  131. int *AlocarVector(int n)
  132. {
  133.     int*A;
  134.     A = (int*)malloc(n*sizeof(int));
  135.     if(A == NULL)
  136.     {
  137.         printf("\nMemoria Insuficiente.");
  138.         return 0;
  139.     }
  140.     return A;
  141. }
  142.  
  143. void main()
  144. {
  145.     int *VecA, n, i;
  146.     printf("Diga o numero de lelementos:");
  147.     scanf("%d", &n);
  148.     VecA = AlocarVector(n);
  149.     LerVector(VecA, n);
  150.     for(i=0;i<n;i++)
  151.         printf("%d\t", Fib(*(VecA++)));
  152. }
Advertisement
Add Comment
Please, Sign In to add comment