fork download
  1. /*
  2.  * main.c
  3.  *
  4.  * Created on: 20 Mar 2019
  5.  * Author: El
  6.  */
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<time.h>
  10.  
  11. void printArr(int arr[],int size);
  12. void fillRndArr(int arr[], int size, int min, int max);
  13.  
  14.  
  15. int main(){
  16. int arr[10];
  17.  
  18. fillRndArr(arr,10, 0, 1200);
  19. printArr(arr,10);
  20.  
  21.  
  22.  
  23. return 0;
  24. }
  25.  
  26. void fillRndArr(int arr[],int size, int min, int max){
  27. srand(time(NULL));
  28. int i;
  29. for(i = 0; i < size; i++){
  30. arr[i] = min + rand() % (max - min + 1);
  31. }
  32. }// if min and max are negative, result is also negative. For example min = -1200, max = - 200. arr [i] = - 1200 + rand % (-200 - (-1200) + 1) = - 1200 + rand % 1001
  33. // rand % 1001 is positive number, but -1200 + rand %1001 is negative, because |-1200| > rand%1001 => -1200 < rand%1001 => -1200 + rand%1001 < 0, it is negative.
  34. // if min is negative and max is positive. For example, min = 1200, max = 1200. arr [i] = -1200 + rand%(1200-(-1200) + 1) = -1200 + rand%2401. rand%2401 < 2401, rand 2401 > 0
  35. // Some of rand%2401 are more than |-1200|, some are less, so arr[i] = -1200 + rand%2401 is sometimes positive, sometimes negative.
  36.  
  37. void printArr(int arr[], int size){
  38. int i;
  39. for(i = 0; i < size;i++){
  40. printf("[%d]",arr[i]);
  41. }
  42. printf("\n");
  43. }
  44.  
  45.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[155][36][503][1136][832][312][128][1197][26][486]