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

I need to write a filter program in c language that reads the numbers from a fil

ID: 3545340 • Letter: I

Question

I need to write a filter program in c language that reads the numbers from a file (input.txt), and writes the positive numbers to a new file (filtered.txt), eliminating the negative numbers. Then i need to write a second program that computes the minimum, maximum, and average of numbers stored in the file filtered.txt that we just created.

The file input txt contains negative and positive numbers m 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 you program using the example below (Create input.txt having the numbers in figure 1 manually) Name your file assign4.1c 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.2c

Explanation / Answer

TO SEPERATE POSITIVE NUMBERS ;


#include <stdio.h>

int main(void) {
    double positive;
    FILE *input,*filtered;
    input=fopen("input.txt","r+");
    while(fscanf(input,"%lf",&positive)!=-1)
    {
        if(positive>0)
        {
            fprintf(filtered,"%lf ",positive);
        }
    }
        return 0;
}


TO FIND MIN , MAX AND AVG :


#include <stdio.h>

int main(void) {
    double positive,max=0,min=99999999999,avg=0,sum=0,count=0;
    FILE *filtered,*output;
    filtered=fopen("filtered.txt","a+");
    output=fopen("statistics.txt","w+");
    while(fscanf(filtered,"%lf",&positive)!=-1)
    {
        count++;
        sum+=positive;
        if(max<positive)
            max=positive;
        if(min>positive)
            min=positive;
    }
    avg=sum/count;
    fprintf(output,"Minimum : %lf ",min);
    fprintf(output,"Maximum : %lf ",max);
    fprintf(output,"Average : %lf ",avg);
    return 0;
}