Здравствуйте Alexander Kotelovich, Вы писали:
AK>Я примерно и начал так реализовывать, потом закрались мысли о полярных координатах, геом. формулах сприрали, периодичности знаков итд.
Вот самый простой вариант ИМХО для начала чтоб что-то было
// spirali.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
const MAX_COLS = 10;
const MAX_ROWS = 10;
void create_and_fill_array(int ***array, int cols, int rows);
void display_array(int **array, int cols, int rows);
void parse_and_display_array(int **array, int cols, int rows);
int main(int argc, char* argv[])
{
int **ptr;
int cols, rows;
std::cout << "Input the number on columns (< 10): ";
std::cin >> cols;
if (cols > MAX_COLS) {
std::cout << "Error. To big number.";
return 0;
}
std::cout << "Input the number of rows (< 10): ";
std::cin >> rows;
if (rows > MAX_ROWS) {
std::cout << "Error. To big number.";
return 0;
}
create_and_fill_array ( &ptr, cols, rows );
std::cout << "A random created array\n\n";
display_array ( ptr, cols, rows );
std::cout << "\n\nThe created vector\n\n";
parse_and_display_array ( ptr, cols, rows );
if(ptr){
for(int i = 0; i < rows; i++)
delete [] ptr[i];
delete [] ptr;
}
std::cout << "\nPress any key...";
getch();
return 0;
}
//---------------------------------------------------------------------
void create_and_fill_array(int ***array, int cols, int rows)
{
*array = new int*[ rows ];
if (NULL == *array) {
return ;
}
srand( time(NULL) );
for(int i = 0; i < rows; ++i){
(*array)[ i ] = new int[ cols ];
for(int j = 0; j < cols; j++)
(*array)[ i ][ j ] = rand() % 100;
}
}
//---------------------------------------------------------------------
void display_array(int **array, int cols, int rows)
{
if (NULL == array) {
return;
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++)
std::cout << array[i][j] << '\t';
std::cout << '\n';
}
}
//---------------------------------------------------------------------
bool is_done(bool *ptr, int cols, int rows)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
if (!ptr[i * cols + j]) {
return false;
}
return true;
}
//---------------------------------------------------------------------
void parse_and_display_array(int **array, int cols, int rows)
{
bool *tmp = new bool[ cols * rows ];
memset( tmp, 0, cols * rows );
static short dx[] = { 1, 0, -1, 0 };
static short dy[] = { 0, 1, 0, -1 };
int startX(0), startY(0), iDir(0);
int nextX, nextY;
while (!is_done (tmp, cols, rows)) {
std::cout << array[startY][startX] << ",";
std::cout.flush ();
tmp[startY * cols + startX] = 1;
nextX = startX + dx[iDir];
nextY = startY + dy[iDir];
if (nextX < 0 || nextX >= cols
||
nextY < 0 || nextY >= rows
||
tmp[nextY * cols + nextX]) {
iDir++;
if (iDir > 3) {
iDir = 0;
}
nextX = startX + dx[iDir];
nextY = startY + dy[iDir];
}
startX = nextX;
startY = nextY;
}
delete [] tmp;
}
//---------------------------------------------------------------------