Download this source file
/* FindMin.c - This program searches an array for the smallest item. The input
* is read from the standard input. The input must be less than MAX_LINES
* lines long. Note that the line length is limited---word processors
* sometimes produce files with one line per paragraph, which won't work here!
*
* The reason the array "data" is global is that on PCs, the amount of memory
* for local objects (ie, on the stack) is very limited. Global objects are
* usually allowed to be larger, but are still limited to less than 32K bytes
* usually. 32000/81 = 395, so we have ~390 lines max. If we allowed longer
* lines of 132, we could only have ~240 lines. Using dynamic memory and/or
* a "Large" or "Huge" memory model on a PC can usually solve these problems.
* (On UNIX machines, you don't usually have such problems.) Note that
* find_min and printout still pass the array as a parameter.
*
* Written 2001 by Wayne Pollock, Tampa Florida USA.
*/
#include
#include
#define MAX_OBJECT_SIZE 32000 /* This is about right for Mac and DOS */
#define LINE_LENGTH (80 + 1) /* Allow one extra byte for final null (0). */
#define MAX_LINES (MAX_OBJECT_SIZE / LINE_LENGTH - 5) /* About 390. */
char data[MAX_LINES][LINE_LENGTH]; /* Global to allow a larger size. */
int find_min (int num_lines, char data[][LINE_LENGTH]);
int main (void)
{ int i, num_lines = 0, smallest;
while (fgets(data[num_lines], LINE_LENGTH, stdin) != NULL)
{ i = strlen(data[num_lines]) - 1; /* i points to last char on line. */
data[num_lines][i] = '\n'; /* Make sure line ends with '\n'. */
++num_lines;
}
smallest = find_min(num_lines, data);
printf("The minimum line is:\n\t%s", data[smallest]);
return 0;
}
int find_min (int num_lines, char data[][LINE_LENGTH])
{
}