File tree Expand file tree Collapse file tree 1 file changed +59
-3
lines changed
Expand file tree Collapse file tree 1 file changed +59
-3
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,60 @@ int32_t f(int32_t x)
66 return x * x + 5 * x + 5 ;
77}
88
9+ class FunctionPreCacher
10+ {
11+ public:
12+ void Setup (int32_t x, uint32_t count, int32_t (*function)(int32_t ))
13+ {
14+ if (!values)
15+ {
16+ this ->x = x;
17+ this ->count = count;
18+ this ->function = function;
19+ }
20+ }
21+
22+ void Compute ()
23+ {
24+ if (function && count > 0 )
25+ {
26+ Release ();
27+ values = new int32_t [count];
28+ if (values)
29+ {
30+ for (uint32_t i = 1 ; i <= count; i++)
31+ {
32+ values[i - 1 ] = function (i * x);
33+ }
34+ }
35+ }
36+ }
37+
38+ void Release ()
39+ {
40+ if (values)
41+ {
42+ delete[] values;
43+ values = nullptr ;
44+ }
45+ }
46+
47+ void Print ()
48+ {
49+ if (values)
50+ {
51+ for (uint32_t i = 1 ; i <= count; i++)
52+ std::cout << " f(" << i * x << " ) = " << values[i - 1 ] << std::endl;
53+ }
54+ }
55+
56+ private:
57+ int32_t * values = nullptr ;
58+ int32_t x = 0 ;
59+ uint32_t count = 0 ;
60+ int32_t (*function)(int32_t x) = nullptr ;
61+ };
62+
963int main ()
1064{
1165 int32_t x;
@@ -15,7 +69,9 @@ int main()
1569 std::cout << " Enter the itterations: " ;
1670 std::cin >> count;
1771
18- std::cout << " Starting at: " << x << " Number of itterations: " << count << std::endl;
19- for (uint32_t i = 1 ; i <= count; i++)
20- std::cout << " f(" << i * x << " ) = " << f (i * x) << std::endl;
72+ FunctionPreCacher pc;
73+ pc.Setup (x, count, &f);
74+ pc.Compute ();
75+ pc.Print ();
76+ pc.Release ();
2177}
You can’t perform that action at this time.
0 commit comments