fork download
  1. #include <iostream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. int main()
  8. {
  9. std::size_t n ;
  10. std::cout << "Enter the size of matrix:\n";
  11. std::cin >> n ;
  12. std::vector< std::vector<int> > a( n, std::vector<int>(n) ) ;
  13.  
  14. std::srand( std::time(nullptr) ) ;
  15. for( std::size_t i = 0 ; i < n ; ++i )
  16. {
  17. a[i][i] = 1 ; // diagonal
  18. for( std::size_t j = i+1 ; j < n ; ++j )
  19. a[i][j] = a[j][i] = std::rand() % 10 ;
  20. }
  21.  
  22. for( const auto& row : a )
  23. {
  24. for( int v : row ) std::cout << v << ' ' ;
  25. std::cout << '\n' ;
  26. }
  27. }
  28.  
Success #stdin #stdout 0s 3436KB
stdin
15
stdout
Enter the size of matrix:
1 7 1 4 0 9 2 7 4 6 1 1 9 2 5 
7 1 7 6 3 5 3 4 3 8 3 6 5 7 7 
1 7 1 7 7 9 4 7 2 8 9 2 3 9 6 
4 6 7 1 1 0 9 2 4 6 9 2 9 6 7 
0 3 7 1 1 4 0 6 9 8 1 9 8 0 6 
9 5 9 0 4 1 9 6 5 2 7 5 6 2 4 
2 3 4 9 0 9 1 4 3 6 4 5 3 2 6 
7 4 7 2 6 6 4 1 5 4 3 3 0 5 9 
4 3 2 4 9 5 3 5 1 9 3 2 8 3 5 
6 8 8 6 8 2 6 4 9 1 7 3 1 2 5 
1 3 9 9 1 7 4 3 3 7 1 8 9 3 0 
1 6 2 2 9 5 5 3 2 3 8 1 3 7 5 
9 5 3 9 8 6 3 0 8 1 9 3 1 2 3 
2 7 9 6 0 2 2 5 3 2 3 7 2 1 2 
5 7 6 7 6 4 6 9 5 5 0 5 3 2 1