Lab 10. One-dimensional and two-dimensional vectors

Lab 10. One-dimensional and two-dimensional vectors.

How to submit your code

Each program (the source code .cpp file) should be submitted through Blackboard (Course Materials > Lab).

You can submit all your programs at the end of the lab session in one submission. This way, we can hopefully avoid the situation when you are quickly writing your program, immediately uploading it to Blackboard, but then, say 10 minutes later, realizing that there is a bug in it.

Basically, submit when you are sure that it will be your final version.

Each program should start with a comment that contains your name and a short program description, for example:

/*
  Author: Your Name 
  Description: Lab 1. Task 1. Hello world
*/

You can submit incomplete programs, but their “incomplete” status must be clearly mentioned in the comment to the program. In this case, also briefly describe what is implemented, and what is not.

Constructors and methods of the class vector<T> you need to know

Constructors:

Methods (member functions):

Programming assignments

Task 1. One-dimensional vector<int>.

Implement the following three functions:

// Print a vector of integers
void print(vector<int> &v);

// Array to Vector conversion function.
// It copies the contents of the array `arr` of length `n` into the output vector 'v'.
// It should work correctly even if the vector `v` is not empty
void array_to_vector(int arr[], int n, vector<int> &v);

// Vector concatenation.
// Concatenate two vectors `a` and `b`, the result should be stored in the vector `c`
// the function should work correctly even if the output vector `c` is not empty
void concat_two (vector<int> &a, vector<int> &b, vector<int> &c);

Please use the following main function as a test:

int main() {
    int arr1[4] = {1,3,5,7};
    int arr2[3] = {2,4,6};

    // Initialize vectors
    vector<int> v1;     // empty vector
    vector<int> v2(10); // or nonempty vector
    
    // convert the arrays to vectors
    array_to_vector(arr1, 4, v1);
    array_to_vector(arr2, 3, v2);

    // print two vectors
    print (v1); cout << endl;
    print (v2); cout << endl;
    
    // concatenate two vectors
    vector<int> result(5);
    concat_two(v1, v2, result);

    // print out the result
    print (result); cout << endl;
}

Task 2. Two-dimensional vector <vector<int> >.

Creation of a two-dimensional vector of vectors looks somewhat tricky, this is how it can be done:

int width = 3;
int height = 4;

// Initializing a 2-dimensional vector by calling a constructor 
// vector<T> v(n, value), where the second argument is a constructor call too:
vector <vector<int> > v (height, vector<int>(width));

// now, you can initialize the 2-dimensional vector in any way you want 
// by assigning the values to v[i][j]:
for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        v[i][j] = (i * width + j) % 10;
    }
}

Implement the following two functions and test them in the main function:

// Print a vector of vectors `v`         Example:
// v[0] - first line,                    1 2 3 4
// v[1] - second line,                   5 6 7 8
// v[2] - third line,                    9 1 2 3
// ... etc.
void print_v(vector <vector<int> > &v);

// Concatenate a vector of vectors `v`, store the result in `c`.
void concat_vector (vector <vector <int> > &v, vector <int> &c);

Task 3. Transposition of two-dimensional vectors

Matrix transposition is the reflection of the matrix over its main diagonal. That is, to get a matrix transposed, you write its rows as columns:

Implement two matrix transposition functions and test them in the main function (start with the first function, it’s easier to implement than the second one):

// Transpose a square vector of vectors
// The vector of vectors must be updated in-place
// 
// Example:
//
//  1 2 3      1 4 7
//  4 5 6  ->  2 5 8
//  7 8 9      3 6 9 
//
void transpose_square (vector <vector <int> > &v);

// Transpose a rectangular vector of vectors
// The vector of vectors must be updated in-place
//
// Example:
// 
//  1 2 3 4      1 5 9
//  5 6 7 8  ->  2 6 1
//  9 1 2 3      3 7 2 
//               4 8 3
//
void transpose_rectangle (vector <vector <int> > &v);