Advertisement
Anglis

showArrWithSnack

Nov 24th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include<iostream>
  2.  
  3.     using namespace std;
  4.  
  5.       const int N = 5; //------n
  6.       const int M = 4;
  7.  
  8.  
  9.       int main() {
  10.           int a[N][M];
  11.           int right, left, top, bottom;
  12.           int _right = 0, _left = M - 2, _top = N - 2, _bottom = 1;
  13.           int right_ = M, left_ = -1, top_ = 0, bottom_ = N;
  14.           int sum = 0, k = 0;
  15.           bool flag = true;
  16.           int counter = 0;
  17.  
  18.           for (int i = 0; i < N; i++)
  19.               for (int j = 0; j < M; j++)
  20.                   a[i][j] = ++sum;
  21.  
  22.           for (int i = 0; i < N; i++) {
  23.               for (int j = 0; j < M; j++)
  24.                   cout << "[" << a[i][j] << "]\t";
  25.               cout << endl;
  26.           }
  27.           cout << endl;
  28.           cout << "---------------------------------" << endl;
  29.           cout << endl;
  30.  
  31.  
  32.           while (1) {
  33.               for (right = _right; right < right_; right++) {
  34.                   //moving right
  35.                   cout << "{right}";
  36.                   cout << a[top_][right] << endl;
  37.                   counter++;
  38.               }
  39.  
  40.  
  41.               if (!(_right < right_))
  42.                   break;
  43.  
  44.               for (bottom = _bottom; bottom < bottom_; bottom++) {
  45.                   //moving down
  46.                   cout << "{down}";
  47.                   cout << a[bottom][right_ - 1] << endl;
  48.                   counter++;
  49.               }
  50.  
  51.  
  52.               if (!(_bottom < bottom_))
  53.                   break;
  54.  
  55.               for (left = _left; left > left_; left--) {
  56.                   //moving left
  57.                   cout << "{left}";
  58.                   cout << a[bottom_ - 1][left] << endl;
  59.                   counter++;
  60.               }
  61.  
  62.               if (!(_left > left_))
  63.                   break;
  64.  
  65.               for (top = _top; top > top_; top--) {
  66.                   //moving top
  67.                   cout << "{up}";
  68.                   cout << a[top][left_ + 1] << endl;
  69.                   counter++;
  70.               }
  71.  
  72.               if (!(_top > top_))
  73.                   break;
  74.  
  75.               _right++;
  76.               _left--;
  77.               _top--;
  78.               _bottom++;
  79.               right_--;
  80.               left_++;
  81.               top_++;
  82.               bottom_--;
  83.               k++;
  84.           }
  85.           cout << "Check sum" << endl;
  86.           cout << counter << endl;
  87.           cout << M*N << endl;
  88.  
  89.  
  90.           return 0;
  91.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement