#include "pch.h"
#include
using namespace std;
double f(double x)
{
double m = -7 * pow(x, 3) - 5.8*x - 4;
return m;
}
int main()
double a = -1; double b = 2;
flag1:
double c = 0.5*(a + b);
if (f(c) == 0)
cout << "The result is " << c << endl;
if (f(c)*f(a) < 0)
b = c;
goto flag1;
if (f(c)*f(b) < 0)
a = c;
if (c >= a && c <= b)
cout << "The solution belongs to the interval" << endl;
else
cout << "The solution doesn't belongs to the interval";
return 0;
Тут с флагами. Если не нравятся флаги - перестройте под себя.
===== PascalABC.NET =====
function Coeff(x, y: array of real): (real, real);
begin
var Sx := x.Sum;
var Sy := y.Sum;
var Sx2 := x.Sum(p -> p * p);
var Sxy := x.Zip(y, (p, q) -> p * q).Sum;
var n := x.Length;
var a0 := (Sy * Sx2 - Sxy * Sx) / (n * Sx2 - Sqr(Sx));
var a1 := (n * Sxy - Sy * Sx) / (n * Sx2 - Sqr(Sx));
Result := (a0, a1)
end;
var n := ReadInteger('Количество точек:');
var x := ReadArrReal('Значения х:', n);
var y := ReadArrReal('Значения y:', n);
var (a0, a1) := Coeff(x, y);
$'a0 = {a0}, a1 = {a1}'.Print
end.
#include "pch.h"
#include
#include
using namespace std;
double f(double x)
{
double m = -7 * pow(x, 3) - 5.8*x - 4;
return m;
}
int main()
{
double a = -1; double b = 2;
flag1:
double c = 0.5*(a + b);
if (f(c) == 0)
cout << "The result is " << c << endl;
if (f(c)*f(a) < 0)
{
b = c;
goto flag1;
}
if (f(c)*f(b) < 0)
{
a = c;
goto flag1;
}
if (c >= a && c <= b)
cout << "The solution belongs to the interval" << endl;
else
cout << "The solution doesn't belongs to the interval";
return 0;
}
Тут с флагами. Если не нравятся флаги - перестройте под себя.
===== PascalABC.NET =====
function Coeff(x, y: array of real): (real, real);
begin
var Sx := x.Sum;
var Sy := y.Sum;
var Sx2 := x.Sum(p -> p * p);
var Sxy := x.Zip(y, (p, q) -> p * q).Sum;
var n := x.Length;
var a0 := (Sy * Sx2 - Sxy * Sx) / (n * Sx2 - Sqr(Sx));
var a1 := (n * Sxy - Sy * Sx) / (n * Sx2 - Sqr(Sx));
Result := (a0, a1)
end;
begin
var n := ReadInteger('Количество точек:');
var x := ReadArrReal('Значения х:', n);
var y := ReadArrReal('Значения y:', n);
var (a0, a1) := Coeff(x, y);
$'a0 = {a0}, a1 = {a1}'.Print
end.