Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

thank you for helping!! The file input.txt contains negative and positive number

ID: 3545298 • Letter: T

Question

thank you for helping!!

The file input.txt contains negative and positive numbers in the range [-100.0, 100.0]. Write a filter program that reads the numbers from the file, and writes the positive numbers to a new file, filtered.txt eliminating the negative numbers. An example is shown in Figure 1. Test your program using the example below (Create input.txt having the numbers in figure 1 manually) Name your file assign4_1.c Write a program that computes the minimum, maximum and average of numbers stored in the file, filtered.txt that you created in question 1. Write the result to a new file, statistics.txt. The format of the files is shown in Figure 2. Name your file assign4_2.c After coding your programs, first test them using the example above (Create input.txt having the number in figure 1 manually). Make sure that your program is working for the above example. After that, use the input.txt file on the class web page. The content of the corresponding statistics.txt file created in the end should be: Minimum: 0.1 Maximum: 99.9 Average: 49.9 Submit the electronic copies (c source files) using blackboard. The program you submit should be your own work. Cheating will be reported to the office of academic integrity. Both the copier and copiee will be held responsible.

Explanation / Answer

/// save as assign4_1.c

#include<stdio.h>
int main()
{
FILE* fp = fopen("input.txt","r");
FILE* fwrite = fopen("filtered.txt","w");
float value;
    if( fp != NULL )
    {
      while ( !feof(fp ) )
    {
    fscanf(fp,"%f",&value);
    if(value>0)
    fprintf(fwrite,"%.1f ",value);
      }
      fclose(fp);
    fclose(fwrite);
   }
}
return 0;

/// save as assign4_2.c

#include<stdio.h>
int main()
{
FILE* fp = fopen("filtered.txt","r");
FILE* fwrite = fopen("statistics.txt","w");
float max=0;
float min = 0;
float sum =0;
int count=0;
float value;
    if( fp != NULL )
    {
      fscanf(fp,"%f",&value);
    max = value;
    min = value;
    sum = sum +value;
    count++;
    while ( !feof(fp ) )
      {
      fscanf(fp,"%f",&value);
    if(value>max) max= value;
    if(value<min) min = value;
    sum = sum +value;
    count++;
      }
      fprintf(fwrite,"Minimum: %.1f ", min);
    fprintf(fwrite,"Maximum: %.1f ", max);
    fprintf(fwrite,"Average: %.1f ", (sum/count));

      fclose(fp);
    fclose(fwrite);
   }
return 0;
}