Guest User

Untitled

a guest
Dec 11th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int fill_matrix(string filename, int *N, double **A)
  9. {
  10. int n, i, j;
  11. double *a;
  12.  
  13. ifstream fin(filename.c_str());
  14.  
  15. if (!(fin >> n))
  16. return 2;
  17.  
  18. a = (double *) malloc(n * n * sizeof(double) );
  19.  
  20. if (a == NULL)
  21. return 2;
  22.  
  23. for (i = 0; i < n; i++)
  24. for (j = 0; j < n; j++)
  25. if (!(fin >> a[i * n + j]))
  26. {
  27. free(a);
  28. return 5;
  29. }
  30.  
  31. *N = n;
  32. *A = a;
  33.  
  34. return 0;
  35. }
  36.  
  37. int fill_vector(string filename, int *N, double **A)
  38. {
  39. ifstream fin(filename.c_str());
  40.  
  41. int n, i;
  42. double *a;
  43.  
  44. if (!(fin >> n))
  45. return 2;
  46.  
  47. a = (double *) malloc(n * sizeof(double));
  48.  
  49. if (a == NULL)
  50. return 2;
  51.  
  52. for (i = 0; i < n; i++)
  53. fin >> a[i];
  54.  
  55. *N = n;
  56. *A = a;
  57.  
  58. return 0;
  59. }
  60.  
  61. void fill_matrix_formula(int n, double **A)
  62. {
  63. int i, j;
  64. double *a;
  65.  
  66. a = (double *) malloc(n * n * sizeof(double) );
  67.  
  68. for (i = 0; i < n; i++)
  69. for (j = 0; j < n; j++)
  70. a[i * n + j] = 1.0/(i + j + 1.0);
  71.  
  72. *A = a;
  73. }
  74.  
  75. void output_matrix(string filename, int n, double *a)
  76. {
  77. //ofstream fout(filename.c_str());
  78.  
  79. int i, j;
  80.  
  81. for (i = 0; i < n; i++)
  82. {
  83. for (j = 0; j < n; j++)
  84. cout << a[i * n + j] << " ";
  85.  
  86. cout << endl;
  87. }
  88. }
  89.  
  90. void output_vector(string filename, int n, double *a)
  91. {
  92. //ofstream fout(filename.c_str());
  93.  
  94. int i;
  95.  
  96. cout << endl << "~~";
  97.  
  98. for (i = 0; i < n; i++)
  99. cout << a[i] << " ";
  100.  
  101. }
  102.  
  103. int main()
  104. {
  105. int n = 5;
  106. double *a;
  107. double *v;
  108.  
  109. fill_matrix("input.txt", &n, &a);
  110. //fill_matrix_formula(n, &a);
  111. output_matrix("output.txt", n, a);
  112. fill_vector("input.txt", &n, &v);
  113.  
  114. output_vector("output.txt", 5, v);
  115.  
  116. cout << n;
  117.  
  118. }
Add Comment
Please, Sign In to add comment