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

For the program assigned below, submit the following: Copy of the source code (.

ID: 3769334 • Letter: F

Question

For the program assigned below, submit the following:

Copy of the source code (.c files) input file and output file saved as an electronic attachment to C Assignment 8 located in under Assignments.

The following data provided by the National Weather Service, showing the average high and low temperatures for Indianapolis for each month of the year.

Month

Average High Temp

Average Low Temp

1

35.6

20.5

2

40.2

23.9

3

51.7

32.8

4

63.4

42.7

5

72.8

52.6

6

81.9

62.1

7

85.0

65.8

8

84.0

64.4

9

77.6

56.2

10

65.3

44.7

11

52.2

35.1

12

38.9

24.4

                                   

Create a new project in Visual Studio.

Then create a data file in Notepad with two columns, with high temperatures in the column on the left and corresponding low temperatures in the column on the right. Do not include column labels or dates.

Save the file in the project folder. (If you have trouble saving to the project, you may save to the h drive and include the directory in the filename in the fopen statement, making sure to use double slashes: “h:\filename.txt”.)

In the program check to make sure the file opened successfully.

Then write a program to read in the 12 sets of temperature data using a for or while loop. If using a for loop, add the number of lines at the top of the data file and read in the number of lines from the file. If using a while loop, use a sentinel (-999 at the end of the data).

As values are read in the loop calculate the necessary sums and values for the following (Do not determine any of the values merely by inspection):

The average high temperature for the year and the average low temperature for the year.

The number of months that the average high temperature is more than 60 degrees.

The number of months that the average low temperature is less than 50 degrees.

Write the averages high, the average low temperatures and the number of months that the high is over 60 and the low is below 50 to an output file. Close both the files at the end of the program. Submit a sample execution with your .c file copy, input data file, and output fil

Month

Average High Temp

Average Low Temp

1

35.6

20.5

2

40.2

23.9

3

51.7

32.8

4

63.4

42.7

5

72.8

52.6

6

81.9

62.1

7

85.0

65.8

8

84.0

64.4

9

77.6

56.2

10

65.3

44.7

11

52.2

35.1

12

38.9

24.4

Explanation / Answer

Complete Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MONTHS 12 // number of months in the year
#define TYPES 2 // high and low temperatures

void readData(char filename[20], double temperatures[MONTHS][TYPES]);
double averageHigh(double temperatures[MONTHS][TYPES]);
double averageLow(double temperatures[MONTHS][TYPES]);
int countHighTempOver60(double temperatures[MONTHS][TYPES]);
int countLowTempBelow50(double temperatures[MONTHS][TYPES]);
void printResults(char filename[20], double averageHighTemperature, double averageLowTemperature, int numberOfHighTemperaturesOver60, int numberOfLowTemperaturesBelow50);

int main()
{
char inputfilename[20];
char outputfilename[20];

double temperatures[MONTHS][TYPES];
double averageHighTemperature;
double averageLowTemperature;
int numberOfHighTemperaturesOver60;
int numberOfLowTemperaturesBelow50;

printf("Enter the input file name: ");
scanf("%s", inputfilename);

printf("Enter the output file name: ");
scanf("%s", outputfilename);

readData(inputfilename, temperatures);

averageHighTemperature = averageHigh(temperatures);

averageLowTemperature = averageLow(temperatures);

numberOfHighTemperaturesOver60 = countHighTempOver60(temperatures);

numberOfLowTemperaturesBelow50 = countLowTempBelow50(temperatures);

printResults(outputfilename, averageHighTemperature, averageLowTemperature, numberOfHighTemperaturesOver60, numberOfLowTemperaturesBelow50);

return 0;
}

void readData(char filename[20], double temperatures[MONTHS][TYPES])
{
FILE *infile = fopen(filename, "r");

if(infile == NULL)
{
  printf("The %s file cannot be opened! ", filename);
      exit(EXIT_FAILURE);
}
  
char ht[10], lt[10];

for(int i = 0; fscanf(infile, "%s %s", ht, lt) != EOF && i < MONTHS; i++)
{
  temperatures[i][0] = atof(ht);
  temperatures[i][1] = atof(lt);
}

fclose(infile);
}

double averageHigh(double temperatures[MONTHS][TYPES])
{
double totalHigh = 0;

for(int i = 0; i < MONTHS; i++)
{
  totalHigh = totalHigh + temperatures[i][0];
}

double avgHigh = totalHigh / MONTHS;

return avgHigh;
}

double averageLow(double temperatures[MONTHS][TYPES])
{
double totalLow = 0;

for(int i = 0; i < MONTHS; i++)
{
  totalLow = totalLow + temperatures[i][1];
}

double avgLow = totalLow / MONTHS;

return avgLow;
}

int countHighTempOver60(double temperatures[MONTHS][TYPES])
{
int count = 0;
for(int i = 0; i < MONTHS; i++)
{
  if(temperatures[i][0] > 60)
  {
   count++;
  }
}

return count;
}

int countLowTempBelow50(double temperatures[MONTHS][TYPES])
{
int count = 0;

for(int i = 0; i < MONTHS; i++)
{
  if(temperatures[i][1] < 50)
  {
   count++;
  }
}

return count;
}

void printResults(char filename[20], double averageHighTemperature, double averageLowTemperature, int numberOfHighTemperaturesOver60, int numberOfLowTemperaturesBelow50)
{
FILE *outfile = fopen(filename, "w");

fprintf(outfile, "Average High Temperature: %.2f ", averageHighTemperature);
fprintf(outfile, "Average Low Temperature : %.2f ", averageLowTemperature);
fprintf(outfile, "Number of High Temperatures Over 60: %d ", numberOfHighTemperaturesOver60);
fprintf(outfile, "Number of Low Temperatures below 50: %d ", numberOfLowTemperaturesBelow50);

fclose(outfile);
}

Input file: input.txt

35.6 20.5
40.2 23.9
51.7 32.8
63.4 42.7
72.8 52.6
81.9 62.1
85.0 65.8
84.0 64.4
77.6 56.2
65.3 44.7
52.2 35.1
38.9 24.4

Output on console:

Output file: output.txt

Average High Temperature: 62.38
Average Low Temperature : 43.77
Number of High Temperatures Over 60: 7
Number of Low Temperatures below 50: 7