Please I need this program written in C An air quality index measuring the conce
ID: 3856810 • Letter: P
Question
Please I need this program written in C
An air quality index measuring the concentration of Carbon Monoxide in Omaha, NE on each day of the current year is stored in a file named CO_omaha_2017.txt. Write a program to read the data from the file into an array of integer values. Dimension the array to be of size 400 but count the number of values as you read them, so that your program can be used throughout the year. Return the count from your read data function and use the count as the array size in the rest of your functions (this is a partially filled array). Your program must calculate and print the average air quality index for the days in the file as well as the day number of the lowest and highest values. The day number is the array index of the largest/smallest value plus 1. Your program must then sort the data using the selection sort algorithm and display the sorted data. All results can be printed to the monitor. Your program must also create and display a frequency array that shows the number of days with each index value. You can assume that the air quality indices range from 0 to 19. To do this, use the air quality index, which is your array value as an index into a second frequency array, incrementing the appropriate element of the frequency array. The Unit 7 notes contain an example program with functions to create and display a frequency array. Your program should have separate functions to: 1) Read the data 2) Find the average 3) Find the day number of the highest and lowest air quality indices 4) Sort the data 5) Create the frequency array 6) Display the highest day, lowest day, average, sorted data, and frequency array Also calculate and display the median of the data set, this is the middle value when the data is sorted or the average of the 2 middle values when the data set size is even. Contents of : 3 6 5 5 6 6 10 14 7 9 8 9 8 14 14 8 8 5 7 7 10 10 6 5 5 7 6 6 6 8 7 5 7 6 7 7 10 10 8 8 18 19 11 13 9 9 9 5 8 13 3 5 2 2 2 2 3 6 5 5 6 2 3 2 2 3 5 3 3 3 3 3 3 3 5 6 6 6 6 6 6 6 7 8 9 9 9 8 2 2
Explanation / Answer
C Program:
#include <stdio.h>
#include <stdlib.h>
//Function that reads data from file
int readData(int data[])
{
int cnt=0;
FILE *fp;
//Opening file in read mode
fp = fopen("CO_Omaha_2017.txt", "r");
//Iterating over file data
while(!feof(fp))
{
//Reading a value from file and storing in array
fscanf(fp, "%d", &data[cnt]);
//Incrementing count
cnt += 1;
}
//Closing file
fclose(fp);
//Return count
return cnt;
}
//Function that calculates average
double findAverage(int data[], int cnt)
{
int i, sum=0;
double avg;
//Iterating over data in array
for(i=0; i<cnt; i++)
{
//Accumulating sum
sum += data[i];
}
//Calculating average
avg = sum / (double)cnt;
return avg;
}
//Function that finds largest and smallest index
void findLargestSmallest(int data[], int cnt, int *largest, int *smallest)
{
int i;
//Initially set to starting index
*largest = 0;
*smallest = 0;
//Iterating over data in array
for(i=0; i<cnt; i++)
{
//Checking for largest number
if(data[i] > data[*largest])
{
//Updating index of largest number
*largest = i;
}
//Checking for smallest number
if(data[i] < data[*smallest])
{
//Updating index of smallest number
*smallest = i;
}
}
}
//Selection sort
void selectionSort(int data[], int cnt)
{
int i, j, min, temp;
//Iterating over array
for (i=0; i<cnt-1; i++)
{
//Initially set i
min = i;
//Searching for minimum index
for (j = i+1; j < cnt; j++)
{
//Comparing values
if (data[j] < data[min])
{
//Updating minimum index
min = j;
}
}
//Swapping elements
temp = data[i];
data[i] = data[min];
data[min] = temp;
}
}
//Calculating frequency
void frequency(int data[], int cnt, int freq[])
{
int i;
//Iterating over data in array
for(i=0; i<cnt; i++)
{
//Accessing elements and updating frequency
freq[data[i]] += 1;
}
}
//Finding median
double median(int data[], int cnt)
{
//Checking size of array
if(cnt%2==0)
{
//For even number of elements
return((data[cnt/2] + data[cnt/2 - 1]) / 2.0);
}
else
{
//For odd number of elements
return data[cnt/2];
}
}
//Displaying results
void display(int largest, int smallest, double average, double medain, int data[], int freq[], int cnt)
{
int i;
//Printing results
printf(" Average air quality index: %f ", average);
printf(" Largest air quality index day number: %d ", (largest+1));
printf(" Smallest air quality index day number: %d ", (smallest+1));
printf(" Median of data set: %f ", medain);
printf(" Sorted Data: ");
//Iterating over sorted array
for(i=0; i<cnt; i++)
{
printf(" %4d ", data[i]);
}
printf(" Frequency Array: %-20s %-20s ", "Index", "Number of days");
//Iterating over frequency array
for(i=0; i<20; i++)
{
//Printing results
printf(" %-22d %-20d ", i, freq[i]);
}
}
//Main function
int main()
{
int data[400], cnt;
int freq[20] = {0};
double medianVal, average;
int largest, smallest;
//Reading data from file
cnt = readData(data);
//Finding average
average = findAverage(data, cnt);
//Finding largest and smallest index
findLargestSmallest(data, cnt, &largest, &smallest);
//Sorting array
selectionSort(data, cnt);
//Finding median
medianVal = median(data, cnt);
//Finding frequency array
frequency(data, cnt, freq);
//Displaying results
display(largest, smallest, average, medianVal, data, freq, cnt);
printf(" ");
return 0;
}