Question
Write a C program called filter.c that reads from standard input and writes to standard output. The input to your program will be a sequence
Write a C program called filter.c that reads from standard input and writes to standard output. The input to your program will be a sequence of positive integers separated by random amounts of white space. The output of your program will be the input numbers grouped and formatted into columns.
Here is an example of a sequence of 91 input numbers.
7038 9643 8101 9718 4451 8252 3817 697 9206 92 1387 8195 6356 1011 347 8981 957 4635 6958 802 3976 1733 1678 7386 9675 9044 5409 8084 2129 9634 1091 1618 9533 6506 8909 5594 8 6148 7382 8137 1435 574 645 2264 1030 8819 2353 1620 6125 3259 7661 2993 2525 3500 8821 4424 1095 3845 354 6962 9715 7373 1428 798 63 1316 4003 7318 1399 4951 1403 8057 106 556 9513 14 2345 7163 7844 1984 2119 8698 4095 3657 2382 6020 2665 2822 9345 6 7504
Here are those 91 numbers grouped into groups of length 14, then formatted into 8 columns with each column being 7 characters wide (including the spaces), and with one blank line between each group of numbers.
7038 9643 8101 9718 4451 8252 3817 697 9206 92 1387 8195 6356 1011 347 8981 957 4635 6958 802 3976 1733 1678 7386 9675 9044 5409 8084 2129 9634 1091 1618 9533 6506 8909 5594 8 6148 7382 8137 1435 574 645 2264 1030 8819 2353 1620 6125 3259 7661 2993 2525 3500 8821 4424 1095 3845 354 6962 9715 7373 1428 798 63 1316 4003 7318 1399 4951 1403 8057 106 556 9513 14 2345 7163 7844 1984 2119 8698 4095 3657 2382 6020 2665 2822 9345 6 7504
/* This program adds a number to every value read from standard input and writes the result to standard output.
The default value to add to every input number is 0. This default value can be overridden by 1.) a value from a configuration file, or by 2.) an environment variable, or by 3.) a command line argument. */ #include
int main(int argc, char *argv[]) { // Set the default value. int incBy = 0;
// Override the default value with a value from the configuration file. FILE * fp; if ( (fp = fopen("incrementor.cfg", "r")) != NULL ) { // get an operand from the configuration file fscanf(fp, "%d", &incBy); }
// Override the default value with an environment variable value. char * op; if ( (op = getenv( "IncrementorValue" )) != NULL ) { // get an operand from the environment incBy = atoi(op); }
// Get a command line argument (if it exists). if (argc > 1) { // get an operand from the command line incBy = atoi(argv[1]); }
// Process the stream of input numbers. double x; while( scanf("%lf", &x ) != EOF ) { printf("%.1f ", (x + incBy)); }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started