Ответ:
C++:
#include <iostream>
using namespace std;
void fillSpiral(int arr[][10], int n) {
int k = 1, c1 = 0, c2 = n - 1, r1 = 0, r2 = n - 1;
while (k <= n * n) {
for (int i = c1; i <= c2; i++) {
arr[r1][i] = k++;
}
for (int j = r1 + 1; j <= r2; j++) {
arr[j][c2] = k++;
for (int i = c2 - 1; i >= c1; i--) {
arr[r2][i] = k++;
for (int j = r2 - 1; j > r1; j--) {
arr[j][c1] = k++;
c1++;
c2--;
r1++;
r2--;
void printMatrix(int arr[][10], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
cout << endl;
int main() {
int n;
cout << "Введіть значення N: ";
cin >> n;
int arr[10][10];
fillSpiral(arr, n);
printMatrix(arr, n);
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
C++:
#include <iostream>
using namespace std;
void fillSpiral(int arr[][10], int n) {
int k = 1, c1 = 0, c2 = n - 1, r1 = 0, r2 = n - 1;
while (k <= n * n) {
for (int i = c1; i <= c2; i++) {
arr[r1][i] = k++;
}
for (int j = r1 + 1; j <= r2; j++) {
arr[j][c2] = k++;
}
for (int i = c2 - 1; i >= c1; i--) {
arr[r2][i] = k++;
}
for (int j = r2 - 1; j > r1; j--) {
arr[j][c1] = k++;
}
c1++;
c2--;
r1++;
r2--;
}
}
void printMatrix(int arr[][10], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
int n;
cout << "Введіть значення N: ";
cin >> n;
int arr[10][10];
fillSpiral(arr, n);
printMatrix(arr, n);
return 0;
}