Create a program for calculating the function using the loop operator with a precondition. The variable x changes with a step h on a given interval [a, b].

enter image description here

Help me find a bug

#include<cmath> #include <iostream> using namespace std; int main() { double z, x, xn, xk, h; cin >> xn >> xk >> h; while (x <= xk) if (xn <= x < 0) { z = cos(x) * sin(x) / 2; } if (0 <= x <= 2 * xk / 3) { z = pow(sin(x + 1), 2); } if (2 * xk / 3 < x <= xk) { z = pow(cos(x - 1), 2); printf("%6.2f5s %9.3f", x, z); } x = x + h; return 0; } 

Closed due to the fact that the essence of the question is not clear to the participants Vlad from Moscow , Suvitruf , Vladimir Martyanov , Harry , αλεχολυτ 2 Nov '16 at 20:54 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Mary, is it you? - VladD

2 answers 2

Errors:

  1. The initial value of the variable x not specified
  2. while contains the calculation of only one function. The remaining two and an increase in x occurs after the cycle.

Here is the corrected version.

 #include <cmath> #include <stdio.h> #include <iostream> using namespace std; int main() { double z, x, xn, xk, h; cin >> xn >> xk >> h; x = xn; while (x <= xk) { if (x < 0) z = cos(x) * sin(x) / 2; else if (x <= 2 * xk / 3) z = pow(sin(x + 1), 2); else z = pow(cos(x - 1), 2); printf("%6.2f %9.3f\n", x, z); x += h; } return 0; } 
  • thanks a lot, but still ideone.com/01ubdX - Stell
  • @Stell fixed. Lacked Inclusion - Anton Shchyrov
  • one
    People, what are you ?? !! xn <= x < 0 - what is this? This is nonsense! It is compared as (xn <= x) < 0 , so it is always false . Other comparisons in the same vein ... As they say - go learn a materiel ... - Harry
  • @Harry You are absolutely right. I just copied the author’s code without really looking around. Now fixed - Anton Shchyrov
  • @AntonShchyrov If you remove the incomprehensible 5s from printf and add \n , it will be great ... More - after initialization x = xn; always x >= xn , so this condition can be thrown away. It makes no sense to check all three branches - it is better to put in the second and third - else if and else ... - Harry

As I understand it, there will be 33 more copies of the same question ... Well, they did it. The beauty in the output hover yourself ...

 const double Pi = 3.1415926; const double h = Pi/6.0; int main(int argc, const char * argv[]) { for(double x = -Pi; x < Pi + h/2; x += h) { double z = (x < 0.0) ? cos(x)*sin(x)/2.0 : (x <= 2*Pi/3) ? sin(x+1)*sin(x+1) : cos(x-1)*cos(x-1); cout << " x = " << x << " z = " << z << endl; } }