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

C++ Arrays and Design Assignment For this program, you will read in and analyze

ID: 3746409 • Letter: C

Question

C++

Arrays and Design Assignment

For this program, you will read in and analyze temperatures around the nation on a particular day. The input data will be in a text file called temperature.txt. The first line of the file will be a date; this can be read in as a string. This will be followed by an integer that gives the number of locations at which the temperatures were taken; this will be at least 2 and no more than 100. This is followed by that many lines (2 to 100), where each line has two integers representing the daily high and the daily low at one location. A short sample content of temperature .txt is given below.

The program should first read in the data from the file. Use two arrays, one to hold the high temperatures and the other to hold the low temperatures. Once the data has been read in, do the following analysis:

1. Find the average high and the average low for the day. Calculate the averages as real numbers, not integers.

2. Find the highest and the lowest temperature in the nation for the day.

Print the information in a readable format to a file “report.txt”, starting with a title with the date.

For the high temperatures only, calculate and draw a histogram. This means counting the number of temperatures that were less than 0, how many in the range 0 to 9, how many in the range 10 to 19, . . . , how many in the range 90 to 99, and how many were 100 or above. The counts must be in an array of size 12, one entry for the 12 ranges given. The counts should be plotted in a simple graph with a row of *’s giving the count in each range. The task for getting the correct values for the count array should be implemented as a function. What will its parameters be? You may assume that the number of counts in any range will be less than the standard width for the output. See the example below for the given sample input

Once your program is working correctly, use the temperature.txt file below

April 6, 2010
24
88 72
52 36
73 48
89 72
97 84
61 41
48 37
68 45
88 63
79 52
48 21
55 46
54 41
97 81
55 37
79 70
72 43
68 45
102 80
57 39
37 32
45 28
66 45
59 36

Explanation / Answer

Save program and temperature.txt in same folder

//Program

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

using namespace std;
int main() {
int low[100],high[100];
int n;
string date;
//Read file
ifstream inFile;
inFile.open("temperature.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1);
}
getline(inFile,date);
inFile >> n;
for(int i=0;i<n;i++) {
inFile >> high[i];
inFile >> low[i];
}
inFile.close();
//write file
ofstream outFile;
outFile.open("report.txt");
if( !outFile ) {
cout << "Error: file could not be opened" << endl;
exit(1);
}
outFile<<date<<endl;
//Find average high and average low in real no
double avgHigh=0,avgLow=0;
for(int i=0;i<n;i++) {
avgLow+=low[i];
avgHigh+=high[i];
}
avgLow/=n;
avgHigh/=n;
outFile<<"Average High : "<<avgHigh<<endl;
outFile<<"Average Low : "<<avgLow<<endl;

//Find the heighest and lowest temperature for the date
int heighest=high[0];
int lowest=low[0];
for(int i=1;i<n;i++) {
if(heighest<high[i])
heighest=high[i];
if(lowest>low[i])
lowest=low[i];
}
outFile<<"Heighest Temperature : "<<heighest<<endl;
outFile<<"Lowest Temperature : "<<lowest<<endl;
//Find the histogram for high temperature
int histogram[12]={0,0,0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<n;i++) {
if(high[i]<0)
histogram[0]+=1;
else if(high[i]>=100)
histogram[11]+=1;
else
histogram[high[i]/10 + 1]+=1; //This maps all value to appropriate index
}
//print histogram
outFile<<"Histogram"<<endl;
outFile<<"< 0 :";
for(int i=0;i<histogram[0];i++)
outFile<<"*";
outFile<<endl;
for(int i=1;i<11;i++) {
outFile<<(i-1)*10<<"-"<<(i-1)*10+9<<" :";
for(int j=0;j<histogram[i];j++)
outFile<<"*";
outFile<<endl;
}
outFile<<"> 100 :";
for(int i=0;i<histogram[11];i++)
outFile<<"*";
outFile<<endl;
outFile.close();
return 0;
}

//temperature.txt

April 6, 2010
24
88 72
52 36
73 48
89 72
97 84
61 41
48 37
68 45
88 63
79 52
48 21
55 46
54 41
97 81
55 37
79 70
72 43
68 45
102 80
57 39
37 32
45 28
66 45
59 36

//Output(Content of report.txt after executing program)

April 6, 2010

Average High : 68.2083

Average Low : 49.75

Heighest Temperature : 102

Lowest Temperature : 21

Histogram

< 0 :

0-9 :

10-19 :

20-29 :

30-39 :*

40-49 :***

50-59 :******

60-69 :****

70-79 :****

80-89 :***

90-99 :**

> 100 :*