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

You will write a C program that reads 10 temperatures from a file, calculates th

ID: 3577363 • Letter: Y

Question

You will write a C program that reads 10 temperatures from a file, calculates the average temperature, sorts the temperatures from low to high and writes the sorted temperatures back to another file. There will be no other questions for this exam.

Suggested process: Create a c file named YourNameExam2.c

1. Include any necessary libraries and write main function.

2. Write a struct that contains an array of double with 100 elements and an integer to count elements inserted named dbl_arr_t

3. Write function to read doubles from file into a dbl_arr_t named read_temps (remember to pass by reference and use -> to de-reference in called function)

4. Write function to print array to screen named print_temps

5. Test that you program reads the data properly by calling read_temps, then print_temps

6. Write function to calculate the sum of the temperatures named sum_temps

7. Write function to calculate the average temperature named avg_temp (use sum_temps()/count to calculate average)

8. Test that your program calculates the average by printing the average to screen

9. Write function to sort the temperatures from low to high named sort_temps (you can swap with or without a separate swap function)

10. Test that your program sorts properly by calling print_temps

11. Write function to write sorted data to a file named write_temps

12. Check that file was written

13. Close the editor and submit your source code file to Blackboard Remember to pass the struct to read and sort (and swap if you use a separate function) by reference. You can pass the struct by value for the other functions. The temis.txt file contains 10 temperatures. Use a for loop the reads from the file using fscanf 10 times.

Sample screen output from project: Count: 10 90.30 89.50 91.20 90.80 88.40 94.30 95.10 93.70 92.60 93.00 Average temp: 91.89 Count: 10 88.40 89.50 90.30 90.80 91.20 92.60 93.00 93.70 94.30 95.10

Explanation / Answer

#include<stdio.h>

FILE *file,*outfile;
struct temperatures
{
   double dbl_arr_t[100];
   int count;
};

void read_temps(struct temperatures *temp)
{
file = fopen("input.txt", "r");
double number;
int i=0;
while ( fscanf(file, "%lf", & number ) == 1 )
{
   temp->dbl_arr_t[i]=number;
   i++;

}
temp->count=i;
fclose(file);

}

void print_temps(struct temperatures *temp)
{
int i;
       for ( i = 0; i < temp->count; ++i)
       {
           printf("%lf ",temp->dbl_arr_t[i] );
       }
}
double sum_temps(struct temperatures *temp)
{
   int i;
   double sum=0;
       for ( i = 0; i < temp->count; ++i)
       {
           sum+=temp->dbl_arr_t[i];
       }
return sum;
}


void sort(struct temperatures *temp)
{   int c,d;
   for (c = 0 ; c < ( temp->count - 1 ); c++)
{
for (d = 0 ; d < temp->count - c - 1; d++)
{
if (temp->dbl_arr_t[d] > temp->dbl_arr_t[d+1])
{
double swap = temp->dbl_arr_t[d];
temp->dbl_arr_t[d] = temp->dbl_arr_t[d+1];
temp->dbl_arr_t[d+1] = swap;
}
}
}
}

double avg_temp(struct temperatures *temp)
{
  
           return (double)sum_temps(temp)/temp->count;
}

void write_temps(struct temperatures *temp)
{


       outfile = fopen("out.txt", "w");

int i=0;
while ( i<temp->count )
{
   fprintf(outfile,"%lf ",temp->dbl_arr_t[i]);
   i++;

}

fclose(file);
}
int main(int argc, char const *argv[])
{
   struct temperatures temp;

   read_temps(&temp);
   printf("Count is %d ",temp.count);
   print_temps(&temp);
   printf("Average of temperatures is %lf ", avg_temp(&temp));
   sort(&temp);
   printf("Count is %d ",temp.count);
   print_temps(&temp);
   write_temps(&temp);
   return 0;
}

=================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc temp.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Count is 10
90.300000
89.500000
91.200000
90.800000
88.400000
94.300000
95.100000
93.700000
92.600000
93.000000
Average of temperatures is 91.890000
Count is 10
88.400000
89.500000
90.300000
90.800000
91.200000
92.600000
93.000000
93.700000
94.300000
95.100000

==============================================================================

input.txt

90.30
89.50
91.20
90.80
88.40
94.30
95.10
93.70
92.60
93.00

===============================================

out.txt

88.400000
89.500000
90.300000
90.800000
91.200000
92.600000
93.000000
93.700000
94.300000
95.100000

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