/* Lab 8 Student Names Go Here */ #include #define kArraySize 5 #define kSentinel -1.0 /* * Function Using a Sentinel-Controlled Loop to Store Input Data in an Array. * Gets data to place in list until value of sentinel is encountered in * the input. * Returns number of values stored. * Stops input prematurely if there are more than list_size data values before * the sentinel or if invalid data is encountered. * Pre: sentinel and list_size are defined and list_size is the declared size of * list */ int fill_to_sentinel(int list_size, /* input - declared size of list */ double sentinel, /* input - end of data value in input list */ double list[]) /* output - array of data */ { double item; int num_items; int status; /* Sentinel input loop */ num_items = 0; status = scanf("%lf", &item); while (status == 1 && item != sentinel && num_items < list_size) { list[num_items] = item; ++num_items; status = scanf("%lf", &item); } /* Issues error message on premature exit */ if (status != 1) { printf("\n*** Error in data format ***\n"); printf("*** Using first %d data values ***\n", num_items); } else if (num_items >= list_size) { printf("\n*** Error: number of items exceeds array size ***\n"); printf("*** Using first %d data values ***\n", num_items); } /* return used portion of array */ return num_items; } int main(void) { double table[kArraySize]; int in_use; /* number of elements of table in use */ int index; /* loop index */ printf("Enter data items terminated by -1.\n"); in_use = fill_to_sentinel(kArraySize, kSentinel, table); printf("List of data values\n"); for (index = 0; index < in_use; ++index) { printf("%13.3f\n", table[index]); } return (0); }