Question:
I have to do a program on alternating sums using vectors. I am not sure how to start this program. I thought about using an array and every even index add the value and every odd index subtract the value. Does that sound right? Any ideas?

The only good way to do this is to figure it out yourself. You've thought about using an "array," but didn't you just mention "vectors"? You might have correctly described your algorithm; now you just need to implement it.

I have what sounds like the same assignment and yes that will work. If this is the same assignment, use a vector because the assignment specifies this type. Then, test for evens and odds. The first index is zero which is even so add evens and subtract odds.

An alternating series is one in which the sign flip-flops. For example,

4 + 4/3 + 4/5 + 4/7 + 4/9 + 4/11 + 4/13

is not an alternating series. But the following is:

4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + 4/13

I think the original poster wants to take a vector containing

[ a b c d e f g h ... ]

and compute

a - b + c - d + e - f + g - h + ...

Below is my source code but I am getting errors stating invalid conversions. Any ideas?

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

double alternating_sum(vector<double>);

int main()
{


        double num_values = 0;

        while (true)
        {
        cout << " Enter a real number (CTRL-D to quit):  ";
        cin >> num_values;
        }
        vector<double> num_sum(num_values);
        double altsum_of_vector = 0;
        altsum_of_vector = alternating_sum(num_sum);

        cout << " The alternating sum of the vector is " << altsum_of_vector << endl;

        return 0;
}


        double alternating_sum(vector<double> v)
        {
                double sum = 0;
                if (v.size() == 0)
                {
                        return 0;
                }

                else
                {
                        for (int i = 0; i < v.size(); i++)
                        {
                                if ( (i % 2) == 0)
                                {
                                        sum += v[i];
                                }
                                else
                                {
                                        sum -= v[i];
                                }
                        }
                }
                return sum;
        }

Edited 3 Years Ago by pyTony: fixed formating

Below is the error message I am receiving.


p9-2.cpp: In function `int main()':
p9-2.cpp:31: warning: passing `double' for argument 1 of `std::vector<_Tp,
_Alloc>::vector(unsigned int) [with _Tp = double, _Alloc =
std::allocator<double>]'
[hs001@cs hs001]$

That's a warning, not an error. It's telling you that the constructor expects an unsigned int, but you're passing a double, and there may be a loss of data in the conversion.

Okay, I see that is a warning I ran the program and had an infinite loop. I am not sure what a constructor is with vectors. I know what it is with classes.
Here is my revised code for this program. However, I am still not getting a value for my alternate sum of vectors.

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

double alternating_sum(vector<double>);

int main()
{


    double num_values = 0;
    int count = 0;

//  vector<double> num_sum; 

    cout << " Enter a real number (CTRL-D to quit): ";
    cin >> num_values;

    if (cin.eof())
    {
        cout << " You didn't enter a number!" << endl;
        return 1;
    }

    while (true)
    {
    cout << " Enter a real number (CTRL-D to quit):  ";
    cin >> num_values;
    if (cin.eof())
    {
        break;
    }
/*  ++count;
        for ( int v = 0; v < count; v++)
        {
            num_sum[v] = num_values;
        }   */
    }  // end of while loop
    vector<double> num_sum(num_values); 
    double altsum_of_vector = 0;
    altsum_of_vector = alternating_sum(num_sum);

    cout << " The alternating sum of the vector is " << altsum_of_vector << endl;

    return 0;
}


    double alternating_sum(vector<double> v)
    {
        double sum = 0;
        if (v.size() == 0)
        {
            return 0;
        }

        else
        {
            for (int i = 0; i < v.size(); i++)
            {
                if ( (i % 2) == 0)
                {
                    sum += v[i];
                }
                else
                {
                    sum -= v[i];
                }   
            }
        }
        return sum;
    }

Edited 3 Years Ago by pyTony: fixed formating

>I am not sure what a constructor is with vectors. I know what it is with classes.
Um, vector is a class.

>I am still not getting a value for my alternate sum of vectors.
That's because you never place any values in the vector. Compare your code with this (I modified it to work and tightened things up a bit):

#include <iostream>
#include <vector>

using namespace std;

double alternating_sum(const vector<double>& v);

int main()
{
  vector<double> num_sum;
  double num_values;

  cout << "Enter a series of real numbers (CTRL-D to quit): ";

  while (cin >> num_values)
    num_sum.push_back(num_values);

  cout << " The alternating sum of the vector is "
    << alternating_sum(num_sum) << endl;

  return 0;
}


double alternating_sum(const vector<double>& v)
{
  double sum = 0;

  for (int i = 0; i < v.size(); i++)
  {
    if ( (i % 2) == 0)
      sum += v[i];
    else
      sum -= v[i];
  }

  return sum;
}

I meant the keyword class. I looked up the pop_back and push_back in the book and now understand their relation with vectors. How do you know when to pass by value or pass by reference, is their a certain rule to follow?


Thanks.

>How do you know when to pass by value or pass by reference
For built-in types like int, pass by value unless you need to modify the original value. For class types, pass by reference when you can and by value when you must. If you're worried about changing an object when passing it by reference, pass it by const reference.

This article has been dead for over six months. Start a new discussion instead.