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

include <stdio.h> #define _CRT_SECURE_NO_WARNINGS int main() { FILE *fpIn = fope

ID: 3811337 • Letter: I

Question

include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS

int main()

{

       FILE *fpIn = fopen("ChicagoSanfranciscoTemperatures.txt", "r");

       //Checking to make sure the file opened successfully.

       if (fpIn == NULL)

       {

              printf("File not found. Terminating... ");

              return 0;

       }

       double avgChicago = 0.0, avgSanfrancisco = 0.0;

       int day, C, SF, sfMoreThanc = 0, sfAndCLessThan80 = 0, numOfDays = 0;;

       int lowTempChicago, lowTempChicagoDay, highTempSF, highTempSFDay;

      

      

       // Reading the variables

       while ((fscanf(fpIn, "%d%d%d", &day, &C, &SF)) == 3)

       {

              numOfDays++;

              /* The average temperature for the month in Chicago and the average temperature

              for the month in San Francisco.*/

              avgChicago += C;

              avgSanfrancisco += SF;

              /* The number of days the temperature in San Francisco bigger than the

              temperature in Chicago.*/

              if (SF > C)

                     sfMoreThanc++;

              // Below 80 degrees

              if (SF < 80 && C < 80)

                     sfAndCLessThan80++;

      

              if (day == 1)

              {

                     lowTempChicago = C;

                     lowTempChicagoDay = 1;

              }

              else if (C < lowTempChicago)

              {

                     lowTempChicago = C;

                     lowTempChicagoDay = day;

              }

             

              if (day == 1)

              {

                     highTempSF = C;

                     highTempSFDay = 1;

              }

              else if (SF > highTempSF)

              {

                     highTempSF = C;

                     highTempSFDay = day;

              }

       }

       avgChicago /= numOfDays;

       avgSanfrancisco /= numOfDays;

       // printing the averages counts and the maximum and minimum temperatures and dates

      

       FILE *fpOut = fopen("ChicagoSanfranciscoTemperatures.txt", "w");

       fprintf(fpOut, "The average in Chicago is: %f. ", avgChicago);

       fprintf(fpOut, "The average in San Francisco is: %f. ", avgSanfrancisco);

       fprintf(fpOut, "The number days the temperature in San Francisco was bigger than that of Chicago is: %d. ", sfMoreThanc);

       fprintf(fpOut, "The number days the temperature in both cities in the same day was smaller than 80 degrees is: %d. ", sfAndCLessThan80);

       fprintf(fpOut, "The low temperature for Chicago is %d, and the date on which it occurred %d. ", lowTempChicago, lowTempChicagoDay);

       fprintf(fpOut, "The high temperature in August for San Francisco is %d, and the date is %d. ", highTempSF, highTempSFDay);

       // closing files

       fclose(fpIn);

       fclose(fpOut);

       return 0;

}

Write a C program NOT C++  

Upload the source code (.c file) and the output file

Please write the correct program for this problem

Observe the usual guidelines regarding the initial comment section, indenting, and so on.

1.   In the above programing, a data file of dates, temperatures in Chicago and temperatures in San Francisco for the month of August, 2009, was created and used in a program. In that program data had to be read and used in a single loop since we had not studied how to store values in an array.

Modify the program so that arrays for dates, Chicago temperatures, and San Francisco temperatures are declared with 31 elements (the maximum number of days in any month) and when the temperatures are read from the data file, dates are read into the array of dates, the temperatures for Chicago are read into the Chicago array and the temperatures for San Francisco are read into the San Francisco array. Include the number 31 as the first entry in your data file, and read in the number of days in the month. Your loop counter will start with the value 0 and continue until the number of days -1 is reached. Use a separate for loop just to read in the dates and temperatures.

In separate for loops (after all the temperatures have been read) calculate the following:

(Do not put all the calculations in the same loop.)

a. Determine in a for loop the sum of temperatures for the month in Chicago and the sum of temperatures for the month in San Francisco. Calculate and print averages outside the loop.

b. Find and print the dates on which the temperature in San Francisco was greater than the temperature in Chicago. These dates can be printed from the loop in which they are determined.

