#include <iostream>
#include <vector>
#include <array>
#include <fstream>
#include <math.h>
using namespace std;
const double pi = 3.14;
void fillArrayY(const double* x, double* y, int size, double a, double b, double c) {
for (int i = 0; i < size; i++) {
y[i] = a * sin(x[i]) - b * sin(2 * x[i]) + c * sin(3 * x[i]);
}
}
void fillArrayX(double* x, int points, double df, double dy) {
for (int i = 0; i < points; i++) {
x[i] = df + i * ((dy - df) / (points - 1));
}
}
void fillArray(double* a, int n) {
int i = 2, k = 2;
for (int i = 0; i < n; i++) {
a[i] = sin(pi * k * i / n);
}
}
void printArray1(double* a, int n) {
cout << "PR1";
cout << "[" << n << "]";
for (int i = 0; i < n; i++) {
cout << a[i] << ' ';
}
}
void printArray2(double* arr, double count, double a, double b) {
double sum;
sum = b - a;
for (int i = 0; i < count; i++) {
sum = a + i * ((b - a) / (count - 1));
arr[i] = sum;
}
}
void saveArray(const char* fileName, double* a, int n) {
std::ofstream f;
f.open(fileName);
for (int i = 0; i < n; i++) {
f << i << "\t" << a[i] << "\n";
}
f.close();
}
void saveArray1(const char* fileName, double* x, double* y, int n) {
std::ofstream f;
f.open(fileName);
for (int i = 0; i < n; i++) {
f << x[i] << "\t" << y[i] << "\n";
}
f.close();
}
void plotFile(const char* fileName) {
std::ofstream f;
f.open("plot.plt");
f << "plot '" << fileName << "' with linespoints pt 7 ps 2\n";
f << "pause -1\n";
f.close();
system("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\" plot.plt");
}
void saveMaxPoints(const vector <double>& arrMax, double a, double b, double c, const char* fileName) {
std::ofstream f;
f.open(fileName);
for (int i = 0; i < arrMax.size(); i++) {
f << arrMax[i] << " " << a * sin(arrMax[i]) - b * sin(2 * arrMax[i]) + c * sin(3 * arrMax[i]) << "\n";
}
f.close();
}
void saveMinPoints(const vector <double>& arrMin, double a, double b, double c, const char* fileName) {
std::ofstream f;
f.open(fileName);
for (int i = 0; i < arrMin.size(); i++) {
f << arrMin[i] << " " << a * sin(arrMin[i]) - b * sin(2 * arrMin[i]) + c * sin(3 * arrMin[i]) << "\n";
}
f.close();
}
vector <double> findelocalMin(const double* x,const double* y, int n) {
vector <double> vec;
for (int i = 1; i < n - 1; i++) {
if (abs(y[i - 1]) < abs(y[i]) && abs(y[i]) > abs(y[i + 1])) {
vec.push_back(x[i]);
}
}
cout << "\n";
return vec;
}
vector <double> findelocalMax(const double* x, const double* y, int n) {
vector <double> vec;
for (int i = 1; i < n - 1; i++) {
if (x[i - 1] > x[i] && y[i] < y[i + 1]) {
vec.push_back(x[i]);
}
}
cout << "\n";
return vec;
}
void saveArray2(const char* fileName1, double* x, double* y, int n) {
std::ofstream f;
f.open(fileName1);
for (int i = 0; i < n; i++) {
f << x[i] << "\t" << y[i] << "\n";
}
f.close();
}
void plotFile1(const char* fileName1, double* LS) {
std::ofstream f;
int num = 0;
for (int i = 0; i < 20; i++) {
if (abs(LS[i]) > num) {
num = i;
}
}
f.open("plot1.plt");
f << "plot '" << fileName1 << "' with linespoints pt 7 ps 2\n";
f << num << " linecolor \"orange""\n";
f << "pause -1\n";
f.close();
system("\"C:\\Program Files\\gnuplot\\bin\\gnuplot.exe\" plot1.plt");
}
int main() {
int const points = 20;
double x[points];
double y[points];
double LS[points];
double a = 5, b = 2, c = 1;
vector <double> Maxarg;
vector <double> Minarg;
fillArrayX(x, points, 0, 4 * pi);
fillArrayY(x, y, points, a, b, c);
fillArray(x, 20);
printArray1(x, 20);
printArray2(x, 6, 1, 2);
Maxarg = findelocalMax(x, y, points);
Minarg = findelocalMin(x, y, points);
saveArray("D:\\PMI 201B\\Lab1\\data.txt", x, 20);
saveMaxPoints(Maxarg, a, b, c, "maxpoints.txt");
saveMinPoints(Minarg, a, b, c, "minpoints.txt");
saveArray1("D:\\PMI 201B\\Lab1\\data.txt", x, y, points);
saveArray2("D:\\PMI 201B\\Lab1\\data.txt", x, y, points);
plotFile("D:\\PMI 201B\\Lab1\\data.txt");
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dm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