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 with user input
int i, vector[MAXELEMS];
for (i = 0; i < numberOfElems; i++)
scanf("%d", &vector[i]);
|
Works ok, unless too many numbers; usually wastes space.
Recall that assert() terminates program with standard error message if test fails.
|