[prev] [index] [next]

Dynamic Data Example (cont)

Suggestion #1: allocate a large vector; use only part of it

#define MAXELEMS 1000

// how many elements in the vector
int numberOfElems;
scanf("%d", &numberOfElems);
assert(numberOfElems <= MAXELEMS);

// declare vector and fill from stdin
int i, vector[MAXELEMS];
for (i = 0; i < numberOfElems; i++) {
	scanf("%d", &vector[i]);
}

Works ok, unless too many numbers; usually wastes space.

assert() terminates program with standard error message if test fails.