Answer L22

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

/* Puzzle A22 -- print a n by m block of stars centered on a N by M background of dots */
int main( int argc, char *argv[] )
{
  int row, col;
  int n=5, m=7 ;    /* default size of small block */
  int N=11, M=13 ;  /* default size of big block */
  int left, right, top, bottom; /* location of small block */
  
  /* Use command line arguments if supplied */
  if ( argc == 5 )
  {
    n = atoi( argv[1] );
    m = atoi( argv[2] );
    N = atoi( argv[3] );
    M = atoi( argv[4] );
  }

  /* Remind user of command line arguments */
  else if ( argc != 1 && argc != 5 )
  {
    printf("block.exe n m N M\n");
    
    return 0;
  }
  
  /* Sides of the small block */
  left = M/2 - m/2;
  right = left + m - 1;
  top = N/2 - n/2;
  bottom = top + n - 1;
  
  /* Print the block within a block */
  for ( row=0; row<N; row++ )
  {
    for ( col=0; col<M; col++ )
    {
      /* if in the inner block, print Star */
      if ( row>=top && row<=bottom && col>=left && col<=right )
        printf("*");
      else
        printf(".");
    }
    printf("\n");
  }
  
  printf("\n");
  	
  return 0;
}

Comments: Command line arguments are very useful. The user can quickly enter parameters without the bother of responding to a series of prompts. For the current program, say that the executable is named block.exe. Then, when the program is run from a command window (the "DOS window") the values for n, m, N, and M are entered like this:

C:\>block.exe 5 7 11 13

If you are running the program from the Dev-C++ environment, pull down the Execute menu:

DEV-C++ menu

Then click on Parameters...

DEV-C++ command line parameters

Then enter the parameters in the box.

With either of these methods, when your program starts running it has two things: the number of arguments on the command line, and an array that contains pointers to the arguments on the command line. These arguments are stored as null-terminated strings. Arguments on the command line are separated by spaces. The first argument (element 0) of the array is the name of the executable program.

Usually a main() program uses the name argc for the number of arguments (the argument count), and argv for the array of argument values. In the program this is declared like this:

int main()

The string argv[0] is the program name (block.exe in this case), then argv[1] is the first string that the user entered (the character 5 in this case), argv[2] is the second string that the user entered (the character 7), and so on.

The argument count is always one or greater. The program name will always be in argv[0], so that counts as one argument.

The user-entered arguments are null-terminated strings, so if you want numeric values use a C function to convert them. This program uses atoi()to convert ascii to integer. So the four parameters that the user might have entered are converted to integers and placed in variables using these statements:

    n = atoi( argv[1] );
    m = atoi( argv[2] );
    N = atoi( argv[3] );
    M = atoi( argv[4] );

This program has some typical logic dealing with command line parameters. Usually there are default values and code that reminds the user how to enter the parameters.



Back to Puzzle Home