amazon

Tuesday 16 December 2014

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.

Exercise 3.20: 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.





#include <iostream>
//#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<int> i;
    int j=-1,c;

    while(cin >> c)
    {
        j++;
        i.push_back(c);
    }

    cout  << "here the pair goes live" << endl;
    for(auto k=0; k<j; k++)
    {
        cout << i[k] + i[k+1] << endl;
    }
cout  << "here the  goes live" << endl;

    for(auto k=0; k<j/2+1; k++)
    {
        cout << i[k] + i[j-k] << endl;
    }
}


0 comments: