Advertisement
campos20

Untitled

May 20th, 2020
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define MAXIMO 100
  6.  
  7. // Gera numeros aleatorios
  8. // Autor Alexandre Campos
  9.  
  10. // Gera 1 numero aleatorio
  11. int numero_aleatorio(){
  12.     return random() % MAXIMO;
  13. }
  14.  
  15. int * gera_numeros_aleatorios(int n){
  16.  
  17.     // Aloca n numeros inteiros na memoria
  18.     int * resultado = malloc(n * sizeof(int));
  19.  
  20.     // Atribuicao de valores
  21.     for (int i=0; i<n; i++){
  22.         resultado[i] = numero_aleatorio();
  23.     }
  24.  
  25.     return resultado;
  26. }
  27.  
  28. int main()
  29. {
  30.     // Chamar 1 vez
  31.     srand(time(NULL));
  32.  
  33.     // Declaracao
  34.     int n = 30;
  35.     int * numeros_aleatorios;
  36.  
  37.     // Chamada da funcao que gera numeros aleatorios
  38.     numeros_aleatorios = gera_numeros_aleatorios(n);
  39.  
  40.     // Exibir esses numeros
  41.     for (int i=0; i<n; i++){
  42.         printf("Posicao %d: %d\n", i, numeros_aleatorios[i]);
  43.     }
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement