distance to the closest border.c

Nov 23rd, 2021
997
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.
3.     distance to the closest border.c
4.
7.
8.       n = 3
9.
10.   1  1  1  1  1
11.
12.   1  2  2  2  1
13.
14.   1  2  3  2  1
15.
16.   1  2  2  2  1
17.
18.   1  1  1  1  1
19.
20.     You can find all my C programs at Dragan Milicev's pastebin:
21.
22.     https://pastebin.com/u/dmilicev
23.
24. */
25.
26. #include <stdio.h>
27.
28. // Returns smallest of four numbers dl, dr, dt, db
29. int min_of_4_numbers( int dl, int dr, int dt, int db){
30.     int min = dl;
31.
32.     if( min>dr ) min=dr;
33.     if( min>dt ) min=dt;
34.     if( min>db ) min=db;
35.
36.     return min;
37. }
38.
39. // Numbers in pattern are the distance to the closest border.
40. // There are the distance to the left dl, right dr, top dt and bottom db border.
41. void number_pattern(int n){
42.     int raw, col, d;
43.     // The distance to the left, right, top and bottom border:
44.     int dl, dr, dt, db;
45.
46.     printf("\n      n = %d \n\n\n",n);
47.
48.     for( raw=0; raw<2*n-1; raw++){
49.         for( col=0; col<2*n-1; col++){
50.             // The distance to the left, right, top and bottom border:
51.             dl = col;
52.             dr = 2*n-1 - (col+1);
53.             dt = raw;
54.             db = 2*n-1 - (raw+1);
55.
56.             d = min_of_4_numbers(dl, dr, dt, db);   // The distance to the closest border
57.             printf("%3d", d+1);                     // Print according number
58.         }
59.         printf("\n\n");                             // new raw
60.     }
61. }
62.
63. int main(void){
64.
65.     number_pattern(3);
66.
67.     printf("\n\n");
68.
69.     number_pattern(5);
70.
71.     printf("\n\n");
72.
73.     return 0;
74.
75. } // main()
76.