15. a. Write a program to process a collection of daily high temperatures. Your
ID: 3641503 • Letter: 1
Question
15. a. Write a program to process a collection of daily high temperatures.Your program should count and print the number of hot days (high
temperature 85 or higher), the number of pleasant days (high tem-
perature 60-84), and the number of cold days (high temperature less
than 60). It should also display the category of each temperature.
Test your program on the following data:
55 62 68 74 59 45 41 58 60 67 65 78 82 88 91
92 90 93 87 80 78 79 72 68 61 59
b. Modify your program to display the average temperature (a real
number) at the end of the run.
//create file for following data
Test your program on the following data: 55 62 68 74 59 45 41 58 60 67 65 78 82 88 91 92 90 93 87 80 78 79 72 68 61 59 and use this file
Can you solve this problem with creating file and using
#include ifstream inF; string name; please.............. I will really appreciate you i will definetly rate life saver thanks
Explanation / Answer
put temperature file in the same folder with your .cpp file
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//PROTOTYPES
int main()
{
//LOCAL DECLARATIONS
ifstream inF;
string name;
int temp;
int hotDays = 0;
int normalDays = 0;
int coldDays = 0;
int tempSum = 0;
int tempCount = 0;
cout << "Enter file name: ";
cin >> name;
inF.open(name.c_str()); //you need cstring or character array to open file
if (!inF)
{
cerr << "Cannot open file ";
exit(1);
}
while (inF >> temp)
{
if (temp >= 85)
hotDays++;
else if (temp < 60)
coldDays++;
else
normalDays++;
tempSum += temp;
tempCount++;
}
inF.close();
cout << " Number of hot day(s): " << hotDays << endl
<< "Number of pleasant day(s): " << normalDays << endl
<< "Number of cold day(s): " << coldDays << endl
<< " Average temperature: " << (double)tempSum / tempCount << endl;
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.