·
Implement
the specifications that are described in the exercise
·
Use
the exact names that are given for functions, classes, parameters, etc. (when
this is specified)
·
Write
the necessary code to demonstrate that your program is working as intended
·
You
have to complete a minimum of 70% of the exercise
a) Implement a function for
swapping two values using pointers as parameters:
void swap(int *x, int
*y);
The
function should swap the values pointed to by the arguments.
b) Use the function to swap the two integer values a
and b in the following code:
int a = 1;
int b = 2;
swap(......);
//fill in the arguments
cout
<< "a = " << a
<< endl; // a should now be 2
cout << "b
= " << b << endl; // b should
now be 1
c) Use the function to swap the two integer values pointed to by c and d
in the following code:
int *c = new int(3);
int *d = new int(4);
swap(.....);//fill in the arguments
cout
<< "*c = " << *c
<< endl; // c should now be 4
cout
<< "*d = " << *d
<< endl; // d should now be 3
a) Copy and paste the code below into your own project. Implement the
functions createNewTable() and printTable() that are used in the main-method.
The
program is a simple example on the use of dynamic arrays. The program starts
with an empty array (of size 0) and the user is asked to input 5 values at a
time (until the program is terminated by ‘n’). For every
iteration a new array is created that includes all the previous integers
in table in addition to the new input. The printTable() function is only used to
print out the text “Table now contains….” and the values in the array.
int main(){
int incr_size = 5;
int size = 0;
int *table = new int[size];
int *input = new int[incr_size];
char c = 'y';
while (toupper(c) == 'Y'){
cout
<< "Write a sequence of "
<< incr_size << " integer values:
";
for (int i = 0; i
< incr_size; i++){
cin
>> input[i];
}
table
= createNewTable(table, input, size, incr_size);
size
+= incr_size;
printTable(table,
size);
cout
<< "Continue (y/n): ";
cin
>> c;
}
return 0;
}
The
output should be something like this:
Write a sequence of 5 integer
values: 1 2 3 4 5
Table now contains 5 elements: 1 2 3
4 5
Continue (y/n): y
Write a sequence of 5 integer
values: 1 2 3 4 6
Table now contains 10 elements: 1 2
3 4 5 1 2 3
4 6
Continue (y/n): y
Write a sequence of 5 integer
values: 0 0 33 55 99
Table now contains 15 elements: 1 2
3 4 5 1 2 3 4
6 0
0 33 55 99
Continue (y/n): n
b) The code above does not deallocate the
memory that is not used anymore (the line that assigns new array to the table
variable is the main problem).
Implement the appropriate memory management that is needed in the main method
(you have to add a few lines including a delete [] statement).
a) Create a class IntegerSet for which objects can hold a set of integers. A set can be
represented internally as a vector and is a collection of unique values – any
integer value can only appear once in the vector. Implement the following
public member functions for your class (and any additional functions that you
may need):
void
insertElement(int i);
IntegerSet*
unionOfSets(IntegerSet&);
IntegerSet*
intersectionOfSets(IntegerSet&);
Hints:
A union is the set-theoretical union of two sets (includes all values found in
either of two sets). An intersection is the set-theoretical intersection of two
sets (includes all values found in both sets).
b) Extend the class with the a member function void printSet() that
prints the set as a list of numbers separated by spaces. Print "---" for
an empty set.
Write a main method that tests your class by instantiating some sets, adding
integers and creating new sets for unions and intersections.
Implement
the following two classes (copy and paste the code, implement the member
functions and overloaded operators).
A course has a name and a vector holding pointers to the students taking that
course.
A student has a name and a vector of the courses the student is taking.
The
main challenge in this exercise is that the implementation should include a way
to simplify the process of updating the vectors. Whenever a student is added to
a course, the addStudent() function should make an implicit call to the students addCourse() function to update the list of courses a
student is taking. You should be able to
run the main method that is listed and get the output that is indicated below.
class Student;
class Course{
private:
string name;
vector<Student*> students;
public:
Course(string name): name(name){};
string getName();
void
addStudent(Student *s);
friend
ostream& operator <<(ostream&,
Course*);
};
class Student{
private:
string name;
vector<Course*> courses;
public:
Student(string name): name(name){};
string
getName();
void
addCourse(Course *c);
friend
ostream& operator <<(ostream&,
Student*);
};
int main(){
Student *s1 = new Student("Per
Person");
Student *s2 = new Student("Tor
Jenssen");
Student *s3 = new Student("Lene
Marlin");
Course *c1 = new Course("TDT4102");
Course *c2 = new Course("TDT4100");
c1->addStudent(s1);
c1->addStudent(s2);
c1->addStudent(s3);
c2->addStudent(s1);
c2->addStudent(s2);
cout <<
s1;
cout <<
s2;
cout <<
s3;
cout <<
c1;
cout <<
c2;
}
The output should be something like
this:
Per Person: TDT4102, TDT4100
Tor Jenssen: TDT4102, TDT4100
Lene Marlin: TDT4102
TDT4102: Per Person, Tor Jenssen, Lene Marlin
TDT4100: Per Person, Tor Jenssen
Press any key to continue . . .