Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- distance to the closest border.c
- Task:
- https://www.facebook.com/groups/CPP.and.C.programming.for.Beginners/permalink/1792537050933194/
- n = 3
- 1 1 1 1 1
- 1 2 2 2 1
- 1 2 3 2 1
- 1 2 2 2 1
- 1 1 1 1 1
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // Returns smallest of four numbers dl, dr, dt, db
- int min_of_4_numbers( int dl, int dr, int dt, int db){
- int min = dl;
- if( min>dr ) min=dr;
- if( min>dt ) min=dt;
- if( min>db ) min=db;
- return min;
- }
- // Numbers in pattern are the distance to the closest border.
- // There are the distance to the left dl, right dr, top dt and bottom db border.
- void number_pattern(int n){
- int raw, col, d;
- // The distance to the left, right, top and bottom border:
- int dl, dr, dt, db;
- printf("\n n = %d \n\n\n",n);
- for( raw=0; raw<2*n-1; raw++){
- for( col=0; col<2*n-1; col++){
- // The distance to the left, right, top and bottom border:
- dl = col;
- dr = 2*n-1 - (col+1);
- dt = raw;
- db = 2*n-1 - (raw+1);
- d = min_of_4_numbers(dl, dr, dt, db); // The distance to the closest border
- printf("%3d", d+1); // Print according number
- }
- printf("\n\n"); // new raw
- }
- }
- int main(void){
- number_pattern(3);
- printf("\n\n");
- number_pattern(5);
- printf("\n\n");
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement