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
|
#include <iostream>
#include <fstream>
using namespace std;
const size_t nmax {50};
void indhent_vektor(const string& fn, double v[nmax], size_t& n);
void udskriv_vektor(double v[nmax], size_t n);
void indhent_matrix(const string& fn, double A[nmax][nmax], size_t& n, size_t& m);
void udskriv_matrix(double v[nmax][nmax], size_t n, size_t m);
void multiplik(double A[nmax][nmax], double v[nmax], size_t n);
int main()
{
double v[nmax] {}, A[nmax][nmax] {};
size_t n {}, n1 {}, m1 {};
indhent_vektor("vektor.txt", v, n);
cout << "\nIndhent vektor fra fil";
udskriv_vektor(v, n);
indhent_matrix("matrix.txt", A, n1, m1);
cout << "\nIndhent matrix fra fil";
udskriv_matrix(A, n1, m1);
cout << "\nMultiplied";
multiplik(A, v, n);
return 0;
}
void indhent_vektor(const string& fn, double v[nmax], size_t& n)
{
ifstream fil(fn);
if (fil) {
fil >> n;
for (size_t i = 0; i < nmax && i < n && fil >> v[i]; ++i);
fil.close();
} else
cout << "Cannot open file " << fn << '\n';
}
void udskriv_vektor(double v[nmax], size_t n) {
for (size_t i = 0; i < n; ++i)
cout << "\n[" << v[i] << "]";
cout << '\n';
}
void indhent_matrix(const string& fn, double A[nmax][nmax], size_t& n, size_t& m) {
ifstream fil(fn);
if (fil) {
fil >> n;
fil >> m;
for (size_t i = 0; i < nmax && i < n; ++i)
for (size_t j = 0; j < nmax && j < m; ++j)
fil >> A[i][j];
fil.close();
} else
cout << "Cannot open file " << fn << '\n';
}
void udskriv_matrix(double v[nmax][nmax], size_t n, size_t m) {
for (size_t i = 0; i < n; ++i) {
cout << "\n[" << v[i][0];
for (size_t j = 1; j < m; ++j)
cout << ' ' << v[i][j];
cout << ']';
}
cout << '\n';
}
void multiplik(double A[nmax][nmax], double v[nmax], size_t n) {
double p[nmax] {};
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
p[i] += A[i][j] * v[j];
cout << "\n[" << p[0];
for (size_t k = 1; k < n; ++k)
cout << ' '<< p[k];
cout << "]\n";
}
|