fork download
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. const int MAX_N = 100;
  5.  
  6. // returns a random number x, where a <= x <= b
  7. // srand(time(NULL)) must be called before using this function
  8. int random(int a, int b){
  9. return (rand() % (b - a + 1)) + a;
  10. }
  11. int main() {
  12. srand(time(NULL)); // initalizing PRNG
  13. int n = 5;
  14. int m = 5;
  15. int a[MAX_N][MAX_N];
  16. // assign each number of the matrix with a random number between 1 and 100
  17. for(int i = 0; i < n; i ++){
  18. for(int j = 0; j < m; j++){
  19. a[i][j] = random(1, 100);
  20. }
  21. }
  22. // print the matrix for visualisation
  23. for(int i = 0; i < n; i++){
  24. for(int j = 0; j < m; j++){
  25. printf("%d ", a[i][j]);
  26. }
  27. printf("\n");
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
74 44 27 28 59 
86 97 67 97 78 
86 36 33 90 86 
30 100 18 8 32 
14 51 5 61 25