fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5. int aleatorio20();
  6. void memory_check(int *aps, int cant);
  7. void iniciar_arre_pares(int *arre, int cant);
  8. void imprime_arreglo(int *arre, int cant);
  9.  
  10. int main()
  11. {
  12. int cant;
  13. int *aps=0;
  14.  
  15. printf("Creamos el numero aleatorio que dara el valor a 'cant'.\n");
  16. cant = aleatorio20();
  17. printf("Cant: %d\n", cant);
  18.  
  19. printf("Creamos el espacio en la memoria para el arreglo.\n");
  20. memory_check(&aps, cant);
  21.  
  22. printf("Llenamos el arreglo con los pares correspondientes.\n");
  23. iniciar_arre_pares(&aps, cant);
  24.  
  25. printf("Imprimimos el arreglo para demostrar el contenido\n");
  26. imprime_arreglo(&aps, cant);
  27.  
  28. return 0;
  29. }
  30.  
  31. int aleatorio20(){
  32.  
  33. int x;
  34. srand(time(NULL));
  35. x = rand() % 20 + 1;
  36. return x;
  37. }
  38.  
  39. void memory_check(int *aps, int cant){
  40. if( (*aps = malloc(sizeof(int)*cant)) == 0){
  41.  
  42. printf("¡No hay memoria!\n");
  43. exit(-1);
  44. }
  45. printf("Memoria creada con exito.\n");
  46. return;
  47. }
  48.  
  49. void iniciar_arre_pares(int *arre, int cant){
  50.  
  51. int x = 0;
  52. for(int i=0; i<cant; i++){
  53. //printf("El lugar %d se llena con el numero %d\n", i, x);
  54. *(arre+i) = x;
  55. x = x + 2;
  56. }
  57. return;
  58. }
  59.  
  60. void imprime_arreglo(int *arre, int cant){
  61.  
  62. printf("El arreglo es: ");
  63.  
  64. for(int i=0; i<cant; i++){
  65. printf("%d ", *(arre+i));
  66. }
  67. printf("\n");
  68. }
  69.  
  70.  
  71.  
  72.  
Success #stdin #stdout 0s 4444KB
stdin
Standard input is empty
stdout
Creamos el numero aleatorio que dara el valor a 'cant'.
Cant: 2
Creamos el espacio en la memoria para el arreglo.
Memoria creada con exito.
Llenamos el arreglo con los pares correspondientes.
Imprimimos el arreglo para demostrar el contenido
El arreglo es: 0 2