0% found this document useful (0 votes)
27 views

Exercise Week 12

The document describes two in-class exercises. The first involves reading environmental data from a CSV file, processing the data by counting poor air quality days and calculating average CO2 levels, and writing the results to a file. The second exercise involves implementing a generic Statistics class template to calculate statistical measures like mean, median, and standard deviation for different numeric data types.

Uploaded by

Dave Seynabou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Exercise Week 12

The document describes two in-class exercises. The first involves reading environmental data from a CSV file, processing the data by counting poor air quality days and calculating average CO2 levels, and writing the results to a file. The second exercise involves implementing a generic Statistics class template to calculate statistical measures like mean, median, and standard deviation for different numeric data types.

Uploaded by

Dave Seynabou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

In-class exercise week 12 : File I/O and

Templates
Jan Mikelson
November 22, 2023

This document consists of one in-class exercise that will be solved during
the exercise sessions in class.

In-class Exercise: Urban Environmental Data


Analysis
In this exercise, you will work with data from an urban environmental mon-
itoring system. Your task is to read the data from a CSV file, process it
according to specific criteria, and write the results to new files.

Exercise 1: Reading CSV Data


We will begin by creating a function to read data from a CSV file named
environment data.csv, which contains various environmental parameters
recorded in an urban area.

Tasks
1. Implement a structure EnvironmentalData to store the data from each
row of the CSV file.

2. Create a function std::vector<EnvironmentalData> readCSV(const std


::string& filename) to read data from the CSV file and return a vector
of EnvironmentalData.

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.

Exercise 3: Writing Results to a File


Finally, write the results of your data analysis to a file.

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.

Exercise 4: Main Function


Implement a main function to demonstrate the complete functionality.

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++.

Exercise 1: Designing the Statistics Class Template


Create a Statistics template class that can compute statistical measures
for different types of numeric data.

Tasks
1. Define a template class Statistics with a single template parameter T
for the numeric data type.

2. Include a private member std::vector<T> data to store the numeric


data.

3. Implement a public method void addData(T value) to add data to the


class.

4. Add public methods T mean(), T median(), and T standardDeviation


() to compute the respective statistical measures.

Exercise 2: Testing the Statistics Class


Demonstrate the functionality of the Statistics class using different numeric
data types.

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.

You might also like