I have code, it performs the following tasks:
Passes int values into the input stream, writes them to a vector. Then we can find out the sum of certain elements of the vector (starting from the 1st to n if n is not larger than the size of the vector).
Now the task is to finalize the code:
1. Change the data type from int to double. 2. Create a vector of real numbers containing N-1 differences between adjacent values and print them.
vector <double> vec; void error(string s) { throw out_of_range(s); } int main() try { string ss; double temp = 0; while(cin >> ss && ss != "exit") { temp = atof(ss.c_str()); vec.push_back(temp); } cout << "difference: "; for (int i = 0; i < vec.size() -1; ++i) { cout << vec[i] - vec[i+1] << ends; } cout << endl; int x(0); int sum(0); cout << "enter: "; cin >> x; if(x > vec.size()) error("out_of_range"); cout << "sum of nambers: "; for(int i = 0; i < x; ++i) { cout << vec[i] << ends; sum += vec[i]; } cout << " == " << sum << endl; return 0; } catch(exception& s) { cout << s.what() << '\n'; }
for(int iter = vec.begin();iter < vec.end(); ++iter)
- its_space5.5
,4.4
and1.1
. You need to get a set of consecutive differences:5.5 - 4.4
,4.4 - 1.1
. That is,1.1
,3.3
in our case. - VladDfor (int i = 0; i < vec.size() -1; ++i) { cout << vec[i] - vec[i+1] << ends; }
for (int i = 0; i < vec.size() -1; ++i) { cout << vec[i] - vec[i+1] << ends; }
- its_space