Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "stdio.h"
- #include <time.h>
- #define M 5
- #define N 5
- void init_matr(int a[][M], int n) {
- unsigned i, j;
- for (i = 0; i < n; i++) {
- for (j = 0; j < M; j++) {
- a[i][j] = rand() % 100;
- }
- }
- }
- void print_matr(int a[][M], int n) {
- unsigned i, j;
- for (i = 0; i < n; i++) {
- for (j = 0; j < M; j++) {
- printf("%4d", a[i][j]);
- }
- printf("\n");
- }
- }
- void the_power_of_spiral(int a[][M], int n) {
- unsigned i, j;
- int m = M;
- int qu = M * n;
- unsigned left = 0;
- unsigned right = M - 1;
- unsigned upper = 0;
- unsigned bottom = n - 1;
- while ((left <= right) && (upper <= bottom) && (qu > 0)) {
- for (j = left; j < right; j++) {
- printf("%d ", a[upper][j]);
- qu--;
- if (qu == 0) { break; }
- }
- for (i = upper; i < bottom; i++) {
- printf("%d ", a[i][right]);
- qu--;
- if (qu == 0) { break; }
- }
- for (j = right; j > left; j--) {
- printf("%d ", a[bottom][j]);
- qu--;
- if (qu == 0) { break; }
- }
- for (i = bottom; i > upper; i--) {
- printf("%d ", a[i][left]);
- qu--;
- if (qu == 0) { break; }
- }
- left++;
- right--;
- upper++;
- bottom--;
- }
- if(qu == 1){printf("%d ", a[n/2][m/2]);}
- }
- int main() {
- int a[N][M] = { 0 };
- init_matr(a, N);
- print_matr(a, N);
- printf("\n");
- the_power_of_spiral(a, N);
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement