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?

Recommended Answers

All 16 Replies

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.

wat exactly do u mean by alternating sums using vectors??

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 + ...

then wat he is doing sounds correct

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;
        }

>Below is my source code but I am getting errors stating invalid conversions.
Post the errors.

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;
    }

>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.

Write a function alternatingSum(N) that computes the sum of the numbers 1 through some integer N, where the signs alternate between + and -. For example, alternatingSum(7) should print 4 because 1 - 2 + 3 - 4 + 5 - 6 + 7 = 4. Then write the main to test the function.
The following is a prototype of the function:

void alternatingSum(int);

Sample screen output:

Enter number = 7
The result = 4

can anyone solve this ?????

Write a function that computes the alternating sum of all elements in an array. For example, if alternating_sum is called with an array containing
1 4 9 16 9 7 4 9 11
then it computes
1 – 4 + 9 – 16 + 9 – 7 + 4 – 9 + 11 = –2

It seems to get stuck when I try to quit the inputs. If understands then please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.