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'; } 
  • @VladD for example I can do more and so for(int iter = vec.begin();iter < vec.end(); ++iter) - its_space
  • The essence of the problem is this: you have numbers, say, 5.5 , 4.4 and 1.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. - VladD
  • @VladD So I don’t understand how to write this into the code - its_space
  • Comments are not intended for extended discussion; conversation moved to chat . - Nofate
  • one
    The question has been exhausted. Here is the code for (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

2 answers 2

You have a string

 copy(vec.begin(), vec.end(), ostream_iterator<double>(cout, " ")); 

just breaking out of your code. But if you are sure that you understand perfectly what it is doing, then the solution to your problem is in one line.

 adjacent_difference(istream_iterator<double>(cin),istream_iterator<double>(), ostream_iterator<double>(cout," ")); 

This, of course, if you have enough I / O. If you need vectors explicitly - you will need 4 lines - an announcement, two calls to copy and one adjacent_difference .

In terms of didactic, I agree with VladD - just to give the code is unreasonable, and I hope that this one, the above code should convince you of this.

  • one
    I apologize in advance, and write output via (vec.begin (), vec.end (), ostream_iterator <double> (cout, "")); it is more convenient or faster than just for (auto u: vec) cout << u << ""; or even banal for (auto i = 0; i <vec.size (); i ++) cout << vec [i] << ""; - pavel
  • @pavel, banal for better (it’s clearer to everyone, in general, the KISS principle works). - avp
  • @pavel I already understood what I wrote, I can not continue) - its_space
  • one
    @pavel Going to meet the wishes of the client! :) As for me - all the output options do not list at all, so the choice of one is a matter of taste. For my one-line solution, the choice was small and obvious :) - Harry
  • one
    @avp: And in short in this case :) - VladD

The task was as follows: In addition, create a vector of real numbers containing N – 1 differences between adjacent values, and print this vector.

Decision:

 std::vector<double> difference; double diff = 0; // тут вот не помню нужна инициализация // или нет)) сам учусь ещё //проходим по вектору vec for (int i = 0; i < vec.size() - 1; ++i) { //разницу заносим в новый вектор diff = vec[i] - vec[i + 1]; difference.push_back(diff); } //выводим на печать std::cout << "N -1: "; for (int i = 0; i < difference.size(); ++i) { std::cout << difference[i] << " "; }