Guest User

Untitled

a guest
Jun 24th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define size 5
  6.  
  7.  
  8. //lĂŞ array
  9. int readVet (int tab[], int tam) {
  10. int c;
  11. for (c = 0; c < tam; c++) {
  12. printf("Vet[%d] > ", c);
  13. scanf("%d", &tab[c]);
  14. }
  15. return 0;
  16. }
  17.  
  18. //faz soma da array
  19. int sumVet (int tab[], int tam) {
  20. int c;
  21. int sum = 0;
  22.  
  23. for (c = 0; c < tam; c++) {
  24. sum += tab[c];
  25. }
  26. return sum;
  27. }
  28.  
  29. //bubble sort da array
  30. int bSort (int tab[], int tam) {
  31. int c;
  32. int aux;
  33.  
  34. int troca = 1;
  35. while (troca != 0) {
  36. troca=0;
  37. for (c = 0; c < tam; c++) {
  38. if (tab[c] > tab[c+1]) {
  39. aux = tab[c];
  40. tab[c] = tab[c+1];
  41. tab[c+1] = aux;
  42. troca=1;
  43. }
  44. }
  45. }
  46. return 0;
  47. }
  48.  
  49. //imprime array
  50. int impressao (int tab[], int tam) {
  51. int c;
  52. for (c = 0; c < tam; c++) {
  53. printf("%d ", tab[c]);
  54. }
  55. }
  56.  
  57. //fatorial
  58. int fat (int n) {
  59. int mult;
  60.  
  61. int fat = 1;
  62. for (mult = 1; mult <= n; mult++) {
  63. fat *= mult;
  64. }
  65. return fat;
  66. }
  67.  
  68. //strlength da vida
  69. int tamStr(char word[]) {
  70. int tam = 0;
  71.  
  72. while (word[tam] != '\0') {
  73. tam++;
  74. }
  75.  
  76. return tam;
  77. }
  78.  
  79. //calcula quadrado
  80. int sqrPwr (int n) {
  81. return n*n;
  82. }
  83.  
  84. //macro -- calcula quadrado
  85. #define sqrPwr2(n) n*n
  86.  
  87. //calcula quadrado como inline
  88. inline int sqrPwr3 (int n) {
  89. return n*n;
  90. }
  91.  
  92. int main (void) {
  93. // int vet[size];
  94. int numbah, numbah2;
  95. time_t ti, tf;
  96. int mac, fun, inl;
  97. long int i;
  98.  
  99. /*
  100. //atencao, estou alocando "minha string" para um PONTEIRO!
  101. char *str = "minha string";
  102.  
  103.  
  104. readVet(vet, size);
  105. printf("\nA soma do vet que voce entrou vale %d.", sumVet(vet, size));
  106.  
  107. printf("\n\nA vet atual esta assim:\n");
  108. impressao(vet, size);
  109.  
  110. printf("\nOrdenando agora...\n");
  111. bSort(vet, size);
  112. impressao(vet, size);
  113.  
  114. printf("\n\nEntre um numero > ");
  115. scanf("%d", &numbah);
  116. printf("O fatorial de %d eh %d.", numbah, fat(numbah));
  117.  
  118. // printf("\n\nEntre uma string > ");
  119. // fflush(stdin);
  120. // scanf("%[^\n]", str);
  121.  
  122. printf("\n\nO tamanho de '%s' vale %d.", str, tamStr(str));
  123. */
  124. printf("\n\nEntre um numero > ");
  125. scanf("%d", &numbah2);
  126.  
  127. //funcao
  128. ti = time(NULL);
  129. for (i = 0; i < 1000000000L; i++) {
  130. fun = sqrPwr(numbah2);
  131. }
  132. tf = time(NULL);
  133. printf("O quadrado de %d vale %d.\nSeu runtime por funca foi de %d.", numbah2, fun, tf-ti);
  134.  
  135. //macro
  136. ti = time(NULL);
  137. for (i = 0; i < 1000000000L; i++) {
  138. mac = sqrPwr2(numbah2);
  139. }
  140. tf = time(NULL);
  141. printf("\n\nO quadrado de %d vale %d.\nSeu runtime por macro foi de %d.", numbah2, mac, tf-ti);
  142.  
  143. //inline
  144. ti = time(NULL);
  145. for (i = 0; i < 1000000000L; i++) {
  146. inl = sqrPwr3(numbah2);
  147. }
  148. tf = time(NULL);
  149. printf("\n\nO quadrado de %d vale %d.\nSeu runtime por inlin foi de %d.", numbah2, inl, tf-ti);
  150.  
  151.  
  152. printf("\n\n");
  153. system("pause");
  154.  
  155. return 0;
  156. }
Add Comment
Please, Sign In to add comment