I'm trying to do a program that takes two vectors and computes the alternating sum, appends the vectors, checks whether two vectors have the same elements in some order, ignoring multiplicities, and removes duplicates. Any help would be appreciated.

#include <iostream>
#include <vector>

using namespace std;

/*
Functions on Vectors.
*/

//computes the alternating sum of all elements in a vector.  Should be -2
int alternating_sum(vector<int> a)
{
  ?
} 

//appends vector b after a.
vector<int> append(vector<int> a, vector<int> b)
{
  ?
}

//checks whether two vectors have the same elements, 
//ignoring multiplicities
bool same_set(vector<int>a, vector<int> b)
{
  ?
} 

//removes duplicates from vector a     ???don't know if this is right???
void remove_duplicates(vector<int>& a)
{
int x;
bool match;
for (unsigned int i=0; i<a.size(); i++)
	{
	x=a[i];
	match=false;
	for (unsigned int j=0; j<b.size()) && (!match); j++)
	{
	if (b[j]==x)
		match=true;
	}
	if(!match)
	return false;
	}
return true;
}


void display_vector( string vector_name,  vector<int> a)
{
	cout << vector_name << " contains the following data: \n";
	for (int i=0; i<a.size(); i++)
		cout<< a[i] << "  ";
	cout<<"\n\n";
}
	  

int main()
{
   vector<int>  a(9);
   vector<int>  b(7);

   a[0] = 1;
   a[1] = 4;
   a[2] = 9;
   a[3] = 16;
   a[4] = 9;
   a[5] = 7;
   a[6] = 4;
   a[7] = 9;
   a[8] = 11;

   b[0] = 11;
   b[1] = 11;
   b[2] = 7;
   b[3] = 9;
   b[4] = 16;
   b[5] = 4;
   b[6] = 1;

   
	// test "alternating_sum" function:
	
	cout << "The alternating sum of vector a is: ";
	
	cout << alternating_sum (a)  << "\n\n";
	
	
	//test "same_set" function
	
	cout << "The elements of the vectors a and b form ";

   if (!same_set(a, b)) cout << "not ";

   cout << "the same set.\n\n";

 
 	//test "append" function:
	
	display_vector ("Appending a after b generates a new vector that", append(b, a) );
	
	
	//test "remove duplicates" function:
	
	remove_duplicates(a);
	display_vector ("The vector a without duplicates", a );
 
 	return 0;
}

Dani AI

Generated

— quick, practical fixes and idiomatic approaches (building on ’s tip to use the STL).

Keep these rules in mind first: pass input vectors by const reference to avoid copies, prefer size_t for sizes/indices, and decide whether you must preserve element order (that changes the method for removing duplicates).

Alternating sum (plus, minus, plus...):

int alternating_sum(const std::vector<int>& v) {
    int sum = 0;
    int sign = 1;
    for (int x : v) { sum += sign * x; sign = -sign; }
    return sum;
}

Appending two vectors (efficient, reserves space once):

std::vector<int> append(const std::vector<int>& a, const std::vector<int>& b) {
    std::vector<int> out;
    out.reserve(a.size() + b.size());
    out.insert(out.end(), a.begin(), a.end());
    out.insert(out.end(), b.begin(), b.end());
    return out;
}

Check “same elements ignoring multiplicities” — treat each input as a set:

#include <unordered_set>

bool same_set(const std::vector<int>& a, const std::vector<int>& b) {
    return std::unordered_set<int>(a.begin(), a.end())
         == std::unordered_set<int>(b.begin(), b.end());
}

Remove duplicates — two options:

  • Preserve original order (stable, O(n) average): track seen values and compact in-place.
  • Order doesn't matter: sort + erase(unique) is shorter and usually faster for large vectors.

Preserve-order example:

void remove_duplicates(std::vector<int>& v) {
    std::unordered_set<int> seen;
    auto out = v.begin();
    for (auto it = v.begin(); it != v.end(); ++it)
        if (seen.insert(*it).second) *out++ = *it;
    v.erase(out, v.end());
}

Note: the remove_duplicates in the original post had undefined symbols and an incorrect return type — make it void and operate on the passed vector. These snippets are concise, avoid unnecessary copying, and follow modern, safe patterns.

Recommended Answers

All 2 Replies

I wonder when I get a job if I can post my work and ask people to fill in the blanks for me. Moral of the story what do you need help with not I need help heres my code fill it in.

the STL has built-in functions which will help you with most of that, read up on vectors, and see how some of the overloaded operators for vector work. also, look at the <algorithm> header.

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.