Introduction

This lab will introduce you to reading data into and processing data contained within two-dimensional arrays. In this lab, you will take in data representing the highest/lowest temperatures for each month of the year. You will then find the minimum/maximum temperature for the year and the average low/high temperature for the year.

Lab: Temperature Statistics

In this lab you will write a program that uses two-dimensional arrays to store data from a text file in main memory. The data from the text file you will be storing holds the high and low temperatures for each month of the year. You will:

  • Read in the data from a TXT file to a two-dimensional array.
  • Find the lowest temperature for the year.
  • Find the highest temperature for the year.
  • Compute the average low temperature for the year.
  • Compute the average high temperature for the year.

To do these 5 tasks you will need to start by reading the data from a TXT file into a two-dimensional array. In order to do this you will prompt, to the command line, your user to enter a TXT file name. You will then attempt to open the TXT file name the user enters, if you are unable to open the TXT file name the user enters, output an error and loop until the user enters a valid file name. The error you will output is:

Error: Invalid input file name.

Once you have your data stored into a two-dimensional array you can process the information in it to get the statistics you need for this lab. To retrieve these statistics we will write 3 functions described below.

Lowest Temperature for the Year

In our dataset the first column of data will contain the low temperatures for each month of the year. Create a function:

float lowestTemp (float[][2])

Which takes in a two-dimensional array that is n x 2 in size, where n is the amount of rows in the two-dimensional array, and returns the lowest floating point number in the first column (Note: the 2 in the formal parameters denotes 2 columns).

Highest Temperature for the Year

In our dataset the second column of data will contain the high temperatures for each month of the year. Create a function:

float highestTemp (float[][2])

Which takes in a two-dimensional array that is n x 2 in size, where n is the amount of rows in the two-dimensional array, and returns the lowest floating point number in the first column (Note: the 2 in the formal parameters denotes 2 columns).

Average Low Temperature

Create a function:

float averageLowTemp (float[][2])

Which iterates through the first column of data in the passed two-dimensional array and finds the average of the column.

Average High Temperature

Create a function:

float averageHighTemp (float[][2])

Which iterates through the second column of data in the passed two-dimensional array and finds the average of the column.

Once you have completed the above functions you can use them to match the example output. The order to call each function above to get values to match the example output is:

  • Call lowestTemp to get a value to output for the lowest temperature of the year.
  • Call highestTemp to get a value to output for the highest temperature of the year.
  • Call averageLowTemp to get a value to output for the average low temperature of the year.
  • Call averageHighTemp to get a value to output for the average high temperature of the year.

Example Output

An example of an interaction with your program is shown below, your output should match these examples exactly. (The words printed in blue are from the computer, based on your commands, the words in red are user input. Note: these colors are simply here to distinguish components and not needed in your program.):

Alexs-iMac:lab12 alex$ ​./a.out
TEMPERATURE STATISTICS
What is the data file name?
**error
Error: Invalid input file name.
What is the data file name?
**error.txt
Error: Invalid input file name.
What is the data file name?
**Lab12Data.txt
Henderson 2019
Lowest temperature of the year was 35.2 F.
Highest temperature of the year was 115.5 F.
Average low temperature of the year was 56.55 F.
Average high temperature of the year was 84.66 F.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.