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

Write a C program to process a collection of 30 daily high temperatures read fro

ID: 3662519 • Letter: W

Question

Write a C program to process a collection of 30 daily high temperatures read from standard I/O. Your program should count and print the number of hot days (high temperature 30 or higher), the number of pleasant days (high temperature 18-29), and the number of cold days (high temperature less than 18). Also calculate the average highest temperature for the 30 days. The average should be a float and printed with 2 decimal places. Define a constant for the number of days. Test your program with the file test.txt. Assume the executable is named a.out then you can run your program as follows: ./a.out < test.txt

(Please be clear with steps, as I am having a hard time learning this program)

Explanation / Answer

Please use the following code. If any small changes you feel is required, please do the same and you can use. Please let me know the results. Please do ask if you have any doubts.

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main (void) {
   DIR *dir;
   struct dirent *dirent;
   char dev[16]; // Dev ID
   char devPath[128]; // Path to device
   char buf[256]; // Data from device
   char tmpData[6]; // Temp C * 1000 reported by device
   char path[] = "/sys/bus/w1/devices";
   ssize_t numRead;
   FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
   float temperature[100],HighestTemp=00f;
   String temp[30];
   int i=0,countHot=0,countPleasent=0,countCold=0;
   int days=30;
   float AverageTemperature;
/*Read the file in which you have temperature readings line by line*/
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(EXIT_FAILURE);

while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu : ", read);
printf("%s", line);
       temp[i]=line;/*Since the line has only temparature number*/
       i++;
}

fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
     
/*Convert the string to number*/
       for(i=0;i<days;i++){
                   temperature[i] = (float) strtol(temp[i], (char **)NULL, 10);
                   if(temperature[i]>=30.00f){
                   countHot++;
                   HighestTemp+=HighestTemp;
                   }else if(temperature[i]<30.00f && temperature[i]>=18.00f){
                   countPleasent++;
                  
                   }else{
                   countCold++;
                   }

       }
       printf("Total number of hot days $f",&countHot);
       printf(" Total number of pleasent days $f",&countPleasent);
       printf(" Total number of cold days $f",&countCold);
      
       /*calculate the average of hot days*/
       AverageTemperature=HighestTemp/countHot;      
      
/* return 0; --never called due to loop */
}

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