C/C++

Циклический алгоритм. Составить программу на с++ Помогите, пожалуйста

1 задание
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
double input(const char* msg) {
cout << msg;
double value;
cin >> value;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
double f(double x) {
if (fabs(x - 1) < 1e-5) return numeric_limits<double>::infinity();
return 2 * sin(x) / pow(1 - x, 2);
}
void show(size_t n, double x, double y, double dx = numeric_limits<double>::max()) {
const string msgs[] = {
"возрастает",
"неизменна",
"убывает",
"разрыв",
"нет данных",
};
string msg;
if (dx == numeric_limits<double>::max()) {
msg = msgs[4];
dx = 0;
}
else if (fabs(y) == numeric_limits<double>::infinity()) msg = msgs[3];
else if (fabs(dx) < 1e-5) msg = msgs[1];
else if (dx < 0) msg = msgs[2];
else msg = msgs[0];
cout << fixed
<< setw(3) << n << ". "
<< setw(11) << setprecision(2) << x
<< setw(15) << setprecision(4) << y
<< setw(17) << msg
<< setw(15) << setprecision(4) << dx
<< '\n';
}
int main() {
system("chcp 1251 > nul");
double a, b;
do {
a = input("a: ");
b = input("b: ");
} while (a > b);
auto h = input("h: ");
auto stop = b + h / 2;
auto prev = f(a);
auto n = 0U;
show(++n, a, prev);
for (double x = a + h; x < stop; x += h) {
auto next = f(x);
auto dx = next - prev;
if (prev == numeric_limits<double>::infinity()) dx = numeric_limits<double>::max();
if (next == numeric_limits<double>::infinity()) dx = next;
show(++n, x, next, dx);
prev = next;
}
system("pause > nul");
}
Вилис Хасбутдинов
Вилис Хасбутдинов
70 066
Лучший ответ
Проверяй !
#include "iostream"
#include "cmath"
#include "cstdio"
using namespace std; int main ()
{ double a,b,h,x,y,Y,Max=-9.999e303,Min=-Max,max,min; int i,m,n=0; char s; for (;;) { n++; cout << "a b h > "; cin >> a >> b >> h; x=a-h; y=f(x); max=Max; min=Min; m=floor((b-a)/h); cout << "Таблица No." << n << ", x, f(x), рост :" << endl; for (i=0; i <= m; i++) { x=a+i*h; Y=2*sin(x)/pow(1-x,2); if (Y > y) s='+'; else if (Y == 0) s='='; else s='-'; y=Y; if (y > max) max=y; if (y < min) min=y;
printf("%10.7f%22.16f %c\n",x,y,s); } cout << "Min=" << min << ", Max=" << max << endl; } return 0; }
А вообще-то здесь нет максимума как такового, поскольку при x=1 у функции непревосходная сингулярность (что ровно то же, что и несобственный максимум, равный бесконечности), так что максимум программа находит только среди узловых значений, а не вообще !..