Home > Mobile >  How to print V in c programming in 10X10 row and column in a optimized way?
How to print V in c programming in 10X10 row and column in a optimized way?

Time:06-14

I printed V using * in specific rows and columns. I had lots of if statements. I am sharing my code below.

Is there any optimised way to print the V pattern in 10X10 row column? (without many if conditions)?

#include <stdio.h>

int main() {
    int row, column;
    for (row = 1; row <= 10; row  ) {
        for (column = 1; column <= 10; column  ) {
            if (row == 1 && (column == 1 || column == 10)
             || row == 3 && (column == 2 || column == 9)
             || row == 5 && (column == 3 || column == 8)
             || row == 7 && (column == 4 || column == 7)
             || row == 10 && column == 5)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

CodePudding user response:

This code considers the relation between the row and the position of the *, if you are in the first row, then we want the column 0 and colum 9 to print *, then second row, we want column 1 and column 8 and so on. Thus, I used an iterator for the rows and iterator for columns to know in a concrete row which column I'm printing.

  #include <stdio.h>

int main()
{
    int matrix_size = 10; //Assuming is squared matrix 10x10
    int counter = 0;
    int i,j;
    for(i=0;i<(int)(matrix_size/2);i  ) {
        for(j=0;j<matrix_size;j  ) {
            if(j==i || j==(matrix_size-1-i)) {
                printf("*");
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    
    return 0;
}

EDIT: compared to tshiono solution, I write the V in just 5 rows and he prints it in 10 rows but assuming an space line between each line. Boths solutions are ok depending on what u want.

CodePudding user response:

I don't think this is optimized but will be simpler and scalable with SIZE.

#include <stdio.h>
#define SIZE 10
#define MID ((SIZE-1)/2)                // midst position of SIZE
#define ABS(x) ((x)<0?-(x):(x))         // absolute value of x

int main()
{
    int i, j;
    for (i = 0; i < SIZE; i  ) {
        for (j = 0; j < SIZE; j  ) {
            if (i % 2 == 0 && ABS(j - MID) == MID - i / 2) putchar('*');
            else putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}

CodePudding user response:

For the V shape to be symmetric, the number of rows and columns should be odd. Here is a simpler method:

#include <stdio.h>
#include <stdlib.h>

int main() {
    for (int n = 11, row = 0; row < n; row  ) {
        for (int column = 0; column < n; column  )
            putchar(" *"[2 * abs(column - n / 2) == n - 1 - row]);
        printf("\n");
    }
    return 0;
}

Output:

*         *

 *       *

  *     *

   *   *

    * *

     *

For a thicker V shape:

#include <stdio.h>
#include <stdlib.h>

int main() {
    for (int n = 11, row = 0; row < n; row  ) {
        for (int column = 0; column < n; column  )
            putchar(" *"[abs(2 * abs(column - n / 2) - (n - 1 - row)) <= 1]);
        printf("\n");
    }
    return 0;
}

Output:

*         *
**       **
 *       *
 **     **
  *     *
  **   **
   *   *
   ** **
    * *
    ***
     *

CodePudding user response:

Other answers take into account the specific shape of V and optimize around that.

I suggest an optimized solution for any shape.

This involves a lookup table containing all the locations of * characters composing the shape.

struct { int row,col; } shape[] = {
   {1,1}, {1,10}, {3,2}, {3,9}, {5,3}, {5,8}, {7,4}, {7,7}, {10,5},
   {-1,-1}
};

The last location ({-1,-1}) has the same purpose as terminating '\0' for strings.

  • Related