#include #include #include #include #include #include #include #include using namespace std; void load_numbers(const char* filename) { vector numbers; fstream file; double f; double sum = 0.0; double mean = 0.0; file.open(filename, ios_base::in); if (!file.is_open()) { cout << "Error opening file: " << strerror(errno); return; } while (file >> f) { sum += f; numbers.push_back(f); } mean = sum / (double)numbers.size(); cout << endl << "Total loaded " << numbers.size() << " floats. " << endl; cout << "Sum of floats: " << sum << endl; cout << "Average of floats: " << mean << endl; file.close(); } void load_chars(const char* filename) { vector chars; fstream file; char c; int comma_number = 0; file.open(filename, ios_base::in); if (!file.is_open()) { cout << "Error opening file: " << strerror(errno); return; } while (file >> c) { if (c == ',') { comma_number++; } chars.push_back(c); } cout << endl << "Total loaded " << chars.size() << " chars. " << endl; cout << "Total commas in the text file: " << comma_number << endl; file.close(); } typedef vector > Matrix; bool multiply_sqaure(const Matrix &A, const Matrix &B, Matrix &M) { int size_a = A.size(); int size_b = B.size(); Matrix result(size_a); bool error = false; for (int i; i < size_a; i++) { vector row = A.at(i); if (row.size() != size_a) { return false; } result.at(i).resize(size_a); } if (size_a != size_b) return false; for (int row_num = 0; row_num < size_a; row_num++) { vector row = A.at(row_num); for (int col_num = 0; col_num < size_a; col_num++) { double sum = 0.0; for (int i = 0; i < size_a; i++) { sum += row[i] * B[i][col_num]; } result[row_num][col_num] = sum; } } M = result; return true; } int main() { unsigned int concurrency_number = thread::hardware_concurrency(); int from = 0; int to = 100000; double sum = 0.0; thread numbers_thread(&load_numbers, "numbers.txt"); thread chars_thread(&load_chars, "chars.txt"); chars_thread.join(); numbers_thread.join(); Matrix A(2), B(2), C; A[0].resize(2); A[1].resize(2); B[0].resize(2); B[1].resize(2); A[0][0] = 1.0; A[0][1] = 2.0; A[1][0] = 3.0; A[1][1] = 4.0; B[0][0] = 1.0; B[0][1] = 0.0; B[1][0] = 0.0; B[1][1] = 1.0; multiply_sqaure(A, B, C); cout << C[0][0] << '\t' << C[0][1] << endl; cout << C[1][0] << '\t' << C[1][1] << endl; cout << "The main thread finished."; return 0; }