Advertisement
Kimossab

IP - Freq 2015

Jan 21st, 2015
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. //1
  2. //for(int c=0; c<3; c++) ???
  3. //c     i
  4. //0     -
  5. //0     0
  6. //0     1 (++i) < 4
  7. //0     2 < 4
  8. //0     3 < 4
  9. //0     4 < 4 (sai do ciclo)
  10. //esta parte é sempre executada deste modo para todo o valor de c
  11. //O que aparece no ecra:
  12. //i = 4 -> c = 0
  13. //i = 4 -> c = 1
  14. //i = 4 -> c = 2
  15.  
  16. //2
  17. void EscreveDivisores(int N)
  18. {
  19.     printf("Divisores de %d\n", N);
  20.     for(int i=N; i>0; i--)
  21.     {
  22.         if(N%i == 0)
  23.             printf("[%i]", i);
  24.     }
  25. }
  26.  
  27. //3
  28. bool VetoresIguais(int a[], int tama, int b[], int tamb)
  29. {
  30.     if(tama != tamb)
  31.         return false;
  32.     for(int i=0; i<tama; i++)
  33.         if(a[i] != b[i])
  34.             return false;
  35.     return true;
  36. }
  37.  
  38. //4
  39. //a
  40. //posicao = 3
  41.  
  42. //b
  43. //o parametro n é o numero de elementos no vetor (array), chave é o valor que procuramos no vetor e *t é o vetor.
  44. //o programa começa por criar uma variavel local i com o valor 0 que corresponde à primeira "célula" do vetor.
  45. //com um ciclo while que verifica se o valor dessa célula é diferente da chave e se i é menor ou igual a n-1 (ou seja se i está dentro do tamanho do vetor)
  46. //se se verificar então incrementa o i em 1
  47. //se nao se verificar entao verificar entao vê se o i ainda pertence ao tamanho do vetor, se nao pertencer quer dizer que a chave nao foi encontrada, logo retorna -1
  48. //se o i pertencer ao tamanho do vetor a função retorna o numero da celula em que a chave foi encontrada (ou seja i)
  49.  
  50. //5
  51. //a
  52. typedef struct
  53. {
  54.     char matricula[9];
  55.     char marca[20];
  56.     char codigolugar[4];
  57.     int ano;
  58.     int situacao;
  59. }Camiao;
  60.  
  61. Camiao Cam[600];
  62.  
  63. //b
  64. float AvariasRenault()
  65. {
  66.     int total=0, av=0;
  67.     for(int i=0; i<600; i++)
  68.     {
  69.         if(!stricmp(Cam[i].marca,"Renault")) //se for igual ao renault
  70.         {
  71.             total++;
  72.             if(Cam[i].situacao == 2)
  73.                 av++;
  74.         }
  75.     }
  76.     return (float)total/(float)av;
  77. }
  78.  
  79. //6
  80. float Soma(int n)
  81. {
  82.     if(n==1) return 1.0/3.0;
  83.     return ((float)n/3.0) * Soma(n-1);
  84. }
  85. //7
  86. int ContaA(char *nome)
  87. {
  88.     FILE *f = fopen(nome,"r");
  89.     if(!f)
  90.         return 0;
  91.  
  92.     int A=0;
  93.     while(!feof(f))
  94.     {
  95.         if(fgetc(f) == 'A')
  96.             A++;
  97.     }
  98.     fclose(f);
  99.     return A;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement