edvard_davtyan

Untitled

Mar 22nd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct pt
  4. {
  5.     int x, y;
  6.     pt()
  7.     {
  8.     }
  9.     pt(int x, int y): x(x), y(y)
  10.     {
  11.     }
  12. };
  13. istream& operator>> (istream& in, pt& p)
  14. {
  15.     char c;
  16.     return in >> c >> p.x >> c >> p.y >> c;
  17. }
  18. ostream& operator<< (ostream& out, const pt& p)
  19. {
  20.     return out << "(" << p.x << ", " << p.y << ")";
  21. }
  22. template<typename X>
  23. void read(X *mas, int a, int b)
  24. {
  25.     for (int i = 0; i<a; i++)
  26.     {
  27.         for (int j = 0; j<b; j++)
  28.         {
  29.             cin >> mas[i][j];
  30.         }
  31.     }
  32. }
  33. template<typename X>
  34. void print(X *mas, int a, int b)
  35. {
  36.     for (int i = 0; i < a; i++)
  37.     {
  38.         for (int j = 0; j < b; j++)
  39.         {
  40.             cout << mas[i][j] << ' ';
  41.         }
  42.         cout << endl;
  43.     }
  44. }
  45. template <typename X>
  46. void print_snake(X *mas, int a, int b)
  47. {
  48.     for (int i = 0; i< a; i++)
  49.     {
  50.         if (i % 2 == 0)
  51.         {
  52.             for (int j = 0; j < b; j++)
  53.             {
  54.                 cout << mas[i][j] << ' ';
  55.             }
  56.         }
  57.         else
  58.         {
  59.             for (int j = 0; j < b; j++)
  60.             {
  61.                 cout << mas[i][b - j - 1] << ' ';
  62.             }
  63.         }
  64.     }
  65.     cout << endl;
  66. }
  67. int main()
  68. {
  69.     pt mas[100][100];
  70.     int a, b;
  71.     cout<<"enter the number of rows and columns"<<endl;
  72.     cin >> a >> b;
  73.     cout<<"enter the matrix row by row"<<endl;
  74.     read(mas, a, b);
  75.     cout<<"your matrix is:" << endl;
  76.     print(mas, a, b);
  77.     cout<<"your matrix in snake order:" << endl;
  78.     print_snake(mas, a, b);
  79.     system("pause");
  80.     return 0;
  81. }
Add Comment
Please, Sign In to add comment