[ create a new paste ] login | about

Link: http://codepad.org/1BDgsGwi    [ raw code | output | fork ]

C++, pasted on Nov 8:
#include <iostream>
using namespace std;

/*
x > -3 або x < 3 - y=x
x < -3 - y = (x^2 - 3 * x + 2) / 2
x > 3 - y=x^3
*/

float f(float x);

int main(){
    float y, x;
    float xn = -5.5f;
    float xk =  5.5f;
    float hx = 0.1f;
    size_t zero = 0;
    cout<<"\tx\tf(x)"<<endl;
    for( x = xn; x < xk && x != 3; x += hx )
    {
        cout<<x<<"\t"<<(y = f(x))<<endl;
        if( fabs(y) < 2E-6 )
            zero++;
    }
    cout<<"zero count : "<<zero<<endl;
    cin.get();
    return 0;
}

float f(float x){
    float y = 0;
    if( x < -3 )
        y = (x*x - 3*x + 2) / 2;
    else
    if( x > 3 )
        y = x*x*x;
    else
        y = x;
    return y;
}


Output:
	x	f(x)
-5.5	24.375
-5.4	23.68
-5.3	22.995
-5.2	22.32
-5.1	21.655
-5	21
-4.9	20.355
-4.8	19.72
-4.7	19.095
-4.6	18.48
-4.5	17.875
-4.4	17.28
-4.3	16.695
-4.2	16.12
-4.1	15.555
-4	15
-3.9	14.455
-3.8	13.92
-3.7	13.395
-3.6	12.88
-3.5	12.375
-3.4	11.88
-3.3	11.395
-3.2	10.92
-3.1	10.455
-3	10
-2.9	-2.9
-2.8	-2.8
-2.7	-2.7
-2.6	-2.6
-2.5	-2.5
-2.4	-2.4
-2.3	-2.3
-2.2	-2.2
-2.1	-2.1
-2	-2
-1.9	-1.9
-1.8	-1.8
-1.7	-1.7
-1.6	-1.6
-1.5	-1.5
-1.4	-1.4
-1.3	-1.3
-1.2	-1.2
-1.1	-1.1
-1	-1
-0.900003	-0.900003
-0.800003	-0.800003
-0.700003	-0.700003
-0.600003	-0.600003
-0.500003	-0.500003
-0.400003	-0.400003
-0.300003	-0.300003
-0.200003	-0.200003
-0.100003	-0.100003
-2.99513e-06	-2.99513e-06
0.099997	0.099997
0.199997	0.199997
0.299997	0.299997
0.399997	0.399997
0.499997	0.499997
0.599997	0.599997
0.699997	0.699997
0.799997	0.799997
0.899997	0.899997
0.999997	0.999997
1.1	1.1
1.2	1.2
1.3	1.3
1.4	1.4
1.5	1.5
1.6	1.6
1.7	1.7
1.8	1.8
1.9	1.9
2	2
2.1	2.1
2.2	2.2
2.3	2.3
2.4	2.4
2.5	2.5
2.6	2.6
2.7	2.7
2.8	2.8
2.9	2.9
3	3
3.1	29.7909
3.2	32.7679
3.3	35.9369
3.4	39.3039
3.5	42.8749
3.6	46.6558
3.7	50.6528
3.8	54.8718
3.9	59.3188
4	63.9998
4.1	68.9208
4.2	74.0878
4.3	79.5067
4.4	85.1837
4.5	91.1247
4.6	97.3357
4.7	103.823
4.79999	110.592
4.89999	117.649
4.99999	125
5.09999	132.651
5.19999	140.608
5.29999	148.877
5.39999	157.464
5.49999	166.374
zero count : 0


Create a new paste based on this one


Comments: