[comp.std.c] A small programming challenge.

kevin@loki.une.oz.au (Kevin Pollard) (05/15/91)

I have written some code to accept command line parameters and apply
meaningful parameters - but I feel it is an interesting problem which
must have some neat solutions.

The task is:
	allow up to 4 parameters to be entered on the command line
	(that is, progname [ p1 [ p2 [ p3 [ p4 ] ] ] ] if you know what I mean) 
	The program is to accept the parameters. If the a parameter
	is missing then it is to default to the parameter is to 
	default to the one before it.
	(that is, if  progname x y z   is entered, then the 4th parameter
	 is to default to z)
	and so on down to the first parameter which is to have a hard
	coded default. If no parameters were entered then all parmeters
	would end up defaulting to this hard coded default.



Does anyone out there know of some neat ways of doing this.

This is really an algorithm problem, but since there is no algorithm
newsgroup, and I program in C on an IBM-type machine, this would be
a relevant newsgroup.

-- 
Kevin Pollard			Internet:	kevin@loki.une.oz.au
 University of NewEngland
	Northern Rivers		Phone:		+61 (066) 203694
Lismore NSW Australia		"My brain hurts"

kevin@nodecg.ncc.telecomwa.oz.au (Kevin Spencer) (05/16/91)

In article <1499@loki.une.oz.au> kevin@loki.une.oz.au (Kevin Pollard) writes:
>I have written some code to accept command line parameters and apply
>meaningful parameters - but I feel it is an interesting problem which
>must have some neat solutions.
>
>The task is:
>	allow up to 4 parameters to be entered on the command line
>	(that is, progname [ p1 [ p2 [ p3 [ p4 ] ] ] ] if you know what I mean) 
>	The program is to accept the parameters. If the a parameter
>	is missing then it is to default to the parameter is to 
>	default to the one before it.
>	(that is, if  progname x y z   is entered, then the 4th parameter
>	 is to default to z)
>	and so on down to the first parameter which is to have a hard
>	coded default. If no parameters were entered then all parmeters
>	would end up defaulting to this hard coded default.
>
>
>
>Does anyone out there know of some neat ways of doing this.
>
>This is really an algorithm problem, but since there is no algorithm
>newsgroup, and I program in C on an IBM-type machine, this would be
>a relevant newsgroup.
>
>-- 
>Kevin Pollard			Internet:	kevin@loki.une.oz.au
> University of NewEngland
>	Northern Rivers		Phone:		+61 (066) 203694
>Lismore NSW Australia		"My brain hurts"

----- Solution 1 -----
#include <stdio.h>

#define PAR(n) (n < argc) ? argv[n] : argv[argc-1])

main(argc,argv)
	int		argc;
	char	**argv;
{
	int		i;

	for (i=1; i<5; i++) puts(PAR(i));
}

----- Solution 2 -----
#include <stdio.h>
#include <string.h>

static char *args[4];

main(argc,argv)
	int		argc;
	char	**argv;
{
	int		i;

	for (i=1; i<4; i++)
		args[i] = (i<argc ? argv[i] : argv[argc-1]);

	for (i=0; i<4; i++) puts(args[i]);
}

----------------------

Solution 1 is shorter, but references to PAR must be in main, or argc and
argv must be globally referenceable.
Solution 2 is more robust, but more complicated.

-- 
-----------------------------------///-----------------------------------------
Kevin Spencer (09) 420 8173       /// Insert profound gem of wisdom here...
kevin@ncc.telecomwa.oz.au     \\\///
-------------------------------\XX/--------------------------------------------

barmar@think.com (Barry Margolin) (05/17/91)

In article <1991May16.073245.12652@nodecg.ncc.telecomwa.oz.au> kevin@nodecg.UUCP (Kevin Spencer) writes:
>	for (i=1; i<4; i++)
>		args[i] = (i<argc ? argv[i] : argv[argc-1]);

This never sets args[4] (and in fact, the declaration I didn't include in
the above quote doesn't declare it large enough).  Also, it doesn't default
args[1] properly when no arguments are given.  It ends up being defaulted
to argv[0] (usually the program name) rather than the intended hardcoded
default.

The code should be something like

void main (argc, argv)
int argc;
char *argv[];
{
	char *args[4];

	args[0] = (argc < 2) ? DEFAULT : argv[1];

	for (i = 1; i < 4; i++) {
	    args[i] = (i+1 < argc) ? argv[i+1] : args[i-1];
	}
	...
}
-- 
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

ttobler@unislc.uucp (Trent Tobler) (05/21/91)

From article <1499@loki.une.oz.au>, by kevin@loki.une.oz.au (Kevin Pollard):
> I have written some code to accept command line parameters and apply
> meaningful parameters - but I feel it is an interesting problem which
> must have some neat solutions.
> 
> The task is:
> 	allow up to 4 parameters to be entered on the command line
> 	(that is, progname [ p1 [ p2 [ p3 [ p4 ] ] ] ] if you know what I mean) 
> 	The program is to accept the parameters. If the a parameter
> 	is missing then it is to default to the parameter is to 
> 	default to the one before it.
> 	(that is, if  progname x y z   is entered, then the 4th parameter
> 	 is to default to z)
> 	and so on down to the first parameter which is to have a hard
> 	coded default. If no parameters were entered then all parmeters
> 	would end up defaulting to this hard coded default.
> 
> 
> 
> Does anyone out there know of some neat ways of doing this.
> 
> This is really an algorithm problem, but since there is no algorithm
> newsgroup, and I program in C on an IBM-type machine, this would be
> a relevant newsgroup.

Are you sure this is not a homework assignment?  It looks so simple that
I hesitate to give the answer.  Perhaps the algorithem is what you want.  If
so here it is:

  assign default to hard_default.
  assign n to 0 (or 1 if that is the array base)
  repeat 4 times :
     if there is an argument then assign p[n] to it.
       otherwise, assign default to p[n].
     assign default to p[n]
     increment n

--

  Trent Tobler