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

Part 1: Daily Averages Consider the small input sample below. Please write c cod

ID: 3737241 • Letter: P

Question

Part 1: Daily Averages Consider the small input sample below.

Please write c code.

Part 1: Daily Averages Consider the small input sample below 3 11 12 05 1 8.7 3 11 11 56 143 8.6 3 11 13 01 163 8.9 3 10 18 05 1 7.3 3 10 19 01 1 7.3 2 28 01 02 2 -1.0 2 28 09 07 2-0.5 6 10 17 00 111 18.7 Notice that there are observations present for 4 different days (March 11th, March 10th, Feb. 28th and June 10th) Your task for part 1 is to write a program which reads an input file containing any number of observations and generates an output file called daily_averages_summary.txt containing the average temperature for each month/day in the input file The output will be a text file with one line for each month/day in the input file. Each line will have the format "month day average temperature", where temperature is printed with two decimal places (using the %.2f, specifier). The output must be printed ordered by month and day Days for which no observations were recorded must be omitted from the output. For example, on the input sample above, the output would be 2 28 -0.75 3 10 7.30 3 11 8.73 6 10 18.70

Explanation / Answer

// File daily_Avg.c

#include "stdio.h"

#include <stdlib.h>

#include <string.h>

#define ARRAY_SIZE 100

int main()

{

char fileName[] = "input_data.txt";

char outputFileName[] = "daily_average_summary.txt";

char line[ARRAY_SIZE];

int month[ARRAY_SIZE];

int day[ARRAY_SIZE];

double observations[ARRAY_SIZE];

int itr=0;

int count = 0;

FILE *fp = fopen(fileName, "r"); // read mode

if (fp == NULL)

{

perror("Error while opening the file. ");

exit(EXIT_FAILURE);

}

while(fgets(line, sizeof line, fp)!=NULL)

{ // keep looping until NULL pointer

count = 0;

char *token = strtok(line, " ");

while (token != NULL)

{

count++;

switch(count)

{

case 1:

month[itr] = atoi(token);

break;

case 2:

day[itr] = atoi(token);

break;

case 6:

observations[itr] = atof(token);

break;

}

printf("%s ", token);

token = strtok(NULL, " ");

}

itr++;

}

fclose(fp);

// start write in file code

FILE *outPutFp = fopen(outputFileName, "w"); // write mode

if (outPutFp == NULL)

{

perror("Error while opening the file. ");

exit(EXIT_FAILURE);

}

double average = 0.0;

double sum[ARRAY_SIZE] = {0.0};

int obsCount[ARRAY_SIZE] = {0};

for(int i = 0; i < ARRAY_SIZE; )

{

for(int j = 0; j < ARRAY_SIZE; j++)

{

if(month[i] == month[j] && day[i] == day[j]) // if two dates are same, calculate sum and count

{

sum[i] += observations[i];

obsCount[i] += 1;

}

}

if(month[i] > 0 )

{

average = sum[i] / obsCount[i];

fprintf(outPutFp, "%d %d %.2f ", month[i], day[i], average);

}

i = i + obsCount[i];

}

fclose(outPutFp);

return 0;

}