fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <fstream>
  5. #include <math.h>
  6.  
  7. using namespace std;
  8. const double pi = 3.14;
  9.  
  10. void fillArrayY(const double* x, double* y, int size, double a, double b, double c) {
  11. for (int i = 0; i < size; i++) {
  12. y[i] = a * sin(x[i]) - b * sin(2 * x[i]) + c * sin(3 * x[i]);
  13. }
  14. }
  15. void fillArrayX(double* x, int points, double df, double dy) {
  16. for (int i = 0; i < points; i++) {
  17. x[i] = df + i * ((dy - df) / (points - 1));
  18. }
  19. }
  20. void fillArray(double* a, int n) {
  21. int i = 2, k = 2;
  22. for (int i = 0; i < n; i++) {
  23. a[i] = sin(pi * k * i / n);
  24. }
  25. }
  26. void printArray1(double* a, int n) {
  27. cout << "PR1";
  28. cout << "[" << n << "]";
  29. for (int i = 0; i < n; i++) {
  30. cout << a[i] << ' ';
  31. }
  32. }
  33. void printArray2(double* arr, double count, double a, double b) {
  34. double sum;
  35. sum = b - a;
  36. for (int i = 0; i < count; i++) {
  37. sum = a + i * ((b - a) / (count - 1));
  38. arr[i] = sum;
  39. }
  40. }
  41. void saveArray(const char* fileName, double* a, int n) {
  42. std::ofstream f;
  43. f.open(fileName);
  44. for (int i = 0; i < n; i++) {
  45. f << i << "\t" << a[i] << "\n";
  46. }
  47. f.close();
  48. }
  49. void saveArray1(const char* fileName, double* x, double* y, int n) {
  50. std::ofstream f;
  51. f.open(fileName);
  52. for (int i = 0; i < n; i++) {
  53. f << x[i] << "\t" << y[i] << "\n";
  54. }
  55. f.close();
  56. }
  57. void plotFile(const char* fileName) {
  58. std::ofstream f;
  59. f.open("plot.plt");
  60. f << "plot '" << fileName << "' with linespoints pt 7 ps 2\n";
  61. f << "pause -1\n";
  62. f.close();
  63. system("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\" plot.plt");
  64. }
  65. void saveMaxPoints(const vector <double>& arrMax, double a, double b, double c, const char* fileName) {
  66. std::ofstream f;
  67. f.open(fileName);
  68.  
  69. for (int i = 0; i < arrMax.size(); i++) {
  70. f << arrMax[i] << " " << a * sin(arrMax[i]) - b * sin(2 * arrMax[i]) + c * sin(3 * arrMax[i]) << "\n";
  71. }
  72. f.close();
  73. }
  74.  
  75. void saveMinPoints(const vector <double>& arrMin, double a, double b, double c, const char* fileName) {
  76. std::ofstream f;
  77. f.open(fileName);
  78.  
  79. for (int i = 0; i < arrMin.size(); i++) {
  80. f << arrMin[i] << " " << a * sin(arrMin[i]) - b * sin(2 * arrMin[i]) + c * sin(3 * arrMin[i]) << "\n";
  81. }
  82. f.close();
  83. }
  84. vector <double> findelocalMin(const double* x,const double* y, int n) {
  85. vector <double> vec;
  86. for (int i = 1; i < n - 1; i++) {
  87. if (abs(y[i - 1]) < abs(y[i]) && abs(y[i]) > abs(y[i + 1])) {
  88. vec.push_back(x[i]);
  89. }
  90. }
  91. cout << "\n";
  92. return vec;
  93. }
  94. vector <double> findelocalMax(const double* x, const double* y, int n) {
  95. vector <double> vec;
  96. for (int i = 1; i < n - 1; i++) {
  97. if (x[i - 1] > x[i] && y[i] < y[i + 1]) {
  98. vec.push_back(x[i]);
  99. }
  100. }
  101. cout << "\n";
  102. return vec;
  103. }
  104. void saveArray2(const char* fileName1, double* x, double* y, int n) {
  105. std::ofstream f;
  106. f.open(fileName1);
  107. for (int i = 0; i < n; i++) {
  108. f << x[i] << "\t" << y[i] << "\n";
  109. }
  110. f.close();
  111. }
  112. void plotFile1(const char* fileName1, double* LS) {
  113. std::ofstream f;
  114. int num = 0;
  115. for (int i = 0; i < 20; i++) {
  116. if (abs(LS[i]) > num) {
  117. num = i;
  118. }
  119. }
  120. f.open("plot1.plt");
  121. f << "plot '" << fileName1 << "' with linespoints pt 7 ps 2\n";
  122. f << num << " linecolor \"orange""\n";
  123. f << "pause -1\n";
  124. f.close();
  125. system("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\" plot1.plt");
  126. }
  127.  
  128. int main() {
  129. int const points = 20;
  130. double x[points];
  131. double y[points];
  132. double LS[points];
  133. double a = 5, b = 2, c = 1;
  134. vector <double> Maxarg;
  135. vector <double> Minarg;
  136. fillArrayX(x, points, 0, 4 * pi);
  137. fillArrayY(x, y, points, a, b, c);
  138. fillArray(x, 20);
  139. printArray1(x, 20);
  140. printArray2(x, 6, 1, 2);
  141. Maxarg = findelocalMax(x, y, points);
  142. Minarg = findelocalMin(x, y, points);
  143. saveArray("D:\\PMI 201B\\Lab1\\data.txt", x, 20);
  144. saveMaxPoints(Maxarg, a, b, c, "maxpoints.txt");
  145. saveMinPoints(Minarg, a, b, c, "minpoints.txt");
  146. saveArray1("D:\\PMI 201B\\Lab1\\data.txt", x, y, points);
  147. saveArray2("D:\\PMI 201B\\Lab1\\data.txt", x, y, points);
  148. plotFile("D:\\PMI 201B\\Lab1\\data.txt");
  149. return 0;
  150. }
Success #stdin #stdout #stderr 0.01s 5296KB
stdin
Standard input is empty
stdout
PR1[20]0 0.308866 0.587528 0.808736 0.950859 1 0.951351 0.809672 0.588816 0.31038 0.00159265 -0.30735 -0.586238 -0.807798 -0.950365 -0.999997 -0.951841 -0.810605 -0.590102 -0.311894 

stderr
sh: 1: C:\Program Files\gnuplot\bin\gnuplot.exe: not found