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

Use the temperatures.txt ( 67 67 69 71 77 79 80 80 82 85 87 88 91 93 94 95 95 96

ID: 675235 • Letter: U

Question

Use the temperatures.txt ( 67 67 69 71 77 79 80 80 82 85 87 88 91 93 94 95 95 96 94 90 85 82 81 77 74) that contains 25 temperature values to be read for each day.  Your program should read thosevalues and save them to an array. When finished calculating the max, min and avg, it shouldwrite those values to the end of that file.  Therefore,
1) Delete the get_values() function in your existing code. You will no longer need it.
2) In its place, define a read_temps() function that will ask the user the name of the file tobe read, reads that file and saves the read values to an array.
3) The calc_values() function you already have should be called from either main() orfrom read_temps(). Whichever function calls calc_values(), it needs to pass thearray to it by reference. In addition to calculating the max, min and avg as you did previously ,it will write those values to the same external file. It can do so by either building a new fileof the same name with all the hourly values as well as the calculated values, or it canappend the existing file with the calculated values

This is what I have so far and I'm stumped by the directions:

#include <stdio.h>
#include <stdlib.h>
void calc_results(int time[], int size);
#define read_temps(temperatures.txt);
int main ()
{
rand();
//Declare temperature array with size 25 since we are going from 0 to 24
int i, temp[25];
for (i = 0; i < 25; i++) {
temp[i] = get_value();
}
//Temperature for the day of October 14, 2015
printf("Temperature conditions on October 14, 2015: ");
printf(" Time of day Temperature in degrees F ");
for (i = 0; i < 25; i++) {
printf( "%d %d ",i,temp[i]);
}
calc_results(temp, 25);
return 0;
}
int get_value() {
int random;
//Generate random number from 60 to 100
random = rand() % 40 + 60;
return random;
}
void calc_results(int temp[], int size) {
int i, min, max, sum = 0;
float avg;
min = temp[0];
max = temp[0];
//Loop that calculates min,max, sum of array
for (i = 0; i < size; i++) {
if (temp[i] < min) {
min = temp[i];
}
if (temp[i] > max) {
max = temp[i];
}
sum = sum + temp[i];
}
avg = (float) sum / size;
printf(" Min Temperature for the day is : %d ", min);
printf("Max Temperature for the day is :%d ", max);
printf("Average Temperature for the day is : %f ", avg);
}

Explanation / Answer

/**C program that prompts user to enter the name of the input file called as "input.txt" and finds the min, max and average values of temperatures and write to "output.txt" file */

//temperature.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//function prototype
void calc_results(int time[], int size);
void read_temps(int temp[]);

//set size of array as global value
#define SIZE 25

int main ()
{
  
   rand();
   //Declare temperature array with size 25 since we are going from 0 to 24
   int i, temp[SIZE];
  

   read_temps(temp);

   //Temperature for the day of October 14, 2015
   printf("Temperature conditions on October 14, 2015: ");
   printf(" Time of day Temperature in degrees F ");
  
   for (i = 0; i < SIZE; i++)   
       printf( "%d %d ",i,temp[i]);
  

   //call the metod calc_results(temp, SIZE);
   calc_results(temp, SIZE);

  

   //pause the program output on console until user enters a key
   getch();
   return 0;
}


/**The method read_temps that takes the input array temp
and prompt user to enter the name of the input file
"input.txt" and then reads the tempertures from the file
for 24 hours of day */
void read_temps(int temp[])
{

   char fileName[50];
   int temperature;
   int counter=0;
   printf("Enter input file name : ");
   //prompt for file name
   scanf("%s",fileName);

   //open the input file
   FILE *fp=fopen(fileName, "r");

   //check if file exists or not
   if(!fp)
   {
       printf("File doesnot exist. ");
       getch();
       //if not exit, close the program
       exit(0);
   }

   //read temperatures from the file input.txt until end of file is encountered
   while(fscanf(fp,"%d",&temperature)!=EOF)
   {
       //store the values in the temp array
       temp[counter]=temperature;
       //incremnt the coutner by one
       counter++;
   }

   //close the input file stream fp
   fclose(fp);
}
void calc_results(int temp[], int size)
{
   int i, min, max, sum = 0;
   float avg;
   min = temp[0];
   max = temp[0];
   //Loop that calculates min,max, sum of array
   for (i = 0; i < size; i++)
   {
       if (temp[i] < min)
       {
           min = temp[i];
       }


       if (temp[i] > max)
       {
           max = temp[i];
       }
       sum = sum + temp[i];
   }

   avg = (float) sum / size;


   //open an external output file
   FILE *fout=fopen("output.txt","w");


   //Temperature for the day of October 14, 2015
   fprintf(fout,"Temperature conditions on October 14, 2015: ");
   fprintf(fout," Time of day Temperature in degrees F ");
   //write time of day and temperature
   for (i = 0; i < SIZE; i++)
   {
       fprintf( fout,"%d %d ",i,temp[i]);
   }

   printf(" Min Temperature for the day is : %d ", min);
   printf("Max Temperature for the day is :%d ", max);
   printf("Average Temperature for the day is : %f ", avg);

   //write min ,max and avg to the file "output.txt"
   fprintf(fout," Min Temperature for the day is : %d ", min);
   fprintf(fout,"Max Temperature for the day is :%d ", max);  
   fprintf(fout,"Average Temperature for the day is : %f ", avg);

   //close the output file stream
   fclose(fout);
}

------------------------------------------------------------------------------------------------------------------------------------------------------

Sample input.txt file

7 67 69 71 77 79 80 80 82 85 87 88 91 93 94 95 95 96 94 90 85 82 81 77 74

------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

output.txt

Temperature conditions on October 14, 2015:

Time of day   Temperature in degrees F

0        7
1        67
2        69
3        71
4        77
5        79
6        80
7        80
8        82
9        85
10        87
11        88
12        91
13        93
14        94
15        95
16        95
17        96
18        94
19        90
20        85
21        82
22        81
23        77
24        74

Min Temperature for the day is : 7
Max Temperature for the day is :96
Average Temperature for the day is : 80.760002

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