-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddvec.cpp
More file actions
76 lines (59 loc) · 1.93 KB
/
addvec.cpp
File metadata and controls
76 lines (59 loc) · 1.93 KB
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
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>
std::vector<float> read_raw_file(const std::string& file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + file_name);
}
int num_values;
file >> num_values;
std::vector<float> values(num_values);
for (int i = 0; i < num_values; ++i) {
file >> values[i];
}
file.close();
return values;
}
void write_raw_file(const std::string& file_name, const std::vector<float>& values) {
std::ofstream file(file_name);
if (!file.is_open()) {
throw std::runtime_error("Could not open file: " + file_name);
}
file << values.size() << "\n";
for (const float& value : values) {
file << value << "\n";
}
file.close();
}
std::vector<float> add_vectors(const std::vector<float>& vec1, const std::vector<float>& vec2) {
if (vec1.size() != vec2.size()) {
throw std::invalid_argument("Input vectors must have the same length");
}
std::vector<float> result(vec1.size());
for (size_t i = 0; i < vec1.size(); ++i) {
result[i] = vec1[i] + vec2[i];
}
return result;
}
int main(int argc, char* argv[]) {
try {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <input_file1> <input_file2>" << std::endl;
return 1;
}
std::string file1 = argv[1];
std::string file2 = argv[2];
std::vector<float> vec1 = read_raw_file(file1);
std::vector<float> vec2 = read_raw_file(file2);
std::vector<float> result = add_vectors(vec1, vec2);
std::string output_file = "output.raw";
write_raw_file(output_file, result);
std::cout << "Result written to " << output_file << std::endl;
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}