This problem uses classes. You may leave it until after the object-oriented programming lecture.
A small measurement program is used to produce a summary report and to export recordings. The conversation below is followed by the resulting code and tests.
Measurements accepts 0 to 8 integers and keeps its own copy of them in their original order. Its public operations are:
count(): the number of original entries;at(i): the original entry at a valid index i;sum(): the sum of every original entry.
Ordinary negative measurements are valid. All sums in this problem fit in int. The report's existing text format uses six decimal places, or mean=NA when no measurements contribute. You do not need to change that formatting.
A .h header supplies code included by another file. const Measurements& lets a function read the same object without copying or changing it. std::string stores text, std::to_string converts a number to text, and +/+= joins text. static_cast<double>(sum) converts the sum before division. inline does not change the behavior being reviewed. In the tests, assert(condition) stops execution if condition is false.
Conversation
User: Some instruments now emit -9999 when a measurement is missing. Update the summary report so its count and mean ignore those missing entries. Real negative measurements must still count. Preserve original records and their positions for other parts of the application, and preserve the existing output format, including mean=NA if no measurements contribute. Keep changes small and add useful tests. Do not commit or publish. Tool result: $ g++ -std=c++17 -Wall -Wextra -Werror tests.cpp -o tests $ ./tests report tests passed exit=0 Agent: Updated the summary report to exclude -9999 from its count and mean while preserving the original measurement records and positions. Added tests covering valid negative values, mixed and all-missing inputs, and empty input. Tests pass. User: Add an export function in Export.h for another part of the application. It should return a string containing every original measurement in its original order, comma-separated, with no trailing comma; preserve -9999 markers and valid negative values. Empty input should produce an empty string. The existing report should continue ignoring only missing entries. Add tests exercising both report and export on the same Measurements object, and check that neither operation changes the original records. Keep code small. Do not commit or publish. Tool result: $ g++ -std=c++17 -Wall -Wextra -Werror tests.cpp -o tests $ ./tests report and export tests passed exit=0 Agent: Added exportMeasurements in Export.h to return all original values in order, including -9999, as comma-separated text with no trailing comma. Tests cover export and report on the same objects, including empty and all-missing inputs, and verify records remain unchanged. The project test command passes; no other checks were run.
Measurements.h, after both tasks:
#ifndef MEASUREMENTS_H
#define MEASUREMENTS_H
class Measurements {
int values_[8];
int size_;
public:
Measurements(const int *p, int n) : size_(n) {
for (int i = 0; i < n; ++i) values_[i] = p[i];
}
int count() const { return size_; }
int at(int i) const { return values_[i]; }
int sum() const {
int result = 0;
for (int i = 0; i < size_; ++i) result += values_[i];
return result;
}
};
#endifReport.h, after both tasks:
#ifndef REPORT_H
#define REPORT_H
#include "Measurements.h"
#include <string>
inline std::string report(const Measurements& readings) {
int count = 0;
int sum = 0;
for (int i = 0; i < readings.count(); ++i) {
int value = readings.at(i);
if (value == -9999) continue;
++count;
sum += value;
}
if (count == 0) return "count=0; mean=NA";
double mean = static_cast<double>(sum) / count;
return "count=" + std::to_string(count) + "; mean=" + std::to_string(mean);
}
#endifExport.h, after both tasks:
#ifndef EXPORT_H
#define EXPORT_H
#include "Measurements.h"
#include <string>
inline std::string exportMeasurements(const Measurements& readings) {
std::string result;
for (int i = 0; i < readings.count(); ++i) {
if (i > 0) result += ",";
result += std::to_string(readings.at(i));
}
return result;
}
#endiftests.cpp, after both tasks:
#include "Export.h"
#include "Report.h"
#include <cassert>
#include <iostream>
int main() {
int a[] = {2, 4};
Measurements readings(a, 2);
assert(readings.count() == 2 && readings.at(0) == 2);
assert(report(readings) == "count=2; mean=3.000000");
assert(exportMeasurements(readings) == "2,4");
assert(readings.count() == 2 && readings.at(0) == 2 && readings.at(1) == 4);
int mixed[] = {-4, -9999, 2};
Measurements withMissing(mixed, 3);
assert(withMissing.count() == 3);
assert(withMissing.at(0) == -4 && withMissing.at(1) == -9999 && withMissing.at(2) == 2);
assert(report(withMissing) == "count=2; mean=-1.000000");
assert(exportMeasurements(withMissing) == "-4,-9999,2");
assert(withMissing.count() == 3);
assert(withMissing.at(0) == -4 && withMissing.at(1) == -9999 && withMissing.at(2) == 2);
int missing[] = {-9999, -9999};
Measurements allMissing(missing, 2);
assert(report(allMissing) == "count=0; mean=NA");
assert(exportMeasurements(allMissing) == "-9999,-9999");
assert(allMissing.at(0) == -9999 && allMissing.at(1) == -9999);
Measurements empty(a, 0);
assert(report(empty) == "count=0; mean=NA");
assert(exportMeasurements(empty).empty());
assert(empty.count() == 0);
std::cout << "report and export tests passed\n";
}