ROTATE IMAGE IN 90 DEGREE

 avatar
user_6075971
plain_text
2 years ago
1.9 kB
0
Indexable
Never
#include <iostream>
using namespace std;
void rotate_array(int arr[][100], int x, int y)
{
    int count = 0;
    int max = x * y;
    int fr = 0;
    int lr = x - 1;
    int fc = 0;
    int lc = y - 1;
    while(count<max)
    {
        int aa = fc;
        int bb = lc;
        while (aa <= bb)
        {
            swap(arr[fr][aa], arr[fr][bb]);
            aa++;
            bb--;
            count++;
        }
        int a = fc;
        int b = lr;
        while (a <= lc && b >= fr)
        {
            swap(arr[fr][a], arr[b][lc]);
            a++;
            b--;
            count++;
        }
        lc--;
        int c = fc;
        while (c <= lc)
        {
            swap(arr[lr][c], arr[fr][c]);
            c++;
            count++;
        }
        lr--;
     
        int d = fc;
        int e = fr;
        while (d <= lc && e <= lr)
        {
            swap(arr[fr][d], arr[e][fc]);
            d++;
            e++;
            count++;
        }
        fc++;
        int cc = fc;
        int dd = lc;
        while (cc < dd)
        {
            swap(arr[fr][cc], arr[fr][dd]);
            cc++;
            dd--;
        }
        fr++;
    }
    cout << "RESULT" << endl;
    for (int m = 0; m < x; m++)
    {
        for (int n = 0; n < y; n++)
        {
            cout << arr[m][n] << "  ";
        }
        cout << endl;
    }
}
int main()
{
    int i, j;
    cout << "ENTER THE ROW SIZE---";
    cin >> i;
    cout << "ENTER THE COLOUM SIZE---";
    cin >> j;
    int arr[100][100];

    for (int m = 0; m < i; m++)
    {
        for (int n = 0; n < j; n++)
        {
            cin >> arr[m][n];
        }
    }

    for (int m = 0; m < i; m++)
    {
        for (int n = 0; n < j; n++)
        {
            cout << arr[m][n] << "  ";
        }
        cout << endl;
    }
    rotate_array(arr, i, j);
    return 0;
}