Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Матриь.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void print_array(int [], int [5][5]);
- int main()
- {
- int arr[25] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
- int mat[5][5];
- print_array(arr, mat);
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- cout << setw(3) << mat[i][j];
- cout << endl;
- }
- return 0;
- }
- void print_array(int arr[], int mat[5][5])
- {
- int top = 0, bottom = 5 - 1;
- int left = 0, right = 5 - 1;
- int index = 0;
- while (1)
- {
- if (top > bottom)
- break;
- // lqva kolona
- for (int i = top; i <= bottom; i++)
- mat[i][left] = arr[index++];
- left++;
- if (left > right)
- break;
- // dolen red
- for (int i = left; i <= right; i++)
- mat[bottom][i] = arr[index++];
- bottom--;
- if (top > bottom)
- break;
- // dqsna kolona
- for (int i = bottom; i >= top; i--)
- mat[i][right] = arr[index++];
- right--;
- if (left > right)
- break;
- // goren red
- for (int i = right; i >= left; i--)
- mat[top][i] = arr[index++];
- top++;
- }
- }
Add Comment
Please, Sign In to add comment