user9728599
user9728599

Reputation:

logic behind printing this pattern

4 4 4 4 4 4 4  
4 3 3 3 3 3 4   
4 3 2 2 2 3 4   
4 3 2 1 2 3 4   
4 3 2 2 2 3 4   
4 3 3 3 3 3 4   
4 4 4 4 4 4 4  

we have to make a program for above pattern

#include <stdio.h>

int main() 
{

    int n;
    scanf("%d", &n);
    int len = n*2 - 1;
    for(int i=0;i<len;i++){
        for(int j=0;j<len;j++){
            int min = i < j ? i : j;
            min = min < len-i ? min : len-i-1;
            min = min < len-j-1 ? min : len-j-1;
             printf("%d ", n-min);
        }
        printf("\n");
    }
return 0;
}

this is the code to print the above pattern I am getting what is the main logic behind these codes I am trying to debug and trace this code. I need help, especially in these lines

int min = i < j ? i : j;
min = min < len-i ? min : len-i-1;
min = min < len-j-1 ? min : len-j-1;

Upvotes: 0

Views: 1249

Answers (2)

int min = i < j ? i : j; if this statement is true, min=i otherwise min=j.

Same logic applicable for:

 min = min < len-i ? min : len-i-1;
 min = min < len-j-1 ? min : len-j-1;

Upvotes: 0

lockcmpxchg8b
lockcmpxchg8b

Reputation: 2303

The following is a nearly identical version that is much easier to understand. Three confusing lines from the original program are commented out, with three replacement lines given below.

#include <stdio.h>

int main()
{

int n;
scanf("%d", &n);
int len = n*2 - 1;
for(int i=0;i<len;i++){
    for(int j=0;j<len;j++){
//            int min = i < j ? i : j;
//            min = min < len-i ? min : len-i-1;
//            min = min < len-j-1 ? min : len-j-1;
            int min_dist_top_or_bottom = i < len-i ? i : len-i-1;
            int min_dist_left_or_right = j < len-j ? j : len-j-1;
            int min = min_dist_top_or_bottom < min_dist_left_or_right ? min_dist_top_or_bottom : min_dist_left_or_right;
         printf("%d ", n-min);
    }
    printf("\n");
}
return 0;
}

In the commented version, it does the same computation, but builds it up in a strange incremental way. The revised version seeks to keep the calculations independent until they have to be combined to form the answer. After understanding the revised version, the original one is easier to pick apart.

In the revised version, the first line computes whether the current row-index i is closer to the top or the bottom of the table. Based on this it returns the distance to the nearest edge. The second line is quite similar but works on the column index j, hence it returns the distance to the nearest of the left- or right- edge.

The third line chooses the smaller of the values computed on the prior two lines.

The reason all of this is something like 'distance from the center' is that the subsequent printf subtracts the computed value from n, where n is half the width of the table...and the maximum distance an index can be from the edge is the center (i.e., half-way between each edge).

So now you can read the original version of the algorithm as making a rather arbitrary choice to start with the smaller of i or j on the first line. Then the next two lines do the same thing as the first two lines of the revised version, but you have to carry forward the knowledge that min is either i or j.

Consider the first of these lines: if min == i then the line is literally identical to the revised version; but if min == j, then the case (in the revised version) where i < len-i is moot, as j is already smaller than the resulting value of i. However the case where len-i-1 is smaller than min/j is still handled.

In short, the original is much harder to understand because it forces the reader to do a mental case-by-case analysis, and hold all the results in his/her head. The revised version computes simple stand-alone values, then combines them into the desired result.

Upvotes: 1

Related Questions