Заполнить массив по спирали.
Заполнить двумерный массив целыми числами от 1 до 100 по спирали.
идея наверное такая
Код:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int Arr[10][10];
/*NULL init all elements*/
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Arr[i][j] = NULL;
}
}
/*stage 1*/
int ind = 10;
int num = 1;
int dif = 26;
for (int i = 0; i < 5; i++)
{
for (int j = i; j < ind; j++)
{
Arr[i][j] = num;
num++;
}
ind--;
if(i > 0)
{
dif -= 6;
}
else if (i == 4)
{
dif = 6;
}
num = num + dif;
}
/*stage 2*/
int k = 5;
num = 99;
for (int j = k; j > 3; j--)
{
Arr[k][j] = num++;
}
num = 94;
dif = 11;
ind = 3;
/*stage 3*/
for (int i = 6; i < 10; i++)
{
for (int j = ind; j < i; j++)
{
Arr[i][j] = num;
num--;
}
ind--;
if (i > 6)
{
dif += 6;
}
num = num - dif;
}
/*stage 4*/
ind = 8;
num = 36;
dif = 36;
for (int j = 0; j < 4; j++)
{
for (int i = j + 1; i <= ind; i++)
{
Arr[i][j] = num;
num--;
}
ind--;
if(j > 0)
dif -= 10;
num = num + dif;
}
/*last stage*/
ind = 4;
num = 89;
dif = 21;
for (int j = 6; j < 10; j++)
{
for (int i = ind; i <= j; i++)
{
Arr[i][j] = num;
num++;
}
ind--;
if (j > 6)
dif += 10;
num = num - dif;
}
/*Print*/
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(5) << Arr[i][j];
}
cout << endl;
}
_getch();
return 0;
}
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int Arr[10][10];
/*NULL init all elements*/
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Arr[i][j] = NULL;
}
}
/*stage 1*/
int ind = 10;
int num = 1;
int dif = 26;
for (int i = 0; i < 5; i++)
{
for (int j = i; j < ind; j++)
{
Arr[i][j] = num;
num++;
}
ind--;
if(i > 0)
{
dif -= 6;
}
else if (i == 4)
{
dif = 6;
}
num = num + dif;
}
/*stage 2*/
int k = 5;
num = 99;
for (int j = k; j > 3; j--)
{
Arr[k][j] = num++;
}
num = 94;
dif = 11;
ind = 3;
/*stage 3*/
for (int i = 6; i < 10; i++)
{
for (int j = ind; j < i; j++)
{
Arr[i][j] = num;
num--;
}
ind--;
if (i > 6)
{
dif += 6;
}
num = num - dif;
}
/*stage 4*/
ind = 8;
num = 36;
dif = 36;
for (int j = 0; j < 4; j++)
{
for (int i = j + 1; i <= ind; i++)
{
Arr[i][j] = num;
num--;
}
ind--;
if(j > 0)
dif -= 10;
num = num + dif;
}
/*last stage*/
ind = 4;
num = 89;
dif = 21;
for (int j = 6; j < 10; j++)
{
for (int i = ind; i <= j; i++)
{
Arr[i][j] = num;
num++;
}
ind--;
if (j > 6)
dif += 10;
num = num - dif;
}
/*Print*/
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(5) << Arr[i][j];
}
cout << endl;
}
_getch();
return 0;
}
Код:
#include <iostream>
using namespace std;
#define ARRAY_SIZE 10
typedef enum {
Horizontal,
Vertical
} Direction;
void main(void) {
int array[ARRAY_SIZE][ARRAY_SIZE];
int row = 0, column = 0, value = 1;
Direction direction = Horizontal;
short hstep = 1, vstep = 1;
while (value <= ARRAY_SIZE * ARRAY_SIZE) {
array[row][column] = value++;
switch (direction) {
case Horizontal:
if (column == ARRAY_SIZE - row - 1) {
row += vstep;
direction = Vertical;
hstep = -hstep;
}
else
column += hstep;
break;
case Vertical:
if (((vstep < 0) && (row == column + 1)) || ((vstep > 0) && (row == column))) {
column += hstep;
direction = Horizontal;
vstep = -vstep;
}
else
row += vstep;
break;
}
}
for (row = 0; row < ARRAY_SIZE; row++) {
for (column = 0; column < ARRAY_SIZE; column++)
cout << array[row][column] << "\t";
cout << endl;
}
cin.get();
}
using namespace std;
#define ARRAY_SIZE 10
typedef enum {
Horizontal,
Vertical
} Direction;
void main(void) {
int array[ARRAY_SIZE][ARRAY_SIZE];
int row = 0, column = 0, value = 1;
Direction direction = Horizontal;
short hstep = 1, vstep = 1;
while (value <= ARRAY_SIZE * ARRAY_SIZE) {
array[row][column] = value++;
switch (direction) {
case Horizontal:
if (column == ARRAY_SIZE - row - 1) {
row += vstep;
direction = Vertical;
hstep = -hstep;
}
else
column += hstep;
break;
case Vertical:
if (((vstep < 0) && (row == column + 1)) || ((vstep > 0) && (row == column))) {
column += hstep;
direction = Horizontal;
vstep = -vstep;
}
else
row += vstep;
break;
}
}
for (row = 0; row < ARRAY_SIZE; row++) {
for (column = 0; column < ARRAY_SIZE; column++)
cout << array[row][column] << "\t";
cout << endl;
}
cin.get();
}
Согласен. Критика справедлива.