Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<time.h>
- const int M = 4;
- const int cols = 4;// stolbu
- void init(int* B, int n);
- void print(int* B, int n);
- void lab(int* B, int n);
- int main()
- {
- const int N = 4;
- srand(time(NULL));
- int* B = (int*)malloc(sizeof(int*) * N * M);
- init(B, N);
- print(B, N);
- lab(B, N);
- free(B);
- return 0;
- }
- void init(int* B, int n){
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < M; j++) {
- B[i * M + j] = rand() % 100;
- }
- }
- }
- void print(int* B, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < M; j++) {
- printf("%3d", B[i * M + j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void lab(int* B, int rows) {
- int krug = 0; int sum = 0;
- int goright, goleft, goup, godown;
- int flag = 0;
- while(sum!=cols*rows) {
- for (goleft = cols - 1 - krug; goleft > krug; goleft--) {
- printf("%3d", B[cols * (rows - 1 - krug) + goleft]);
- sum++;
- }
- if (sum >= cols * rows) break;
- for (goup = rows - 1 - krug; goup > krug; goup--) {
- printf("%3d", B[cols * goup+krug]);
- sum++;
- }
- if (sum >= cols * rows) break;
- for (goright = krug ; goright < cols - krug - 1; goright++) {
- printf("%3d", B[cols * krug + goright]);
- sum++;
- }
- if (sum >= cols * rows) break;
- for (godown = krug; godown < rows - 1 - krug; godown++) {
- printf("%3d", B[cols-1 +(cols)*godown-krug]);
- sum++;
- }
- if (sum >= cols * rows) break;
- krug++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement