Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a 20x20 matrix with random numbers between 1 and 100 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROWS 20
#define COLS 20

void printMatrix(int matrix[][COLS], int size) {
    for (int i = 0; i < size; i++) {  
        for (int j = 0; j < size; j++) {
            printf("%4d", matrix[i][j]);
        } 
        printf("\n");
    }
}
 
int generateRandomInteger(int min, int max) {
    return min + rand() / (RAND_MAX / (max - min + 1) + 1);
    // return rand() % (max - min + 1) + min;
}
     
void generateRandomMatrix(int matrix[][COLS], int size) {
    srand(time(NULL));
    
    for (int i = 0; i < size; i++) {     
        for (int j = 0; j < size; j++) {
            matrix[i][j] = generateRandomInteger(1, 100);
        }
    }
}

int main() {
    int matrix[ROWS][COLS] = {{ 0 }};
    
    generateRandomMatrix(matrix, ROWS);
       
    printMatrix(matrix, ROWS);
    
    return 0;
}




/*
run:
 
  93  77 100   7  57  23  65  96  58  73  42  55 100  22  50  48  28  16   2  37
  77  96  51  41  75  30  20  75  72  53  73  65  29  72  71  86  94  36  82  52
   9  23   6   8  44  56  56  72  71  57   8  47  53  58  87  27  87   7   2  59
  59  74  23  88  46  94  74  39  29  55  90  37  77  96  45  20  51   1  92  21
  57  99  68  10  57  54  37  44  61  39   2  19  13  25   7  58  19  80  96  48
  34  86  85  11  81  29  31  31  30  22  52  86  21  19  96  77  73  32  21  33
  70  23  52  83  47  58  40  66  38  36  13  71  21  97  81   2  26  12  33  55
  33  84  41  53   3  37  30  75  68  50   8  38  72  60  20  19  18  60  85  55
  95  98  26  15  95   7  17  20  18  49  75  50  32  16   3  35  52  33  10  20
  82  17  58  54  76  78  73  94  37  58  48  31  55  74  46  49  80  63  69  97
  11  43  47  43  59  50  77  10  82  86  30  64   3  87  17  79  65  90  72   1
  47  20  32   2  93  78  50  72  40  18  69  50  61  15  92  19  65  69  29  46
  54  58   9  57  45  26  35   9  16   6  10  63  25  42  64  18  19  13  89  59
  30  58   9  91  73   1   9  37  69  38  82  23  95  91  79  40  16  14  49  31
  19  58  93  44 100  56  61  18  69  50  77  99   7  85  89  79  85  98  15  53
  35  97  76  30  87  55  69   2  68  18  33  87  76  26  30  75  82  91  93  50
  41  69  48  47  53  37  26  38  34  41  91  68  37  66  98  23  20  66  25  88
  84  58  74  59  83   4  34  64  94  26  14  35  95  62  81  48  98   7  85  32
  47  75  99  84  41  96   6  61  62  31  48  46  89  22   4  71  25  37  35  18
  63  48  52  57   9  33   4   7  40  89  38  86  64  37  69   5  33  75  65  94

*/

 



answered Nov 1, 2023 by avibootz
edited Nov 2, 2023 by avibootz
...