[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

IMplementation

Posted on 2006-04-17
Medium Priority
1,415 Views
Implement the “empty” functions in this program. The functions requirements are as follows:
• alternating_sum function computes the alternating sum of all elements in a vector. For example, if alternating_sum is called with a vector containing {1 4 9 16 9 7 4 9 11} then it computes 1-4+9-16+9-7+4-9+11 = -2.
• append function appends one vector after another. For example if a is {1 4 9 16} and b is {9 7 4 9 11} the append(a,b) returns the vector {1 4 9 16 9 7 4 9 11}
• same_set function checks whether two vectors have the same elements in some order, ignoring multiplicities. For example two vectors a {1 4 9 16 9 7 4 9 11} and b {11 11 7 9 16 4 1} would be considered identical, thus same_set(a,b) returns true. You will probably need one or more “helper” functions.

• remove_duplicates function removes duplicates from a vector. For example, if it is called with a vector {1 4 9 16 9 7 4 9 11}, then the vector is changed to {1 4 9 16 7 11}

The skeleton code also contains a driver for these functions. If you implement the functions correctly, your program should print the following result:

The alternating sum of vector a is: -2

The elements of the vectors a and b form the same set.
Appending a after b generates a new vector that contains the following data:
11 11 7 9 16 4 1 1 4 9 16 9 7 4 9 11

The vector a without duplicates contains the following data:
1 4 9 16 7 11

#include <iostream>
#include <vector>

using namespace std;

/*
Functions on Vectors.
*/

//computes the alternating sum of all elements in a vector
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 a vector
void remove_duplicates(vector<int>& a)
{

}

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;
}
0
Question by:Geezy1984

LVL 23

Expert Comment

ID: 16474247
This appears to be an academic assignment.   The instructions are clear.  The skeletal program provides most of the infrastructure, leaving your task to fill out the bodies of the 4 functions that manipulate the vector sets.  The experts here cannot do your homework for you - neither of us would learn anything.  If you are having some difficulty doing the assignment, you may ask for some assistance.  But you cannot simply post the assignment here and expect someone to do all the work for you.

Also, cross-posting the same question to multiple forums is discouraged.
0

LVL 12

Expert Comment

ID: 16474770
Try to implement the functions yourself.
0

Accepted Solution

krusho earned 200 total points
ID: 16558615
i'll give it to you in pseudo code

//computes the alternating sum of all elements in a vector
int alternating_sum(vector<int> a)
{
//loop through vector
//subtract odd indexes
}

//appends vector b after a.
vector<int> append(vector<int> a, vector<int> b)
{
//add everything in b to the end of a
}

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

//removes duplicates from a vector
void remove_duplicates(vector<int>& a)
{
//loop through each number in a
//loop through rest of list looking for that number
//remove if found
}
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll