Skip to main content
deleted 52 characters in body; edited title; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

c++ primer 5th edition exercise 3.20 Using inputted data from vector to create two outputs

So I'm trying to figure out a way to do the exercise 3.20 in the c++C++ primer book

I have to create a program that reads input into a vector then then use that data to create 2 outputs.:

  • one is the sum of each adjacent number
  • one is the sum of the first and last number moving inwards ( asas in the 2nd2nd and 2nd2nd last, 3rd3rd and 3rd3rd last etc)

I've created the following code, but it feels off somehow. Can Can I simplify/clean up this stuff a bit? (not going to far)

Thanks for your input!

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using std::cin; using std::cout; using std::endl; using std::vector; using std::string;

int main()
{
    vector<int> nv;
    int n;

    while (cin >> n)
    {
        nv.push_back(n);
    }

    int cnt = 0;
    int output = 0;
    int oldnum = 0;
    
    //output sum of adjacent
    for (int cn : nv)
    {
        output += cn; 
        output += oldnum;
        ++cnt;

        if (cnt == 2)
        {
            cout << output << " ";
            cnt = 1;
            output = 0;
            oldnum = cn;
        }

    }
    cout << endl;

    //output sum of first and last
    int trigger = 1;
    for (decltype(nv.size()) i = 0; i < (nv.size()/2); ++i)
    {
        cout << (nv[trigger - 1]) + (nv[nv.size() - (1 * trigger)]) << " ";
        ++trigger;
    }
    return 0;
}

c++ primer 5th edition exercise 3.20

So I'm trying to figure out a way to do the exercise 3.20 in the c++ primer book

I have to create a program that reads input into a vector then use that data to create 2 outputs.

  • one is the sum of each adjacent number
  • one is the sum of the first and last number moving inwards ( as in the 2nd and 2nd last, 3rd and 3rd last etc)

I've created the following code, but it feels off somehow. Can I simplify/clean up this stuff a bit? (not going to far)

Thanks for your input!

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using std::cin; using std::cout; using std::endl; using std::vector; using std::string;

int main()
{
    vector<int> nv;
    int n;

    while (cin >> n)
    {
        nv.push_back(n);
    }

    int cnt = 0;
    int output = 0;
    int oldnum = 0;
    
    //output sum of adjacent
    for (int cn : nv)
    {
        output += cn; 
        output += oldnum;
        ++cnt;

        if (cnt == 2)
        {
            cout << output << " ";
            cnt = 1;
            output = 0;
            oldnum = cn;
        }

    }
    cout << endl;

    //output sum of first and last
    int trigger = 1;
    for (decltype(nv.size()) i = 0; i < (nv.size()/2); ++i)
    {
        cout << (nv[trigger - 1]) + (nv[nv.size() - (1 * trigger)]) << " ";
        ++trigger;
    }
    return 0;
}

Using inputted data from vector to create two outputs

I'm trying to figure out a way to do the exercise 3.20 in the C++ primer book

I have to create a program that reads input into a vector then use that data to create 2 outputs:

  • one is the sum of each adjacent number
  • one is the sum of the first and last number moving inwards (as in the 2nd and 2nd last, 3rd and 3rd last etc)

I've created the following code, but it feels off somehow. Can I simplify/clean up this stuff a bit?

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using std::cin; using std::cout; using std::endl; using std::vector; using std::string;

int main()
{
    vector<int> nv;
    int n;

    while (cin >> n)
    {
        nv.push_back(n);
    }

    int cnt = 0;
    int output = 0;
    int oldnum = 0;
    
    //output sum of adjacent
    for (int cn : nv)
    {
        output += cn; 
        output += oldnum;
        ++cnt;

        if (cnt == 2)
        {
            cout << output << " ";
            cnt = 1;
            output = 0;
            oldnum = cn;
        }

    }
    cout << endl;

    //output sum of first and last
    int trigger = 1;
    for (decltype(nv.size()) i = 0; i < (nv.size()/2); ++i)
    {
        cout << (nv[trigger - 1]) + (nv[nv.size() - (1 * trigger)]) << " ";
        ++trigger;
    }
    return 0;
}
Source Link
Unhek
  • 83
  • 3

c++ primer 5th edition exercise 3.20

So I'm trying to figure out a way to do the exercise 3.20 in the c++ primer book

I have to create a program that reads input into a vector then use that data to create 2 outputs.

  • one is the sum of each adjacent number
  • one is the sum of the first and last number moving inwards ( as in the 2nd and 2nd last, 3rd and 3rd last etc)

I've created the following code, but it feels off somehow. Can I simplify/clean up this stuff a bit? (not going to far)

Thanks for your input!

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using std::cin; using std::cout; using std::endl; using std::vector; using std::string;

int main()
{
    vector<int> nv;
    int n;

    while (cin >> n)
    {
        nv.push_back(n);
    }

    int cnt = 0;
    int output = 0;
    int oldnum = 0;
    
    //output sum of adjacent
    for (int cn : nv)
    {
        output += cn; 
        output += oldnum;
        ++cnt;

        if (cnt == 2)
        {
            cout << output << " ";
            cnt = 1;
            output = 0;
            oldnum = cn;
        }

    }
    cout << endl;

    //output sum of first and last
    int trigger = 1;
    for (decltype(nv.size()) i = 0; i < (nv.size()/2); ++i)
    {
        cout << (nv[trigger - 1]) + (nv[nv.size() - (1 * trigger)]) << " ";
        ++trigger;
    }
    return 0;
}