C++ Coding HW! In this lab, you will be examining a set of temperatures collecte
ID: 3604206 • Letter: C
Question
C++ Coding HW!
In this lab, you will be examining a set of temperatures collected over a twenty four hour period. Be sure to make use of an array to store these temperatures. You will be required to read in the data points from a file. After reading in the data, you will need to write a segment of code that will find the average of the data set. Make sure to print out the average in main. You will also need to write code to display the temperatures in the table format (on the screen) shown below.
Table Format:
Hour Temperature Difference from the Average
0 35 +/- ???
1 32 +/- ???
After you finish printing out the average and the data in the format shown above, you need to write additional code that will find the lowest temperature in the array. You could sort the data, and grab the piece of data in the first position. However, this will result in no credit. (We will cover sorting the array in two more weeks.) You will have to use a linear search to find the lowest temperature. Once you find it, print out the value you found. In addition to printing out the value, please print out the position where the value was found. This can be done by having a for loop run through the array and search for the value using an if statement such as:
int low = tempArray[0];
int lowPosition = 0;
for (int i = 0; i < sizeOfArray; ++i)
if (tempArray[i] < low)
{
low = tempArray[i];
lowPosition = i;
}
Once you have written this code, write additional code that does the same thing to find the highest value and its position in the array. Print both the lowest and highest value and the position where it was found in the array. Put this output on the screen after the table that you have already printed on the screen.
Data:
Below is the set of data that you should include in your text file. Remember! Your input text file must be in the same folder as your cpp file.
35 32 31 28 25 22 23 25 26 30 32 38 41 43 45 48 52 55 49 44 43 42 49 37
Please include purpose of program. 3-4 sentences. Thanks will rate!
also, if you can answer these questions!
Why would you use an array to store data instead of inputting each value, processing it from a single variable location, and then read in the next value into that variable location? We learned how to read a value, total it, and find an average when we covered loops. Why would you put those values into an array to find the average?
Hour Temperature Difference from the Average
0 35 +/- ???
1 32 +/- ???
Explanation / Answer
The code in c++
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
// intialise the array containg 24 temperatures
int temp[24] = { 0 }, indx = 0, minTemp = 0, maxTemp = 0 , minTempPos = 0, maxTempPos = 0;
double avg = 0;
string tempLine,fileName;
// get the file name with the extension
cout << "Enter the file name to read data from (with extension) : " ;
cin >> fileName;
//try to open the file named tempFile.txt containg the temperatures
ifstream tempFile(fileName);
// check if the file is open
if (tempFile.is_open())
{
// get the line in the file
while (getline(tempFile, tempLine))
{
// get the temperatures into the array of temperatures
int tensPlace = 0, unitPlace = 0;
for (int i = 0; i < tempLine.size(); i++)
{
tensPlace = 0;
unitPlace = 0;
if (tempLine[i] == ' ')
{
continue;
}
tensPlace = tempLine[i++] - 48;
if (i >= tempLine.size())
{
unitPlace = tensPlace;
tensPlace = 0;
temp[indx] = tensPlace * 10 + unitPlace;
break;
}
if (tempLine[i] == ' ')
{
unitPlace = tensPlace;
tensPlace = 0;
}
else
{
unitPlace = tempLine[i] - 48;
}
temp[indx++] = tensPlace * 10 + unitPlace;
}
// FIND AVERAGE
for (int i = 0; i < 24; i++)
{
avg = avg + temp[i];
}
avg = avg / 24;
cout << "The average temp is : " << avg << endl;
cout << "Hour - Temperature - Difference from the Average" << endl;
for (int i = 0; i < 24; i++)
{
cout << " " << i << " " << temp[i] << " " << avg - temp[i] << endl;
}
minTemp = temp[0];
maxTemp = temp[0];
// find minimum and maximum temperartures using linear search
for (int i = 0; i < 24; i++)
{
if (minTemp > temp[i])
{
minTemp = temp[i];
minTempPos = i;
}
if (maxTemp < temp[i])
{
maxTemp = temp[i];
maxTempPos = i;
}
}
cout << " the minimum temperater is : " << minTemp << " at the posiotion : " << minTempPos << endl;
cout << " the maximum temperater is : " << maxTemp << " at the posiotion : " << maxTempPos << endl;
}
}
else
{
cout << "unable to open the file";
}
return 0;
}
Output :
Purpose of program = To learn how to read a text file in C++ using file and standard library function and to learn about linear search alogaritham.
Array help us to query the data making operation on the data values easy.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.