do...while:
#include
#include
using namespace std;
int main() {
double a = 2.17, xn = -1.5, xk = 1.5, dx = 0.2;
double x = xn, y, sum = 0, product = 1;
int count = 0, iteration = 0;
do {
y = sqrt(fabs(a + pow(x, 2))) * log(pow(a, 2) + 3.4) / 2;
if (y > 0) {
count++;
sum += y;
product *= y;
}
if (iteration % 2 == 0) {
cout << "x = " << x << ", y = " << y << endl;
}
x += dx;
iteration++;
} while (x <= xk);
cout << "Sum: " << sum << endl;
cout << "Product: " << product << endl;
cout << "Count of positive y: " << count << endl;
return 0;
}
while:
#include
#include
using namespace std;
int main() {
double a = 2.17, xn = -1.5, xk = 1.5, dx = 0.2;
double x = xn, y, sum = 0, product = 1;
int count = 0, iteration = 0;
while (x <= xk) {
y = sqrt(fabs(a + pow(x, 2))) * log(pow(a, 2) + 3.4) / 2;
if (y > 0) {
count++;
sum += y;
product *= y;
}
if (iteration % 2 == 0) {
cout << "x = " << x << ", y = " << y << endl;
}
x += dx;
iteration++;
}
cout << "Sum: " << sum << endl;
cout << "Product: " << product << endl;
cout << "Count of positive y: " << count << endl;
return 0;
}
for:
#include
#include
#include
using namespace std;
int main() {
const double a = 2.17, xn = -1.5, xk = 1.5, dx = 0.2;
double y, sum = 0, product = 1;
int count = 0;
cout << fixed << setprecision(4);
for (int i = 0; ; i++) {
double x = xn + i * dx;
if (x > xk) break;
y = sqrt(fabs(a + x*x)) * log(a*a + 3.4) / 2;
if (y > 0) {
count++;
sum += y;
product *= y;
}
if (i % 2 == 0) {
cout << "x = " << setw(6) << x << ", y = " << setw(10) << y << endl;
}
}
cout << "\nSum: " << sum << endl;
cout << "Product: " << product << endl;
cout << "Count of positive y: " << count << endl;
return 0;
}
отчете предоставить три программы с разными операторами цикла (do ...while,
while, for). В алгоритме и программе массивов не использовать.Помогите пожалуйста