c. Determine the low temperature in August for Chicago and the date on which it occurred.

d. Determine the high temperature in San Francisco for the month and the date on which it occurred.

e. Find and print the number of days the temperature in both cities (on the same day) was below 80 degrees.

Print the averages, count, dates and high and low temperatures to a file. Close the files at the end of the program. The entire program may be written in function main.

Explanation / Answer

Hi,

Please find the attached files. Please change temperatures in input file as per your convenience.

main.c file

#include <stdio.h>

int main()
{
FILE *fptr = fopen("temp.txt", "r");
//Checking to make sure the file opened successfully.
if (fptr == NULL)
{
printf("File not found. Terminating... ");
return 0;
}
//Variable declaration
int numOfDays, date[31],dateLowChicago, dateHighSF,daysLessThan80 = 0;
float Chicago[31],SF[31],sumChicago = 0.0,sumSF = 0.0, avgChicago, avgSF,tempLowChicago = 500.0, tempHighSF = 0.0;
  
//Reading number of days
fscanf(fptr,"%d ",&numOfDays);

//Reading into array
for(int i = 0; i < numOfDays; i++)
{
fscanf(fptr,"%d %f %f ",&date[i], &Chicago[i], &SF[i]);
}
fclose(fptr);
  
//Creating output file
fptr = fopen("tempout.txt","w");
if (fptr == NULL)
{
printf("File not created. Terminating... ");
return 0;
}
  
//Calculate and print averages
for (int i = 0; i < numOfDays; i++)
{
sumChicago += Chicago[i];
sumSF += SF[i];
}
avgChicago = sumChicago / numOfDays;
avgSF = sumSF / numOfDays;
fprintf(fptr, "The average in Chicago is: %f. ",avgChicago);
fprintf(fptr, "The average in SF is: %f. ",avgSF);
  
// Find temperature difference dates
fprintf(fptr, "The dates in which the temperature in San Francisco was greater than the temperature in Chicago are");
for (int i = 0; i < numOfDays; i++)
{
if (Chicago[i] < SF[i])
fprintf(fptr," %d",date[i]);
}
fprintf(fptr," ");
  
//Find lowest temp in Chicago
for (int i = 0; i < numOfDays; i++)
{
if(tempLowChicago > Chicago[i])
{
tempLowChicago = Chicago[i];
dateLowChicago = date[i];
}
}
fprintf(fptr, "The low temperature for Chicago is %f, and the date on which it occurred %d. ", tempLowChicago, dateLowChicago);
  
// Find Highest temp in SanFransico
for (int i = 0; i < numOfDays; i++)
{
if(tempHighSF < SF[i])
{
tempHighSF = SF[i];
dateHighSF = date[i];
}
}
fprintf(fptr, "The high temperature for SF is %f, and the date on which it occurred %d. ", tempHighSF, dateHighSF);
  
//Find days in which temp is less than 80
for (int i = 0; i < numOfDays; i++)
{
if (Chicago[i] < 80.0 && SF[i] < 80.0)
daysLessThan80++;
}
fclose(fptr);

return 0;
}

input file:

20
1 1.0 1.0
2 2.0 20.0
3 3.0 3.0
4 4.0 4.0
5 5.0 5.0
6 6.0 6.0
7 7.0 7.0
8 8.0 8.0
9 9.0 9.0
10 10.0 10.0
11 11.0 11.0
12 12.0 12.0
13 13.0 13.0
14 14.0 14.0
15 15.0 15.0
16 16.0 16.0
17 17.0 17.0
18 18.0 18.0
19 19.0 19.0
20 20.0 20.0
21 21.0 21.0
22 22.0 22.0
23 23.0 23.0
24 24.0 24.0
25 25.0 25.0
26 26.0 26.0
27 27.0 27.0
28 28.0 28.0
29 29.0 29.0
30 30.0 30.0
31 31.0 31.0

output file:

The average in Chicago is: 10.500000.
The average in SF is: 11.400000.
The dates in which the temperature in San Francisco was greater than the temperature in Chicago are 2
The low temperature for Chicago is 1.000000, and the date on which it occurred 1.
The high temperature for SF is 20.000000, and the date on which it occurred 2.