Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. Lösung:
  2. 1 #include <iostream >
  3. 2 using namespace std;
  4. 3
  5. 4 //#define DEBUG
  6. 5
  7. 6 int main (){
  8. 7 const unsigned int n = 5;
  9. 8 unsigned int matrix[n][n];
  10. 9
  11. 10 unsigned int k=0; /* auxilary variable */
  12. 11 unsigned int i=0; /* element counter */
  13. 12 unsigned int r=0; /* rows <> */
  14. 13 int c=-1; /* columnes ˆv */
  15. 14 while(i < n*n){
  16. 15 /* direction: right */
  17. 16 for(unsigned int j=0; j<n-k; j++){
  18. 17 i++; /* increment element value */
  19. 18 matrix[r][++c] =i; /* write element value */
  20. 19 #ifdef DEBUG
  21. 20 cout <<" >" <<r <<"," <<c <<"="<<i;
  22. 21 #endif
  23. 22 }
  24. 23 #ifdef DEBUG
  25. 24 cout <<endl;
  26. 25 #endif
  27. 26
  28. 27 /* direction: down */
  29. 28 if(i < n*n){
  30. 29 k++;
  31. 30 for(unsigned int j=0; j<n-k; j++){
  32. 31 i++; /* increment element value */
  33. 32 matrix [++r][c] = i; /* write element value */
  34. www.esit.rub.de
  35. 5 / 5 INFO1: ¨Ubung Nr. 3
  36. 33 #ifdef DEBUG
  37. 34 cout <<" v" <<r <<"," <<c <<"="<<i;
  38. 35 #endif
  39. 36 }
  40. 37 #ifdef DEBUG
  41. 38 cout <<endl;
  42. 39 #endif
  43. 40 }
  44. 41
  45. 42 /* direction: left */
  46. 43 if(i < n*n){
  47. 44 for(unsigned int j=0; j<n-k; j++){
  48. 45 i++; /* increment element value */
  49. 46 matrix[r][--c] = i; /* write element value */
  50. 47 #ifdef DEBUG
  51. 48 cout <<" <" <<r <<"," <<c <<"="<<i;
  52. 49 #endif
  53. 50 }
  54. 51 #ifdef DEBUG
  55. 52 cout <<endl;
  56. 53 #endif
  57. 54 }
  58. 55
  59. 56 /* direction: up */
  60. 57 if(i < n*n){
  61. 58 k++;
  62. 59 for(unsigned int j=0; j<n-k; j++){
  63. 60 i++; /* increment element value */
  64. 61 matrix[--r][c] = i; /* write element value */
  65. 62 #ifdef DEBUG
  66. 63 cout <<" ˆ" <<r <<"," <<c <<"="<<i;
  67. 64 #endif
  68. 65 }
  69. 66 #ifdef DEBUG
  70. 67 cout <<endl;
  71. 68 #endif
  72. 69 }
  73. 70 }
  74. 71
  75. 72 for(unsigned int r = 0; r < n; ++r){
  76. 73 for(unsigned int c = 0; c < n; ++c){
  77. 74 if(matrix[r][c] < 10){
  78. 75 cout << ’ ’;
  79. 76 }
  80. 77 cout << matrix[r][c] << ’ ’;
  81. 78 }
  82. 79 cout << endl;
  83. 80 }
  84. 81 return 0;
  85. 82 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement