In: Computer Science
You will create a datafile with the following information: 70 80 90 55 25 62 45 34 76 105You will then write the program to read in the values from the datafile and as you read in each number, you will then evaluate it through an if statement for the weather of the day. For example, when you read in the value 70 from the datafile, you should print to the screen that the temperature is 70° and it is nice outside. After reading in all the values, you will determine which is the highest temperature and which is the lowest temperature. For example, your output will look like this:
The temperature for today is 70. It is nice outside.
The temperature for today is 80. It is getting warm outside.
The temperature for today is 90. It is hot outside.
The temperature for today is 55. It is cool outside.
The temperature for today is 25. It is really cold outside.
The temperature for today is 62. It is comfortable outside.
The temperature for today is 45. It ischilly outside.
The temperature for today is 34. It is cold outside.
The temperature for today is 76. It is nice outside.
The temperature for today is 105. It is really hot outside.
The high temperature for today is 105.
The low temperature for today is 25
Hints: You will need to create an if statement after you read your values in from the database to check the temperatures.
The ranges should be:
Above 100 –really hot
90-100 –hot
80-90 –getting warm
70-80 –nice
60-70 –comfortable
50-60 –cool
40-50 –chilly
30-40 –cold
20-30 –really cold
Below 20 –freezing
Notice that the number values such as 90 shows up in both selections. Instead of 90, one of them has to be 89 and so on. You will determine where the cutoffs are. Do not hard code the output information. Make sure you are running it through an if statement because my datafile will have different values than yours.
//Make sure to save you temperature data in a text file named "temperature.txt"
#include <iostream>
#include <fstream>
using namespace std;
void printList(double arr[], int n);
double getLargest(double arr[], int n);
double getSmallest(double arr[], int n);
int main()
{
double arr[200];
int n;
ifstream inFile;
inFile.open("temprature.txt");
if (inFile.fail())
{
cout << "Unable to open file" << endl;
return -1;
}
n = 0;
while (!inFile.eof())
{
inFile >> arr[n];
n++;
}
inFile.close();
cout << "Loaded tempratures from file.....\n" << endl;
printList(arr, n);
cout << "The high temperature for today is: " << getLargest(arr, n) << endl;
cout << "The low temperature for today: " << getSmallest(arr, n) << endl;
return 0;
}
void printList(double arr[], int n)
{
int i;
for(i=0;i<n;i++){
if(arr[i]<20)
printf("The temperature for today is %f. It is freezing outside\n",arr[i]);
else if(arr[i]>=20 && arr[i]<30)
printf("The temperature for today is %f. It is really cold outside\n",arr[i]);
else if(arr[i]>=30 && arr[i]<40)
printf("The temperature for today is %f. It is cold outside\n",arr[i]);
else if(arr[i]>=40 && arr[i]<50)
printf("The temperature for today is %f. It is chilly outside\n",arr[i]);
else if(arr[i]>=50 && arr[i]<60)
printf("The temperature for today is %f. It is cool outside\n",arr[i]);
else if(arr[i]>=60 && arr[i]<70)
printf("The temperature for today is %f. It is comfortable outside\n",arr[i]);
else if(arr[i]>=70 && arr[i]<80)
printf("The temperature for today is %f. It is nice outside\n",arr[i]);
else if(arr[i]>=80 && arr[i]<90)
printf("The temperature for today is %f. It is getting warm outside\n",arr[i]);
else if(arr[i]>=90 && arr[i]<100)
printf("The temperature for today is %f. It is hot outside\n",arr[i]);
else if(arr[i]>=100 )
printf("The temperature for today is %f. It is hot outside\n",arr[i]);
}
cout << endl;
}
D
double getLargest(double arr[], int n)
{
double largest = arr[0];
for (int i = 1; i < n; i++)
{
if (largest < arr[i])
largest = arr[i];
}
return largest;
}
double getSmallest(double arr[], int n)
{
double smallest = arr[0];
for (int i = 1; i < n; i++)
{
if (smallest > arr[i])
smallest = arr[i];
}
return smallest;
}
//SAMPLE OUTPUT
******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION
******************************************************************************************