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 <iostream>
#include <iomanip>
#include <ctime>
 
#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++) {
            std::cout << std::setw(3) << matrix[i][j] << " ";
        } 
        std::cout << "\n";
    }
}
  
int generateRandomInteger(int first, int last) {
    return rand() % ((last + 1) - first) + first;
}
      
void generateRandomInteger(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 }};
     
    generateRandomInteger(matrix, ROWS);
        
    printMatrix(matrix, ROWS);
}
 
 
 
 
/*
run:
  
 81  51  18   5  65  31  62  36  45  79  71  81 100  98  11  55  76  51  81  85 
 64  74  40  81  68  24  71  35  84  72  95  16  22  65  72  38  95  33  25  39 
 63  95  71  15  44  82  69  72  84  49   8  47  22  99  27  42  22  50  28  57 
 21  22  72  94  86  43  84  32  27   8  22  42  55  93  56  50  74  24  21   9 
 25  29  55  98  27  33  39   1  34  18  57  54  40  29 100  77  23  83  61   2 
 42  82  43  48  74  50  98  99  73  70   7  97  50  61  95  77  46  85  29  79 
  3  37  33  94  17  84  22  40  18  82  93  59  64  35   7  37  84  56  88   8 
 25  94  57  75   7  51   3  52  87  31  30  89  19  14  34  36  97  56  75  14 
 37  67  25  52  53  83  41  88  38  28  47  14  21   3  88  27   5  42  78  92 
 24  60  32  43  73  66  78  22  73   4  35  61  22  11  13  74  93   5  13  82 
 32  59  96   4  62  35  83  18  77  12   9  52  71  41  94  96  58  71  17  30 
 26   3  90  47  14  54  72  58  58  84  40  41  95  87  97   8  73  79  25  49 
 42  86  53  13  78  46   8  35  69  76  16  94  78  57  41  43  11  12   1  20 
 48  92  13  94  30  61   1   2  39  77   3  32  62  55  44  39  52   3  25  20 
 30  40  14   8  97   6   2  59  69  54  30  16  45  42  61  26   2  61  80  92 
 90  82  24  51  88  19  42  39  74  66  11   3   6  76  62   2  81  64  12  49 
 17  41  17  14  35  29  39  88  90  18  80  31  51  55  81  90  73  22  29  98 
 40  39   1  45  14  62  98  46  25   9  46  94   1  14   7  35  43  45  23  84 
 15  54  14  17   8  46   7  32  20  87  82  59  77  82  55  42  95  52  39  72 
 12  84  65  64  50  71  99  92  67  73  75  33  78  40   2  37  85  60  20   4 

*/

 



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