Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <cstdlib>
- void initializeMatrix(int s, int n, double *A, const char *filename) {
- if (s == 0) {
- std::ifstream file(filename);
- if (!file) {
- std::cerr << "Error: Cannot open file " << filename << std::endl;
- std::exit(1);
- }
- for (int i = 0; i < n * n; i++) {
- if (!(file >> A[i])) {
- std::cerr << "Error: Invalid data in file" << std::endl;
- std::exit(1);
- }
- }
- file.close();
- } else {
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- int idx = (i - 1) * n + (j - 1);
- switch (s) {
- case 1: A[idx] = n - std::max(i, j) + 1; break;
- case 2: A[idx] = std::max(i, j); break;
- case 3: A[idx] = std::abs(i - j); break;
- case 4: A[idx] = 1.0 / (i + j - 1); break;
- default: std::cerr << "Error: Invalid formula number" << std::endl; std::exit(1);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment