example.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#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)
Back to wk01_basic_concepts