c++ finish the code, programming homework help
c++ finish the code, programming homework help
There are two “TODO” to do. There will be more info attached in a file.
#include <iostream>_x000D_
#include <string>_x000D_
_x000D_
using namespace std;_x000D_
_x000D_
//==========================================================================_x000D_
// CONSTANTS_x000D_
//==========================================================================_x000D_
_x000D_
// error codes_x000D_
const int ERROR_CAPACITY_OVERFLOW = 1;_x000D_
_x000D_
// maximum size allowed for a set_x000D_
const int MAX_SET_SIZE = 256;_x000D_
_x000D_
//==========================================================================_x000D_
// TYPES_x000D_
//==========================================================================_x000D_
_x000D_
// structure to represent sets of integers_x000D_
struct IntSet {_x000D_
_x000D_
int elem[MAX_SET_SIZE]; // the elements in the set (occupied 0 to n-1)_x000D_
int size = 0; // the number of elements currently in the set_x000D_
_x000D_
};_x000D_
_x000D_
//==========================================================================_x000D_
// FUNCTIONS_x000D_
//==========================================================================_x000D_
_x000D_
//--------------------------------------------------------------------------_x000D_
// isEmpty(s): returns true if the set is empty and false otherwise._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
bool isEmpty(IntSet s) {_x000D_
_x000D_
// s is empty if its size is zero_x000D_
return s.size == 0;_x000D_
_x000D_
}_x000D_
_x000D_
//-------------------------------------------------------------------------_x000D_
// isMember(s, x): Returns true if x is a memeber of set s and false otherwise._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
bool isMember(IntSet s, int x) {_x000D_
_x000D_
// for every occupied cell of our array..._x000D_
for (int i = 0; i < s.size ; i++) {_x000D_
_x000D_
// compare contents to x_x000D_
if (s.elem[i] == x)_x000D_
return true;_x000D_
_x000D_
}_x000D_
_x000D_
// if we're here, x was not in the set_x000D_
return false;_x000D_
_x000D_
}_x000D_
_x000D_
//--------------------------------------------------------------------------_x000D_
// add(s, x): adds element x into set s. If x already is a member of s, then_x000D_
// this operation has no effect._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
void add(IntSet &s, int x) {_x000D_
_x000D_
// if there is room remaining_x000D_
if (s.size < MAX_SET_SIZE) {_x000D_
_x000D_
// if x is not already in the set_x000D_
if (!isMember(s, x)) {_x000D_
_x000D_
// place x in the elem array of s_x000D_
s.elem[s.size] = x;_x000D_
_x000D_
// update the size of s_x000D_
s.size++;_x000D_
_x000D_
}_x000D_
_x000D_
}_x000D_
else {_x000D_
_x000D_
// the set is filled to capacity_x000D_
cout << "Error: attempted to add an element to a maximum capacity set.";_x000D_
exit(ERROR_CAPACITY_OVERFLOW);_x000D_
_x000D_
}_x000D_
_x000D_
}_x000D_
_x000D_
//--------------------------------------------------------------------------_x000D_
// clear(s): Clear the set s so that it is empty._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
void clear(IntSet &s) {_x000D_
s.size = 0;_x000D_
}_x000D_
_x000D_
//--------------------------------------------------------------------------_x000D_
// print(s): Prints set s to the console._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
void print(IntSet s) {_x000D_
_x000D_
cout << "{";_x000D_
for (int i = 0; i < s.size - 1; i++) {_x000D_
cout << s.elem[i] << ", ";_x000D_
}_x000D_
if (s.size > 0) {_x000D_
cout << s.elem[s.size - 1];_x000D_
}_x000D_
cout << "}" << endl;_x000D_
_x000D_
}_x000D_
_x000D_
//-------------------------------------------------------------------------_x000D_
// setIntersection(s1, s2, result): Compute the intersection of s1 and s2 and_x000D_
// place it into result._x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
void setIntersection(IntSet s1, IntSet s2, IntSet &result) {_x000D_
_x000D_
clear(result);_x000D_
_x000D_
// TODO: Complete the code below..._x000D_
_x000D_
// for every member of s1_x000D_
// if it is a member of s2_x000D_
// add it to the result_x000D_
_x000D_
}_x000D_
_x000D_
//-------------------------------------------------------------------------_x000D_
// setUnion(s1, s2, result): Compute the union of s1 and s2 and place it into_x000D_
// result._x000D_
// ------------------------------------------------------------------------_x000D_
_x000D_
void setUnion(IntSet s1, IntSet s2, IntSet &result) {_x000D_
_x000D_
clear(result);_x000D_
_x000D_
// TODO: Complete the code below..._x000D_
_x000D_
// for every member of s1_x000D_
// add it to the result_x000D_
_x000D_
// for every member of s2_x000D_
// add it to the result_x000D_
_x000D_
}_x000D_
_x000D_
//--------------------------------------------------------------------------_x000D_
// MAIN PROGRAM_x000D_
//--------------------------------------------------------------------------_x000D_
_x000D_
int main() {_x000D_
_x000D_
// fill s1 with {1, 2, 3, 4, 5}_x000D_
IntSet s1;_x000D_
for (int x = 1; x <= 5; x++) {_x000D_
add(s1, x);_x000D_
}_x000D_
cout << "s1: ";_x000D_
print(s1);_x000D_
_x000D_
// fill s2 with {4, 5, 6, 7, 8}_x000D_
IntSet s2;_x000D_
for (int x = 4; x <= 8; x++) {_x000D_
add(s2, x);_x000D_
}_x000D_
cout << "s2: ";_x000D_
print(s2);_x000D_
_x000D_
// compute intersection and print it_x000D_
IntSet res1;_x000D_
setIntersection(s1, s2, res1);_x000D_
cout << "intersection of s1 and s2: ";_x000D_
print(res1);_x000D_
_x000D_
// compute union and print it_x000D_
IntSet res2;_x000D_
setUnion(s1, s2, res2);_x000D_
cout << "union of s1 and s2: ";_x000D_
print(res2);_x000D_
_x000D_
}_x000D_
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


