// file: quiz_06/example.cc // // This program computes the sum of abs((2/i)) over a user-specified range. // If it completes succesfully, it should write the sum to stdout. // // system include files // #include #include #include int main(int argc, char** argv) { // decode the command line arguments // FILE* fp; // run a loop // long i_start = atoi(argv[2]); long i_end = atoi(argv[3]); float sum = (float)0.0; long status = (long)0; for (long i = i_start; i < i_end; i++) { // accumulate fractions // sum += (float)2 / fabs((float)i); fprintf(stdout, "%ld: sum = %f\n", i, sum); // open a file and write the current value // if ((fp = fopen(argv[i], "w")) == (FILE*)NULL) { fprintf(stdout, "%s: error opening file (%s)\n", argv[0], argv[1]); return -1; } else { fprintf(fp, "%ld: sum = %f\n", i, sum); } fclose(fp); } // write the output sum // fprintf(stdout, "the sum over the range [%d,%d] = %f\n", i_start, i_end, sum); // exit gracefully // return status; }