Advertisement
Guest User

Untitled

a guest
May 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. double ** inputmf(ifstream &f,int n, int m, string s)
  10. {
  11. int i,j;
  12. double **a;
  13. a=new double *[n];
  14.  
  15. for(i=0;i< n;i++)
  16. {
  17. a[i]=new double[m];
  18. }
  19.  
  20. f.open(s);
  21.  
  22. if (f.fail()) {cout<<"\n Ошибка открытия файла";
  23. exit(1);
  24. }
  25. for(i=0;i< n;i++)
  26. {for(j=0;j< m;j++)
  27. f >>a[i][j];
  28. }
  29. //Закрытие файла
  30. f.close();
  31. return a;
  32. }
  33.  
  34. double* calculate(double** arr,int n, int m) {
  35. double max;
  36. double* result = new double[n];
  37. for (int i = 0; i < n; ++i) {
  38. max = arr[0][i];
  39. for (int j = 0; j < m; ++j) {
  40. if (arr[j][i] > max) max = arr[j][i];
  41. }
  42. result[i] = max;
  43. }
  44. return result;
  45.  
  46. }
  47.  
  48. void outputmf(ofstream &f,double *a, int n, string s)
  49. {
  50. int i,j;
  51. f.open(s);
  52. if (f.fail()) {cout<<"\n Ошибка открытия файла";
  53. exit(1);
  54. }
  55. for(i=0;i< n;i++)
  56. {
  57. f<< setw(10)<< a[i];
  58. }
  59. f.close();
  60. }
  61.  
  62. int main(){
  63. string name;
  64. name = "/home/etryfly/file";
  65. cout << "Enter file name(path):";
  66. cin >> name;
  67. ifstream file;
  68. int n;
  69. cout << "Enter n:";
  70. cin >> n;
  71. double **arr = inputmf(file, n,n, name);//считали массив из файла
  72.  
  73. ofstream ofile;
  74.  
  75. double *b = calculate(arr, n, n);
  76.  
  77. outputmf(ofile, b, n, name);
  78.  
  79.  
  80. system("pause");
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement