Home > OS >  Problem in my code Printing Square form With Natrual Numbers in C By using loops Only
Problem in my code Printing Square form With Natrual Numbers in C By using loops Only

Time:12-14

I'm trying to print a square with natural number Like if i enter Natrual Number 3 I need to print

33333
32223
32123
32223
33333

like that so i did write a code that work perfectly for 3 but i cant understand why he is not working to 4 and 5 etc ... any tips for correcting the code below :

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
    int n = 3,k;
    for (int i = 0; i < (2 * n - 1); i  )
    {
        for (int j = 0;j < (2 * n - 1);j  )
        {
            if (j == 0 || j == (2 * n - 2) || i == 0 || i == (2 * n - 2))
                printf("%d", n);
            else if (i == (n - 1) && j == (n - 1))
                printf("1");
            else
            {
                if (i >= n || i==n-1)
                {
                    k = (i / 2);
                    printf("%d", n - k);
                }
                else
                    printf("%d", n - i);

                
            }
            }
        printf("\n");
    }
    return 0;
}

CodePudding user response:

As mentioned by 0___________ above, more useful variable names will go a long way to helping you solve these problems.

Your solution has essentially hard-coded in the logic required for the 3x3 square, without giving proper consideration to the underlying mathematics required for the general case. See my example below for a more robust solution.

It is always best with these kinds of problems to try to find the most general solution possible. It may be easy to start on the right path by hard-coding the edges and centre as you have, but this breaks down once you reach the rest of the values required. Taking a step back to fully consider the problem will be useful for you in future.

#include <stdio.h>

int max(int a, int b) {
    if (a > b){
        return a;
    }
    
    return b;
}

int main() {
    int size = 4, rowDiff, colDiff;
    
    for (int row = 0; row < (2 * size - 1); row  )
    {
        for (int col = 0;col < (2 * size - 1);col  )
        {
            if (row >= size -1)
                rowDiff = row - size   2;
            else
                rowDiff = size -row;
            
            if (col >= size -1)
                colDiff = col - size   2;
            else 
                colDiff = size - col;
                
            printf("%d", max(rowDiff, colDiff));
        }
        printf("\n");
    }
    return 0;
}
    int main() {
        int size = 4, rowDiff, colDiff;
        
        for (int row = 0; row < (2 * size - 1); row  )
        {
            for (int col = 0; col < (2 * size - 1); col  )
            {
                if (row >= size -1)
                    rowDiff = row - size   2;
                else
                    rowDiff = size -row;
                
                if (col >= size -1)
                    colDiff = col - size   2;
                else 
                    colDiff = size - col;
                    
                printf("%d", max(rowDiff, colDiff));
            }
            printf("\n");
        }
        return 0;
    }

  •  Tags:  
  • c
  • Related