fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define ARRAY_ELEMENT(ARRAY, Y, X, STRIDE) ARRAY[(Y) * STRIDE + (X)]
  6.  
  7. int main(void)
  8. {
  9. unsigned int base;
  10. unsigned int y, x, offsetX, offsetY;
  11.  
  12. int *arr = NULL;
  13. unsigned int arrSize = 0;
  14. unsigned int arrStride = 0;
  15.  
  16. scanf("%u", &base);
  17.  
  18. // Allocate memory for an array.
  19. arrSize = base * base * base * base * sizeof(int);
  20. arrStride = base * base;
  21. arr = (int *)malloc(arrSize);
  22.  
  23. // Initialize array.
  24. memset(arr, 0, arrSize);
  25.  
  26. // Fill value
  27. for(y = 0; y < base; ++y)
  28. {
  29. for(x = 0; x < base; ++x)
  30. {
  31. offsetY = y * base;
  32. offsetX = x * base;
  33. ARRAY_ELEMENT(arr, offsetY + y, offsetX + x, arrStride) = 1;
  34. }
  35. }
  36.  
  37. // Print out.
  38. for(y = 0; y < base * base; ++y)
  39. {
  40. for(x = 0; x < base * base; ++x)
  41. {
  42. printf("%d", ARRAY_ELEMENT(arr, y, x, arrStride));
  43. if((x + 1) % base == 0 && ((x + 1) / base) < base)
  44. printf("|");
  45. }
  46. if((y + 1) % base == 0 && ((y + 1) / base) < base)
  47. {
  48. printf("\n");
  49. for(x = 0; x < base * base; ++x)
  50. {
  51. printf("-");
  52. if((x + 1) % base == 0 && ((x + 1) / base) < base)
  53. printf("+");
  54. }
  55. }
  56. printf("\n");
  57. }
  58.  
  59. // Free up.
  60. free(arr);
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 1856KB
stdin
3
stdout
100|010|001
000|000|000
000|000|000
---+---+---
000|000|000
100|010|001
000|000|000
---+---+---
000|000|000
000|000|000
100|010|001