fork download
  1. #include <stdio.h>
  2.  
  3. // r: Consider a[0] like a row. (C uses row major)
  4.  
  5. // c: Consider a[0] like a column. (C uses column major)
  6.  
  7. void r_rowmajor(int a[2][3])
  8. {
  9. printf("r_rowmajor:");
  10. for (int c = 0; c < 2; c++)
  11. for (int r = 0; r < 3; r++)
  12. printf(" %d", a[c][r]);
  13. puts("");
  14. }
  15.  
  16. void r_colmajor(int a[2][3])
  17. {
  18. printf("r_colmajor:");
  19. for (int r = 0; r < 3; r++)
  20. for (int c = 0; c < 2; c++)
  21. printf(" %d", a[c][r]);
  22. puts("");
  23. }
  24.  
  25. void c_rowmajor(int a[2][3])
  26. {
  27. printf("c_rowmajor:");
  28. for (int c = 0; c < 3; c++)
  29. for (int r = 0; r < 2; r++)
  30. printf(" %d", a[r][c]);
  31. puts("");
  32. }
  33.  
  34. void c_colmajor(int a[2][3])
  35. {
  36. printf("c_colmajor:");
  37. for (int r = 0; r < 2; r++)
  38. for (int c = 0; c < 3; c++)
  39. printf(" %d", a[r][c]);
  40. puts("");
  41. }
  42.  
  43. int main(void)
  44. {
  45. int a[2][3] = {
  46. { 1, 2, 3 }, { 4, 5, 6 },
  47. };
  48.  
  49. c_rowmajor(a);
  50. c_colmajor(a);
  51. r_rowmajor(a);
  52. r_colmajor(a);
  53. }
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
c_rowmajor: 1 4 2 5 3 6
c_colmajor: 1 2 3 4 5 6
r_rowmajor: 1 2 3 4 5 6
r_colmajor: 1 4 2 5 3 6