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

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.