CSE 330 Laboratory 3, and Homework Assignment 2 Solved

40.00 $

Category:

Description

5/5 - (1 vote)

PART 1

Complete the ADT Vector implementation from our previous labs by adding the following two functions to the Vector class:

 

  1. Member function void erase(int k){…} which removes the element at index k.

 

  1. Member function void insert(int k, T x){…} which inserts the new element x at index k. (The element that is at index k prior to insertion and all other elements at higher-numbered indices are to move one position to the right.)

 

 

Details of the processes behind both functions are depicted below.

 

Erase:      Erase the element at index 3 in vector [2][4][6][8][10][12][14][16]

 

Step-by-step …

0   1   2   3    4     5     6    7

… [2][4][6][8][10][12][14][16]

 

… [2][4][6][10][10][12][14][16]

 

… [2][4][6][10][12][12][14][16]

 

… [2][4][6][10][12][14][14][16]

 

… [2][4][6][10][12][14][16][16]

 

… [2][4][6][10][12][14][16][16]

 

… [2][4][6][10][12][14][16]    DONE!

 

Look at the example and realize the kinds of shifts (really, “copies”) that will have the desired effect. Also notice the duplicate element at the end and how it is removed in order to produce the final result.

 

Your function should also handle the special case where the removal index is the index of the element at the highest index … what will be the simplest way to accomplish this element’s removal? (Hint: this function already exists).

 

 

Insert:      Insert new element 7  at index 3 in vector [2][4][6][8][10][12][14][16]

 

Step-by-step …

0   1   2   3    4     5     6    7

… [2][4][6][8][10][12][14][16]

 

… [2][4][6][8][10][12][14][16][16]

 

… [2][4][6][8][10][12][14][14][16]

 

… [2][4][6][8][10][12][12][14][16]

 

… [2][4][6][8][10][10][12][14][16]

 

… [2][4][6][8][8][10][12][14][16]

 

… [2][4][6][7][8][10][12][14][16]    DONE!

 

 

Look at the example and realize the kinds of shifts (really, “copies”) that will have the desired effect. Also notice how the process  starts out with the addition of a copy of the last element at the right of end of the vector.

 

Your function should also handle the special case where the insertion  index is the index that is one more than the highest index pre-insertion … what will be the simplest way to accomplish insertion into this index? (Hint: this function already exists)

 

Wait for instructions on how to test your erase and insert member functions … 

 

 

PART 2

 

Add to your Vector class two additional functions:

 

void erase(iterator itr){ …}     and    insert(iterator itr, T x) {…}

 

These two member functions are to erase the vector item referenced by iterator itr, and to insert at some iterator itr a new value x. It is expected that Part 2 will largely be accomplished as part of Homework 2. 

 

Again, await instructions on how to test these two iterator-based member functions.

 

For Lab Credit: Simply sign your name of the signup sheet. No portal submissions are required.

 

For Homework Assignment 2: Submit on Wednesday, Jan 29, 2020, via the BlackBoard portal  the following files:  (1) file Vector.h with both variants of your member functions erase and insert “highlighted” with lines

//**************** LAB3/HW2 start ************************************** … your four functions …

 

//**************** LAB3/HW2 end ***************************************

 

  • a hardcopy of file VectorMainHW2 .cpp with test code to be determined,
  • a typescript or screen capture that shows your compiling and running of the program . In case you do not achieve a running program, still submit the source code you have along with a typescript/screenshot that shows your “attempt” to compile and the compiler errors you get.

 

The homework assignment is worth 20 points, 5 points per functions.

 

  • lab3.zip