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 matrix with random numbers between 1 and 100 in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>
#include <ctime>
  
#define ROWS 5
#define COLS 6
 
void printMatrix(int matrix[][COLS]) {
    for (int i = 0; i < ROWS; i++) {  
        for (int j = 0; j < COLS; j++) {
            std::cout << std::setw(3) << matrix[i][j] << " ";
        } 
        std::cout << "\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]) {
    srand(time(NULL));
      
    for (int i = 0; i < ROWS; i++) {     
        for (int j = 0; j < COLS; j++) {
            matrix[i][j] = generateRandomInteger(1, 100);
        }
    }
}
 
int main() {
    int matrix[ROWS][COLS] = {{ 0 }};
      
    generateRandomMatrix(matrix);
         
    printMatrix(matrix);
}
  
  
  
  
/*
run:
   
 23  82  60  17  16  99 
 28  61  17  75  98  95 
 19   1  78 100  36  94 
 49  33  55  63  85   8 
 62 100  61  43  46  68 
   
*/

 



answered Nov 13, 2023 by avibootz
...