fork(4) download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. vector<vector<int> > matrix;
  9. int n;
  10.  
  11. cout << "Please enter the size of the identity matrix" << endl;
  12. cin >> n;
  13.  
  14. // Initialize the matrix as a n x n array of 0.
  15. matrix = vector<vector<int> >(n, vector<int>(n,0));
  16.  
  17. // Set the diagonal to be 1s
  18. for(unsigned int t = 0; t < n; t++)
  19. matrix[t][t] = 1;
  20.  
  21. // Print it
  22. for(unsigned int y = 0; y < n; y++)
  23. {
  24. for(unsigned int x = 0; x < n; x++)
  25. cout << "\t" << matrix[y][x];
  26. cout << "\n";
  27. }
  28. }
Success #stdin #stdout 0.02s 2820KB
stdin
15
stdout
Please enter the size of the identity matrix
	1	0	0	0	0	0	0	0	0	0	0	0	0	0	0
	0	1	0	0	0	0	0	0	0	0	0	0	0	0	0
	0	0	1	0	0	0	0	0	0	0	0	0	0	0	0
	0	0	0	1	0	0	0	0	0	0	0	0	0	0	0
	0	0	0	0	1	0	0	0	0	0	0	0	0	0	0
	0	0	0	0	0	1	0	0	0	0	0	0	0	0	0
	0	0	0	0	0	0	1	0	0	0	0	0	0	0	0
	0	0	0	0	0	0	0	1	0	0	0	0	0	0	0
	0	0	0	0	0	0	0	0	1	0	0	0	0	0	0
	0	0	0	0	0	0	0	0	0	1	0	0	0	0	0
	0	0	0	0	0	0	0	0	0	0	1	0	0	0	0
	0	0	0	0	0	0	0	0	0	0	0	1	0	0	0
	0	0	0	0	0	0	0	0	0	0	0	0	1	0	0
	0	0	0	0	0	0	0	0	0	0	0	0	0	1	0
	0	0	0	0	0	0	0	0	0	0	0	0	0	0	1