Performing operations on a processed int vector This question came from C++ Primer 5th edition:


  Q: Read a set of integers into a vector. Print the sum of each pair of
  adjacent elements. Change your program so that it prints the sum of
  the first and last elements, followed by the sum of the second and
  second-tolast, and so on.


I was just wondering if there is any way to improve my solution.

Part 1:

int main(){

    vector&lt;int&gt; intVector;
    int number;
    while (cin &gt;&gt; number){
        intVector.push_back(number);
    }
    for (int i = 0; i &lt; intVector.size(); i++){
        int k;
        k = intVector[i] + intVector[i + 1];
        cout &lt;&lt; k &lt;&lt; endl;
    }
    return 0;
}


Part 2:  

int main(){

    vector&lt;int&gt; intVector;
    int number;
    while (cin &gt;&gt; number) {
        intVector.push_back(number);
    }
    int sizeOfIntVector = intVector.size();
    for(int i = 0; i &lt; sizeOfIntVector / 2; i++){
        int k;
        k = intVector[i] + intVector[sizeOfIntVector - 1 - i];
        cout &lt;&lt; k &lt;&lt; endl;
    }

    return 0;
}
Never use using namespace std;. 
Some reasons why here
Part 1 is fine other than the error Bogdan Yarema pointed out. I would personally just start at 1 instead thought.

for (int i = 1; i &lt; intVector.size(); i++){

    int k = intVector[i] + intVector[i - 1];
    cout &lt;&lt; k &lt;&lt; endl;
}

In case the size of the vector is odd and the element in center should be added to himself (as Bogdan Yarema pointed out as swell), it would be better to get the ceiling of intVector.size() / 2

int half = (intVector.size() + 1 )/ 2;
int last = intVector.size() - 1;
for (int i = 0; i &lt; half; i++) {

    int k = intVector[i] + intVector[last - i];
    std::cout &lt;&lt; k &lt;&lt; std::endl;
}