Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- int fill_matrix(string filename, int *N, double **A)
- {
- int n, i, j;
- double *a;
- ifstream fin(filename.c_str());
- if (!(fin >> n))
- return 2;
- a = (double *) malloc(n * n * sizeof(double) );
- if (a == NULL)
- return 2;
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- if (!(fin >> a[i * n + j]))
- {
- free(a);
- return 5;
- }
- *N = n;
- *A = a;
- return 0;
- }
- int fill_vector(string filename, int *N, double **A)
- {
- ifstream fin(filename.c_str());
- int n, i;
- double *a;
- if (!(fin >> n))
- return 2;
- a = (double *) malloc(n * sizeof(double));
- if (a == NULL)
- return 2;
- for (i = 0; i < n; i++)
- fin >> a[i];
- *N = n;
- *A = a;
- return 0;
- }
- void fill_matrix_formula(int n, double **A)
- {
- int i, j;
- double *a;
- a = (double *) malloc(n * n * sizeof(double) );
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- a[i * n + j] = 1.0/(i + j + 1.0);
- *A = a;
- }
- void output_matrix(string filename, int n, double *a)
- {
- //ofstream fout(filename.c_str());
- int i, j;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < n; j++)
- cout << a[i * n + j] << " ";
- cout << endl;
- }
- }
- void output_vector(string filename, int n, double *a)
- {
- //ofstream fout(filename.c_str());
- int i;
- cout << endl << "~~";
- for (i = 0; i < n; i++)
- cout << a[i] << " ";
- }
- int main()
- {
- int n = 5;
- double *a;
- double *v;
- fill_matrix("input.txt", &n, &a);
- //fill_matrix_formula(n, &a);
- output_matrix("output.txt", n, a);
- fill_vector("input.txt", &n, &v);
- output_vector("output.txt", 5, v);
- cout << n;
- }
Add Comment
Please, Sign In to add comment