-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.cpp
More file actions
43 lines (37 loc) · 735 Bytes
/
exercises.cpp
File metadata and controls
43 lines (37 loc) · 735 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* TODO: write down the polynomial evaluation function we did in lecture
* and write a main function to test it out. Below is a prototype:
* */
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
/* evaluate f(x) = a[0] + a[1]x + a[2]x^2 + ... */
/*
int polyEval(const vector<int>& a, int x)
{
int sum = a[a.size() - 1];
for (size_t i = 1; i < a.size(); i++) {
sum = sum * x + a[a.size()-1-i];
}
return sum;
}
*/
/*
* 4
* x(4)+3
* x(4x+3)+2
* x(x(4x+3)+2)+1
*/
int polyEval(const vector<int>& a, int x) {
int sum;
for (size_t i = 0; i < a.size(); i++) {
sum;
}
return sum;
}
int main() {
vector<int> poly = {1,2,3,4};
int value = 2;
cout << polyEval(poly, value) << "\n";
return 0;
}