[comp.lang.c] Initializing arrays, an advise is needed.

abed@saturn.wustl.edu (Abed M. Hammoud) (05/07/91)

	Hello, I looked in the CFAQ and I wasn't able to find an
	answer to the following question. So please, any comments
	other than flames would be appreciated.

	I have a program that calls a routine multiple times while
	running. Every time the routine get called it have to calculate
	samples of a function say sin(x) and fill an array of like
	n values long. where n, is the same during a given run.

	My question is, is there a way to initialize the elements of
	the array in something like

	float a[n] = { sin(a0), sin(a1), ....., sin(a(n-1)) }

	I remember seeing some discussion about this before, but
	I couldn't remember what was concluded then.

	Any comments will be appreciated.

	Thanks a million, 

+-------------------------------------------------+-----------------------+
|Abed M. Hammoud				  | abed@saturn.wustl.edu |
|Washington University.				  | Office:               |
|Electronic Systems & Signals Research Laboratory.| -Voice:(314) 726-7547 |
|Department of Electrical/Biomedical Engineering. | -FAX:  (314) 726-4434 |
|St. Louis, MO , 63130 USA			  |                       |
+-------------------------------------------------+-----------------------+

wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) (05/07/91)

In article <1991May7.044811.27782@cec1.wustl.edu> abed@saturn.wustl.edu (Abed M. Hammoud) writes:
>	I have a program that calls a routine multiple times while
>	running. Every time the routine get called it have to calculate
>	samples of a function say sin(x) and fill an array of like
>	n values long. where n, is the same during a given run.

One thing you could do is to have a static array inside your function
and compute its elements as needed. For example, if you function is
called foo:

	void foo(int n) {
		static int f[MAX_N] = { 1, 1 };
		static int computed = 2;
		
		while (computed <= n) {
			f[computed] = f[computed-1] * computed;
			++computed;
		}
		
		...
	}

(This isn't quite your example, but I hope you get the idea.) Since 'f'
and 'computed' are static, they keep their values from one call to
another. 

Lars Wirzenius  wirzenius@cc.helsinki.fi
-- 
Lars Wirzenius  wirzenius@cc.helsinki.fi

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (05/08/91)

abed@saturn.wustl.edu (Abed M. Hammoud) writes:

>	I have a program that calls a routine multiple times while
>	running. Every time the routine get called it have to calculate
>	samples of a function say sin(x) and fill an array of like
>	n values long. where n, is the same during a given run.

>	My question is, is there a way to initialize the elements of
>	the array in something like

>	float a[n] = { sin(a0), sin(a1), ....., sin(a(n-1)) }

I assume from one run to the next, n can be different, and n is also passed
as part of the arguments to your function anyway.  I also assume n == 0 is
not a valid condition.

Define a static integer internal to the subroutine and initialize it to 0.
I'll refer to it as k.

When the subroutine is entered, compare the incoming n to k.  If they are
the same, go on... the table is initialized.

If they are not the same, assert that k == 0 && n != 0, and if ok, execute
the code to initialize the table and fall through to do the deed for this
first function call as above.
-- 
 /***************************************************************************\
/ Phil Howard -- KA9WGN -- phil@ux1.cso.uiuc.edu   |  Guns don't aim guns at  \
\ Lietuva laisva -- Brivu Latviju -- Eesti vabaks  |  people; CRIMINALS do!!  /
 \***************************************************************************/