fork download
  1. // This program will construct and display an n*n identity matrix.
  2.  
  3. #include <stddef.h>
  4. #include <stdio.h>
  5.  
  6. int main(void)
  7. {
  8. size_t n=0;
  9.  
  10. printf("Please input `n': \n");
  11. scanf("%zu", &n);
  12.  
  13. int matrix[n][n];
  14.  
  15. for (size_t i=0; i < n; ++i)
  16. for (size_t j=0; j < n; ++j)
  17. if (i == j)
  18. matrix[i][j] = 1;
  19. else
  20. matrix[i][j] = 0;
  21.  
  22. for (size_t i=0; i < n; ++i)
  23. {
  24. for (size_t j=0; j < n; ++j)
  25. printf("%d ", matrix[i][j]);
  26.  
  27. printf("\n");
  28. }
  29. }
Success #stdin #stdout 0s 2296KB
stdin
10
stdout
Please input `n': 
1 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 
0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 1 0 0 
0 0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 0 0 1