C/C++

Спиральное отображения матрицы в векторе

Я сделал все кроме спирали (не получается... :( )

#include
using namespace std;
int main()
{
int m, n, i, j;
float **a;
int *g;

srand(2);

cout << "Int DATA:";

cin >> n >> m;

int re_n;
int re_m;
int G, t = 0;
G = n * m;

g = (int *)malloc(G * sizeof(int *));

a = (float **)calloc(n, sizeof(float *));

for (i = 0; i < n; i++)
{
a[i] = (float *)malloc(m * sizeof(float));
}

for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
a[i][j] = rand() % 100;
}
}

for (i = 0; i < n; i++)
{
cout << endl
<< endl;
for (j = 0; j < m; j++)
{
cout << "A[" << i << "][" << j << "]=" << a[i][j] << " ";
}
}
cout << endl
<< endl;
int c = n / 2;
for (int d = 0; d < c+1; d++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{

g[t] = a[i][j];
}
}
}
for (i = 0; i < G; i++)
{
cout << g[i] << " ";
//cout << "V[" << i << "]=" << g[i] << " ";
}
}

а что конкретно? Есть двумерный массив и как его преобразовать???
ГГ
Гаро Гаро
21 700
Лучший ответ
Гаро Гаро тем более это не вектор!
Денис Михайлов (Прошу прощения за русский, я молдован)
Типа значение и матрицы должны быть перенесены в вектор,
Спирально
Матрица такая:
1 2 3 4 5 6
7 1 2 3 4 5
6 7 8 9 1 2
А в векторе данные должны быть так представлены
1 2 3 4 5 6 5 2 1 9 8 7 6 7 1 2 3 4
Верхняя сторона
Правая сторона
Нижнея' сторона
И та по центру
Денис Михайлов Но длина и ширина матрицы всегда разные
Это мне не понятно как сделать...
Гаро Гаро Проверь сам. Вроде так. Для наглядности 6 на 6 массив. подгонишь под свой в 3 строки r = 6 и q= 18 тогда
Денис Михайлов Хорошо спасибо
Денис Михайлов Больше спасибо!!!
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
class Random {
public:
Random() {
random_device device;
gen.seed(device());
}
int uniform(int first, int last) {
uniform_int_distribution<int> uid(first, last);
return uid(gen);
}
private:
mt19937 gen;
};
struct Direction {
inline static bool right = true;
inline static bool bottom = false;
inline static bool left = false;
inline static bool top = false;
};
int main() {
const int rows = 5;
const int cols = 5;
int matrix[rows][cols];
Random rand;
for (auto& row : matrix) {
for (auto& x : row) {
x = rand.uniform(0, 9);
cout << setw(3) << x;
}
puts("\n");
}
const int length = rows * cols;
int vector[length];
int i = 0, j = 0;
int left = 0, right = rows - 1;
int top = 0, bottom = cols - 1;
for (auto n = 0U; n < length; ++n) {
vector[n] = matrix[i][j];
if (Direction::right) {
if (j < right) ++j;
else {
Direction::right = false;
Direction::bottom = true;
++top;
}
}
if (Direction::bottom) {
if (i < bottom) ++i;
else {
Direction::bottom = false;
Direction::left = true;
--right;
}
}
if (Direction::left) {
if (j > left) --j;
else {
Direction::left = false;
Direction::top = true;
--bottom;
}
}
if (Direction::top) {
if (i > top) --i;
else {
Direction::top = false;
Direction::right = true;
++left;
++j;
}
}
}
puts("");
for (auto x : vector) cout << setw(3) << x;
puts("");
system("pause > nul");
}
Юрий Гайдар
Юрий Гайдар
51 283