randomizing the numbers

I've found this else where on the site and was wondering how would you change this program to produce random numbers instead of the numbers that the user is prompted to put in each position.


#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
srand(time(0));
//initialize the matrix
int** Matrix; //A pointer to pointers to an int.
int rows,columns;
cout<<"Enter number of rows: ";
cin>>rows;
cout<<"Enter number of columns: ";
cin>>columns;
Matrix=new int*[rows]; //Matrix is now a pointer to an array of 'rows' pointers.

//define the matrix
for(int i=0;i<rows;i++)
{
Matrix[i]=new int[columns]; //the ith array is initialized
for(int j= 0;j<columns;j++) //the i,jth element is defined


{
cout<<"Enter element in row "<<(i+1)<<" and column "<<(j+1)<<": ";
cin>>Matrix[i][j];
}
}

//Print the matrix
cout<<"The matrix you have input is:\n";
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
cout<<Matrix[i][j]<<"\t"; //tab between each element
cout<<"\n"; //new row
}

//now, we have to free up the memory we took by releasing each vector:
for(int i=0;i<rows;i++)
delete[] Matrix[i];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <random>

int main()
{
    std::size_t rows ;
    std::cout << "Enter number of rows: ";
    std::cin >> rows;

    std::size_t columns ;
    std::cout << "Enter number of columns: ";
    std::cin >> columns;

    // matrix is a vector containing 'rows' rows
    // each row is a vector containing 'columns' integers
    // https://cal-linux.com/tutorials/vectors.html
    std::vector< std::vector<int> > matrix( rows, std::vector<int>(columns) ) ;

    // fill the matrix with random integers uniformly distributed in [20,70]
    // http://en.cppreference.com/w/cpp/numeric/random
    std::mt19937 rng( std::random_device{}() ) ;
    std::uniform_int_distribution<int> distrib(20,70) ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto& row : matrix ) // for each row in the matrix
        for( int& v : row ) v = distrib(rng) ; // assign a random value to each int

    // print the matrix
    for( const auto& row : matrix ) // for each row in the matrix
    {
        for( int v : row ) std::cout << v << ' ' ; // print the values in the row
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/31ddaf766961c547
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
   srand( time( 0 ) );

   int rows, cols;
   cout << "Enter number of rows: ";   cin >> rows;
   cout << "Enter number of cols: ";   cin >> cols;
   int *M = new int[rows*cols];       // 1-d array, mappable to a 2-d one by: n = i * cols + j
                                      //                                  or: i = n / cols;   j = n % cols
   // Fill
   for ( int n = 0; n < rows * cols; n++ ) M[n] = rand() % 10;      // adapt as required

   // Print
   cout << "The matrix is:\n";
   for( int n = 0; n < rows * cols; n++ )
   {
     cout << M[n] << "\t"; 
     if ( ( n + 1 ) % cols == 0 ) cout << '\n';
   }

   // Tidy up
   delete [] M;
}


Enter number of rows: 3
Enter number of cols: 5
The matrix is:
4	5	9	4	6	
5	4	4	5	0	
2	0	4	8	4	
Thanks, that works perfectly after adjusting the fill numbers. I really appreciate the help you guys are life savers.
Topic archived. No new replies allowed.