fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. void initialize_matrix(vector< vector<int> > &a, int m, int n)
  8. {
  9. int i, j;
  10.  
  11. // make sure the matrix has the given number of rows,
  12. // and that each row has the right number of elements
  13. // one for each column; number of rows is m, number of
  14. // columns is n
  15.  
  16. a.resize(m);
  17.  
  18. for (i=0; i<m; i++)
  19. a[i].resize(n);
  20.  
  21. // generate the matrix
  22.  
  23. for (i=0; i<m; i++)
  24. for (j=0; j<n; j++)
  25. a[i][j] = drand48()*101; // random integer from 0 to 100
  26. }
  27.  
  28. void print_matrix(vector< vector<int> > &a)
  29. {
  30. int i, j;
  31. int m = a.size();
  32. int n = a[0].size();
  33.  
  34. for (i=0; i<m; i++)
  35. {
  36. for (j=0; j<n; j++)
  37. cout << a[i][j] << " ";
  38. cout << endl;
  39. }
  40. }
  41.  
  42. int main()
  43. {
  44. int m, n;
  45. vector< vector<int> > a;
  46.  
  47. // read matrix size from input
  48.  
  49. cin >> m;
  50. cin >> n;
  51.  
  52. // set random seed
  53.  
  54. srand48(123);
  55.  
  56. // initialize the matrix
  57.  
  58. initialize_matrix(a, m, n);
  59.  
  60.  
  61. // print the matrix
  62.  
  63. print_matrix(a);
  64.  
  65. // return 0 from main
  66.  
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0.01s 2864KB
stdin
6 3
stdout
28 41 93 
13 12 35 
63 84 70 
15 78 37 
94 14 85 
12 82 39