Home > Net >  How to reduce the number of tokens for this C program
How to reduce the number of tokens for this C program

Time:11-22

I have written a program that takes a number(integer) as an input, and outputs a pattern of 1s and 0s, using for loops. For e.g. when the input is 7, the output is:

1 
1 1 
1 0 1 
1 0 0 1 
1 0 0 0 1 
1 0 0 0 0 1 
1 1 1 1 1 1 1

The source code is given below:

#include <stdio.h>
int main()
{
    int rows;
    int i,j;
    scanf("%d", &rows);
    for(i=0;i<rows;i  )
    {
        for(j=0;j<=i;j  )
        {
            if(i==j)
            {
                printf("1 \n");
            }
            else if(j==0)
            {
                printf("1 ");
            }
            else if (i==rows-1)
            {
                printf("1 ");
            }
            else
            {
                printf("0 ");
            }
       }
    }
   return 0;
}

When I compile the codem the number of tokens created is 107. I need to bring down the number tokens to less than 102 (to meet the test requirement).

Any suggestions how can I do it please?

CodePudding user response:

Why not just get rid of the braces in your if-else chain? That's 8 tokens, which would get you below your requirement.

CodePudding user response:

  for(j=0;j<=i;j  ){
            if(i==j)
                printf("1 \n");
            else if(j==0 || i==row-1)
                printf("1 ");
            else
                printf("0 ");
  }

so many tokens can be removed from your code.

CodePudding user response:

I would write the following way

include <sdtio.h>

int main( void )
{
    while (1)
    {
        printf( "Enter a non-negative number (0 - exit): " );
        unsigned int n;

        if (scanf( "%u", &n ) != 1 || n == 0) break;

        putchar( '\n' );

        for (unsigned int i = 0; i < n; i  )
        {
            for (unsigned int j = 0; j < i   1; j  )
            {
                unsigned int value = i == 0 || i == n - 1 || j == 0 || j == i;
                printf( "%d ", value );
            }
            putchar( '\n' );
        }

        putchar( '\n' );
    }
}

The program output might look like

Enter a non-negative number (0 - exit): 1

1

Enter a non-negative number (0 - exit): 2

1
1 1

Enter a non-negative number (0 - exit): 3

1
1 1
1 1 1

Enter a non-negative number (0 - exit): 4

1
1 1
1 0 1
1 1 1 1

Enter a non-negative number (0 - exit): 5

1
1 1
1 0 1
1 0 0 1
1 1 1 1 1

Enter a non-negative number (0 - exit): 6

1
1 1
1 0 1
1 0 0 1
1 0 0 0 1
1 1 1 1 1 1

Enter a non-negative number (0 - exit): 7

1
1 1
1 0 1
1 0 0 1
1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1 1

Enter a non-negative number (0 - exit): 0

Or if to reduce the number of tokens then

#include <stdio.h>

int main( void )
{
    unsigned n = 0;

    scanf( "%u", &n );

    for (unsigned i = 0; i < n; i  )
    {
        for (unsigned j = 0; j < i   1; j  )
        {
            unsigned value = i % ( n - 1 ) == 0 || j % i == 0;
            printf( "%d ", value );
        }
        putchar( '\n' );
    }
}

CodePudding user response:

Check this:

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i  )
        for(int j=0;j<=i;j  )
            printf(i == j ? "1 \n" : !j || i == rows - 1 ? "1 " : "0 ");
}

https://godbolt.org/z/3re3beW8W

Or even:

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i  ,putchar('\n'))
        for(int j=0;j<=i;j  )
            printf(!j || i == rows - 1 || i == j ? "1 " : "0 ");
}

or

int main(void)
{
    int rows;
    scanf("%d", &rows);
    for(int i=0;i<rows;i  ,putchar('\n'))
        for(int j=0;j<=i;j  )
            printf("%d ", !j || i == rows - 1 || i == j);
}
  • Related