Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define N 3 //Khai báo cấp ma trận
- //Hàm xoay ma trận
- void xoayMaTran(int a[N][N])
- {
- //Chạy qua từng chu kỳ
- for (int i = 0; i < N / 2; i++) {
- for (int j = i; j < N - i - 1; j++) {
- //Hoán vị từng phần tử theo từng chu kỳ
- int temp = a[i][j];
- a[i][j] = a[N - 1 - j][i];
- a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j];
- a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i];
- a[j][N - 1 - i] = temp;
- }
- }
- }
- //Hàm nhập ma trận
- void nhapMaTran(int arr[N][N])
- {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++)
- {
- cout << "\nNhap arr[" << i << "][" << j << "] = ";
- cin >> arr[i][j];
- }
- cout << '\n';
- }
- }
- //Hàm xuất ma trận
- void xuatMaTran(int arr[N][N])
- {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++)
- {
- cout << arr[i][j] << "\t";
- }
- cout << "\n\n";
- }
- }
- int main()
- {
- int arr[N][N];
- cout << "\n------------- NHAP MA TRAN -------------\n";
- nhapMaTran(arr); //nhập phần tử
- system("cls");
- cout << "\n------------- MA TRAN DA NHAP -------------\n\n";
- xuatMaTran(arr);
- xoayMaTran(arr); //xử lý xoay ma trận
- cout << "\n------------- MA TRAN BI XOAY -------------\n\n";
- xuatMaTran(arr);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement