include stdafx using namespace std int create int int creating of mass

  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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"
using namespace std;
int ** create(int, int); //creating of massiv
void free(int **, int); //deleting of matrix
void initialize(int **, int, int); //input of matrix
void print(int**, int, int); //printing of matrix
int ** turn(int**, int, int, int); //turn mass
int ** turn_left(int**, int, int, int); //turn mass on the left_side
int _tmain()
{
int a=1, b; // answer and number of elements
int j = 0;
int n; //number of string
int m; //number of columns
cout << "Enter the number of the strings\n";
cin >> n;
cout << "Enter the number of the columns\n";
cin >> m;
cout << endl;
int **A = create(n, m); //creating of massiv
initialize(A, n, m); //initializing of massiv
print(A, n, m); //array output on the screen
//turn of massiv ^___^
int k = n / 2; //количество слоев
k = 1; //DANGER DANGER DANGER
while (a == 1)
{
cout << "\nTurn the array\n1)right\n2)left?\n";
cin >> a;
cout << "\nHow many elements to turn an array?\n";
cin >> b;
if (a == 1)
{
while (b > 0)
{
turn(A, n, m, k); //передаем в функцию параметры-имя массива,размер,количество слоев
b--;
}
cout << endl;
print(A, n, m); //array output on the screen
}
else if (a == 2)
{
while (b > 0)
{
turn_left(A, n, m, k); //повторот влево
b--;
}
cout << endl;
print(A, n, m);
}
else
{
cout << "Error. Wrong answer\n";
}
cout << "\nTurn again?\n1)Yes\n2)No\n";
cin >> a;
}
free(A, n); //deleting of matrix
system("pause");
return 0;
}
int** create(int N, int M)
{
int** mas = new int*[N];
for (int i = 0; i<N; i++)
mas[i] = new int[M];
return mas;
}
void initialize(int ** mas, int N, int M)
{
for (int i = 0; i<N; i++){
for (int j = 0; j<M; j++){
mas[i][j] = rand() % 210;
if (i == j)
mas[i][j] = 1;
}
}
}
void print(int** mas, int N, int M)
{
for (int i = 0; i<N; i++)
{
for (int j = 0; j<M; j++){
cout << mas[i][j] << "\t";
}
cout << endl;
}
}
void free(int ** mas, int N)
{
for (int i = 0; i<N; i++)
delete mas[i];
delete[]mas;
}
int ** turn(int **mas, int N, int M, int k)
{
int b; //variable,which should be used,if you want to save one element of matrix
int i, j;
while (k>0)
{
for (int i = 0; i<k; i++)
{
b = mas[i][i];
for (j = i; j<N - i - 1; j++)
mas[j][i] = mas[j + 1][i]; //левую на 1 вверх
for (j = i; j<M - i - 1; j++)
mas[N - i - 1][j] = mas[N - i - 1][j + 1]; //низ на 1 влево
for (j = N - i - 1; j>i; j--)
mas[j][M - i - 1] = mas[j - 1][M - i - 1]; //правую на 1 вниз
for (j = M - i - 1; j>i + 1; j--) //верх на 1 вправо
mas[i][j] = mas[i][j - 1];
mas[i][i + 1] = b;
}
k--;
}
return mas;
}
int** turn_left(int **mas, int N, int M, int k)
{
int b;
int i, j;
while (k>0)
{
for (i = 0; i<k; i++)
{
b = mas[N - i - 1][i];
for (j = N - i - 1; j>i; j--) //левую на 1 вниз
mas[j][i] = mas[j - 1][i];
for (j = i; j<N - 1; j++) //верхнюю на один влево
mas[i][j] = mas[i][j + 1];
for (j = i; j<M - 1 - i; j++) //правую на один вверх
mas[j][N - i - 1] = mas[j + 1][N - i - 1];
for (j = M - i - 1; j>i + 1; j--) //низ на один вправо
mas[N - i - 1][j] = mas[N - i - 1][j - 1];
mas[N - i - 1][i + 1] = b;
}
k--;
}
return mas;
}