Skip to content

Instantly share code, notes, and snippets.

@andreisfedotov
Created April 27, 2017 17:47
Show Gist options
  • Save andreisfedotov/4ae23214ce4979f5fec8e2ad3cdbe197 to your computer and use it in GitHub Desktop.
Save andreisfedotov/4ae23214ce4979f5fec8e2ad3cdbe197 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv)
{
int arraysize;
int offset;
cin >> arraysize;
cin >> offset;
int **A = static_cast<int**>(malloc(arraysize * sizeof(int*)));
int **B = static_cast<int**>(malloc(arraysize * sizeof(int*)));
int *buffer = static_cast<int*>(malloc(offset * sizeof(int)));
int i, j;
for (i = 0; i < arraysize; i++)
{
A[i] = static_cast<int*>(malloc(arraysize * sizeof(int)));
B[i] = static_cast<int*>(malloc(arraysize * sizeof(int)));
buffer[i] = reinterpret_cast<int>(malloc(offset));
}
for (i = 0; i < arraysize; i++)
{
for (j = 0; j < arraysize; ++j)
{
A[i][j] = rand()%100;
}
}
int k;
for (i = arraysize - 1, k = 0; i > offset, k < offset; --i, ++k)
{
buffer[k] = A[i][arraysize - 1];
}
for (j = 0; j < arraysize; ++j)
{
if (j % 2 == 0)
{
for (i = 0; i < offset; ++i)
{
B[i][j] = buffer[offset - i - 1];
}
for (i = offset; i < arraysize; ++i)
{
B[i][j] = A[i - offset][j];
}
for( k = 0; k < offset; ++k)
{
buffer[k] = A[arraysize - k - 1][j];
}
}
else
{
for (i = arraysize - 1, k = 0; i > arraysize - offset - 1; --i, ++k)
{
B[i][j] = buffer[offset - k - 1];
#if DEBUG
if(k > offset)
{
cout << "buffer limit\n";
}
#endif
}
for (i = arraysize - offset - 1; i >= 0; --i)
{
B[i][j] = A[i + offset][j];
}
for (k = 0; k < offset; ++k)
{
buffer[k] = A[k][j];
}
}
}
for (i = 0; i < arraysize; ++i)
{
for (j = 0; j < arraysize; ++j)
{
cout << setw(4) << A[i][j];
}
cout << endl;
}
cout << endl;
cout << endl;
for (i = 0; i < arraysize; i++)
{
for (j = 0; j < arraysize; ++j)
{
cout << setw(4) << B[i][j];
}
cout << endl;
}
for (i = 0; i < arraysize; i++)
{
free(A[i]);
free(B[i]);
}
free(A);
free(B);
free(buffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment