Exercise Week 12
Exercise Week 12
Templates
Jan Mikelson
November 22, 2023
This document consists of one in-class exercise that will be solved during
the exercise sessions in class.
Tasks
1. Implement a structure EnvironmentalData to store the data from each
row of the CSV file.
1
Exercise 2: Data Processing
Now, use the data read from the CSV file to perform specific analysis tasks.
Tasks
1. Implement a function int countPoorAirQuality(const std::vector<
EnvironmentalData>& data) to count the number of entries with ”Poor”
air quality.
2. Create a function double averageCO2CentralPark(const std::vector
<EnvironmentalData>& data) to compute the average CO2 level for the
Central Park location.
3. Write a function std::vector<std::string> highWindDays(const std
::vector<EnvironmentalData>& data) that lists all dates where the
wind speed was greater than 15 km/h.
Tasks
1. Implement a function void writeResultsToFile(const std::string&
filename, const std::vector<EnvironmentalData>& data) to write the
analysis results to a file named environment analysis results.txt.
2. The file should include the count of poor air quality days, the average
CO2 level in Central Park, and a list of high wind speed days.
Tasks
1. Read data from environment data.csv.
2. Perform the analysis using the implemented functions.
3. Write the results to environment analysis results.txt.
2
In-class Exercise: Implementing a Generic Statis-
tics Class
In this exercise, you will implement a generic class for calculating statistical
measures on numeric data sets. The class will demonstrate the power and
flexibility of templates in C++.
Tasks
1. Define a template class Statistics with a single template parameter T
for the numeric data type.
Tasks
1. In the main function, create an instance of Statistics<double> and
add several double values to it.
2. Calculate and display the mean, median, and standard deviation of the
added values.
3
3. Repeat the process for other numeric types like int or float to demon-
strate the class’s versatility.