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

Write a c program Observe the usual guidelines regarding the initial comment sec

ID: 656477 • Letter: W

Question

Write a c program

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

1.   In data below temperatures in Chicago and 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 when the temperatures are read from the data file, the temperatures for Chicago are read into an array and the temperatures for San Francisco are read into a second array.

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

a. Determine the sum of the temperatures for the month in Chicago and the sum of the temperatures for the month in San Francisco. (Find the averages outside the loop).

b. Find and print the dates on which the temperature in San Francisco was greater than the temperature in Chicago.

c. Determine the high temperature in Chicago for the month and the date on which it occurred.

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

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

Date in August

Chicago Temperature

(degrees F)

San Francisco Temperature

(degrees F)

1

75

69

2

79

68

3

86

70

4

86

73

5

79

72

6

81

71

7

73

69

8

89

76

9

91

85

10

86

87

11

81

74

12

82

84

13

86

76

14

88

68

15

89

79

16

90

75

17

82

68

18

84

68

19

81

73

20

79

72

21

73

79

22

69

68

23

73

68

24

79

69

25

82

71

26

72

70

27

66

89

28

71

95

29

69

90

30

66

66

31

66

69

Date in August

Chicago Temperature

(degrees F)

San Francisco Temperature

(degrees F)

1

75

69

2

79

68

3

86

70

4

86

73

5

79

72

6

81

71

7

73

69

8

89

76

9

91

85

10

86

87

11

81

74

12

82

84

13

86

76

14

88

68

15

89

79

16

90

75

17

82

68

18

84

68

19

81

73

20

79

72

21

73

79

22

69

68

23

73

68

24

79

69

25

82

71

26

72

70

27

66

89

28

71

95

29

69

90

30

66

66

31

66

69

Explanation / Answer

#include<stdio.h>

#define MAX 32

struct temp

{

int month;

int day;

int degree;

};

//typedef struct temp temp;

int main()

{

FILE *fpr;

FILE *fpw;

int day,tmp1,tmp2,cnt=0,j=0,month=0;

float avg1=0,avg2=0;

int sum1=0,sum2=0,i,high,low,highIndex,lowIndex;

int dates[MAX];

char month1[12][10]={"Janaury","February","March","April","May","June","July","August","September","November","December"};

fpr=fopen("tempIn.txt","r");

fpw=fopen("tempOut.txt","w");

//create two arrays to hold data for chicago and sanfrancisco

struct temp chicagoData[MAX];

struct temp sanFrancData[MAX];

if( fpr==NULL)

{

printf("Error opening input file ");

return -1;

}

fscanf(fpr,"%d",&month);

//printf("Month=%d ",month);

while((fscanf(fpr,"%d%d%d",&day,&tmp1,&tmp2) )!= EOF)

{

chicagoData[cnt].day=day;

chicagoData[cnt].month=month;

sanFrancData[cnt].day=day;

chicagoData[cnt].degree=tmp1;

sanFrancData[cnt].degree=tmp2;

sanFrancData[cnt].month=month;

//printf("Data = %d %d %d ", chicagoData[cnt].day,chicagoData[cnt].degree, sanFrancData[cnt].degree);

cnt++;

}

//printf("cnt=%d ",cnt);

//find sum1 and sum2 of temps

for( i = 0; i< cnt;i++)

{

sum1+=chicagoData[i].degree;

sum2+=sanFrancData[i].degree;

//printf("sum1=%d sum2=%d ",sum1,sum2);

}

//printf("sum1=%d sum2=%d ",sum1,sum2);

//find average of all temp

avg1=(float)sum1/cnt;

avg2=(float)sum2/cnt;

printf("avg1 for chicago =%.2f avg2 for san franscisco=%.2f ",avg1,avg2);

//find temp of sanfransisco greater than chicago

for( i = 0; i< cnt;i++)

{

if( sanFrancData[i].degree > chicagoData[i].degree)

{

printf("temperature in San Francisco was greater than the temperature in Chicago: %d ",chicagoData[i].day);

dates[j++]=sanFrancData[i].day;

}

}

printf(" ");

//determine the high temperature in Chicago for the month and the date on which it occurred

high=chicagoData[0].degree;

for( i = 0; i< cnt;i++)

{

if( high < chicagoData[i].degree )

{

high=chicagoData[i].degree;

highIndex=i;

}

}

//. Determine the low temperature in San Francisco for the month and the date on which it occurred

low=sanFrancData[0].degree;

for( i = 0; i< cnt;i++)

{

if( low > sanFrancData[i].degree )

{

low=sanFrancData[i].degree;

lowIndex=i;

}

}

//check output file opened

if(fpw == NULL )

{

printf("can't write to file ");

return -1;

}

i=0;

//write to output file with dates for which sanfrancisco temp greater than chicago

fprintf(fpw,"dates and temperature of sanfranscisco which is greater than chicago for the Month= %s ",month1[month-1]);

while( i<j )

{

//printf("dates=%d temp=%d ",dates[i],sanFrancData[dates[i]].degree);

fprintf(fpw,"%d %d ",dates[i],sanFrancData[dates[i]-1].degree);

i++;

}

fprintf(fpw,"Avg1 for chicago=%.2f Avg2 for san francisco=%.2f ",avg1,avg2);

fprintf(fpw,"high temperature in Chicago =%d , low temperature in San Francisco =%d ",high,low);

printf("day on which high temp for Chicago= %d high temp for Chicago= %d, day on which low emp for sanfranscisco= %d low temp for San Francisco=%d, avg1 for chicago =%.2f avg2 for San Francisco=%.2f ",highIndex+1,high,lowIndex+1,low,avg1,avg2);

fclose(fpr);

fclose(fpw);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote