#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <cmath>
// Function to read data from file, normalize it, and write to output file
void processDataset(const std::string& inputFile, const std::string& outputFile) {
std::ifstream inFile(inputFile);
if (!inFile) {
throw std::runtime_error("Unable to open input file: " + inputFile);
}
std::vector<double> data;
double value;
while (inFile >> value) {
data.push_back(value);
}
inFile.close();
if (data.empty()) {
throw std::runtime_error("No data read from file");
}
// Normalize data
double min = *std::min_element(data.begin(), data.end());
double max = *std::max_element(data.begin(), data.end());
double range = max - min;
std::ofstream outFile(outputFile);
if (!outFile) {
throw std::runtime_error("Unable to open output file: " + outputFile);
}
for (double& val : data) {
val = (val - min) / range;
outFile << val << "\n";
}
outFile.close();
std::cout << "Data processed and written to " << outputFile << std::endl;
}
// Function to initialize and print a 2D grid
void initializeGrid(int width, int height, double initialValue) {
std::vector<std::vector<double>> grid(height, std::vector<double>(width, initialValue));
std::cout << "Initial grid configuration (" << width << "x" << height << "):\n";
for (const auto& row : grid) {
for (double val : row) {
std::cout << val << " ";
}
std::cout << "\n";
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <mode> [args...]\n";
std::cerr << "Modes:\n";
std::cerr << " process <input_file> <output_file>\n";
std::cerr << " simulate <grid_width> <grid_height> <initial_value>\n";
return 1;
}
std::string mode = argv[1];
try {
if (mode == "process") {
if (argc != 4) {
throw std::runtime_error("Process mode requires input and output file names");
}
processDataset(argv[2], argv[3]);
}
else if (mode == "simulate") {
if (argc != 5) {
throw std::runtime_error("Simulate mode requires grid width, height, and initial value");
}
int width = std::stoi(argv[2]);
int height = std::stoi(argv[3]);
double initialValue = std::stod(argv[4]);
initializeGrid(width, height, initialValue);
}
else {
throw std::runtime_error("Unknown mode: " + mode);
}
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
## Resources
[auto](html_src/README.md)