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