A22 Answer


#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");
    system("PAUSE");
    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");
  system("PAUSE");	
  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:

Then click on Parameters...

Then enter the parameters in the box.

With either of these methods, when your program starts running it will be supplied with two things: the number of arguments on the command line, and an array that contains the strings of characters from the command line. Spaces on the command line separate the individual arguments. The first argument (element 0) of the array is the name of the executable.

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(int argc, char *argv[])

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 always null-terminated strings, so if you want numberic 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.