Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created November 16, 2011 21:06
Show Gist options
  • Select an option

  • Save ramntry/1371394 to your computer and use it in GitHub Desktop.

Select an option

Save ramntry/1371394 to your computer and use it in GitHub Desktop.
Печать массива в матрицу по спирали
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void printHelix(int * matrix[], int size)
{
int x = size / 2;
int y = x;
int k = 1;
cout << "Helix = { ";
for (;;)
{
for (int i = 0; i < k; i++)
{
cout << matrix[x][y] << ' ';
if (k % 2)
{
x--;
if (x == -1)
{
cout << "}" << endl;
return;
}
}
else
x++;
}
for (int j = 0; j < k; j++)
{
cout << matrix[x][y] << ' ';
if (k % 2)
y++;
else
y--;
}
k++;
}
}
void printMatrix(int * matrix[], int size)
{
int wide = ceil(log10(size*size)) + 1;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
cout << setw(wide) << matrix[i][j];
cout << endl;
}
cout << endl;
}
int main()
{
clog << "This program print the helix-presentation of matrix N x N\n"
<< "Enter N (odd): ";
int n = 0;
cin >> n;
if (n % 2 == 0 || n < 0)
{
cout << "N must be odd and positive!" << endl;
return 1;
}
cout << endl;
int ** matrix = new int*[n];
for (int i = 0; i < n; i++)
{
matrix[i] = new int[n];
for (int j = 0; j < n; j++)
matrix[i][j] = i * n + j;
}
printMatrix(matrix, n);
printHelix(matrix, n);
for (int i = 0; i < n; i++)
delete[] matrix[i];
delete[] matrix;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment