Create a program that calculates statistics from a log of sensor data and prints a report to a file.

Order Description

Learning Outcomes and Introduction

In this lab assignment you will learn about:

  • Opening and closing files with fopen and fclose
  • Reading lines with fgets
  • Interpretting file data with fscanf and sscanf
  • Printing to files and stderr using fprintf


Task 2: Formatted 

Create a program that calculates statistics from a log of sensor data and prints a report to a file.

Your program should accept two command-line arguments: the name of the log file to read, followed by the name of the report file to write. If only one command-line argument is given, write the report to standard output instead. If no arguments are given, read the sensor data from standard input instead.

Note: there is no limit to the number of lines to process from a file, so you should not try to read and store all of the data at once in your program. Instead, accumulate your statistics on a line-by-line basis.

The log data your program processes will be formatted as a table of values. Each line of input will begin with a timestamp, followed by a list of floating-point sensor reading values (separated by white-space). The number of sensor readings on a line can vary from file to file, but every line in a file should have the same number of readings. Every line of input is guaranteed to be less than 500 characters long.

If the log data file cannot be opened, or an error occurs during reading, print an error message to the standard error stream and terminate the program. Similarly print an error message and exit if the log file contains any improperly formatted data or has an inconsistent number of sensor readings on each line.

The report generated by your program should display the timestamp that the minimum and maximum sensor values were recorded (across all readings). In addition, it should report the statistical mean (average) and standard deviation of the values in each column of sensor data.

Example input and output

Given an input file like:

2020-11-17T16:55:00      4.0   3.1e2      1.2

2020-11-17T16:56:00      -3    -5.12339   0

2020-11-17T16:57:00      2e-2   0.44      -0.2E-1

2020-11-17T16:58:00      50    88          30

2020-11-17T16:59:00      1.2   2.4        5.9

Your program should output something like:

Maximum recorded at 2020-11-17T16:55:00 (310)

Minimum recorded at 2020-11-17T16:56:00 (-5.12339)


Sensor 1:

- mean: 10.44

- deviation: 22.25


Sensor 2:

- mean: 79.14

- deviation: 134.68


Sensor 3:

- mean: 7.42

- deviation: 12.86



Math details

The statistical mean x̄ for a set of values is given by the formula:


Where xi is a single data value (such that Σxi is the sum of all values), and N is the total number of values.

The standard deviation σ for a set of values is usually defined as:

σ2=Σ(xi - x̄)2 /(N – 1)

Don't forget to use square-root to get σ!

However, this formulation would require two passes over the data set in order to first compute the mean and then compute the deviation using the mean. An alternate form can be used to compute the standard deviation in a single pass:

σ2=(N(Σxi2) - (Σxi)2)/N(N - 1)