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

Build a program that will accomplish these tasks: 1. Read from 0 to 50 double te

ID: 3546825 • Letter: B

Question

Build a program that will accomplish these tasks:


1. Read from 0 to 50 double temperatures from a file, "temperature.txt".

2. Neatly display each temperature on the screen.

3. Compute the average of the temperatures and display the average on the screen.


Additional requirements:

a) The only global item that can be used is the MAXNUM of temps as a #define of 50

b) It is possible that there may not be anything in the file. (That would be a problem. In that case, don't display a report or compute the average. Just display an error message.

c) It is possible that the file may not have 50 valid temperatures in it - It may only have a couple of them - process accordingly.

d) There may be more than 50 valid temperatures in the file. If so, only read in the first 50.

e) Put in any necessary error checking. There may be invalid temperatures in the file. Do not include them, they must be ignored - since it is not possible to ask the file to re-enter a number as it is with a user doing the entry. Valid temperatures are between -40 and 120.

Explanation / Answer

#include<stdio.h>


#define MAX_NUM_OF_TEMP 50


int main() {


FILE* fp;

fp = fopen("temprature.txt","r");

  

double temprature[MAX_NUM_OF_TEMP];


int i=0;


while(fp && i<MAX_NUM_OF_TEMP) {


fscanf(fp,"%lf",&temprature[i]);


if(feof(fp)) {

break;

}


if(temprature[i]>=-40.0 && temprature[i]<=120.0) {

i++;

}

  

}


fclose(fp);

  

if(i>0) {


double average =0.0;

  

int j=0;


printf("Temprature read from file ");


for(j=0;j<i;j++) {


printf("%3.2f ", temprature[j]);


average+=temprature[j];


}


average/=i;


printf(" Avergae Temprature = %f ",average);


} else {

printf("Temprature.txt file is empty ");

}


return 0;

}

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