I
I
igoodmood2016-03-05 21:24:31
C++ / C#
igoodmood, 2016-03-05 21:24:31

Where is the error in the C++ code?

When testing the program, one of the values ​​looks like: -1.$ What is the reason for this error?
aed9f851a4894fea901e445ba8e15a38.PNG

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
  setlocale(LC_ALL, "Russian");
  double n,a,k,b,x,y,i;
  cout<<"Введите n=";
  cin>>n;
  cout<<"Введите a=";
  cin>>a;
  cout<<"Введите k=";
  cin>>k;
  cout<<"Введите b=";
  cin>>b;
  for(x=a,i=0;i<n;x=k*x+b,i=i+1){
    y=sqrt(pow(cos(x),2)-sqrt(fabs(x)));
    cout<<left<<setw(6)<<setprecision(2)<<x;
    cout<<left<<setw(6)<<setprecision(2)<<y<<endl;
  }
  system("pause");
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-03-05
@MiiNiPaa

in the entered values. Count with your hands. You take the square root of a negative number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question