09 - Advanced vector and array, algorithm

09 - Advanced vector and array, algorithm


🔨 🔥 Assignment 🔥 🔨

Create a new project in the way explained in the first instruction.


Two dimensional vectors

There are multiple ways to create multidimensional array. One of them is to use nested std::vector. To create a two-dimensional array (e.g. for storing the board in a checkers game) a vector of vectors can be used. Consider the example below:

const int number_of_rows = 5;
const int number_of_cols = 5;

std::vector<std::vector<int>> two_dimensional_vector;

for (int i = 0; i < number_of_rows; i++) {
   std::vector<int> row;
   for (int j = 0; j < number_of_cols; j++) {
       row.emplace_back(10 * i + j);
   }

   two_dimensional_vector.emplace_back(row);
}

std::cout << "Row 2, column 3: " << two_dimensional_vector[2][3] << std::endl;

🔨 🔥 Assignment 🔥 🔨

  1. Copy the code generating two dimensional array (using a std::vector of std::vectors). Add a function that prints the 2d array separated by a tabulator (\t) sign for columns and a new line for rows. The result should be:
0       1       2       3       4
10      11      12      13      14
20      21      22      23      24
30      31      32      33      34
40      41      42      43      44
  1. Create a two dimensional array (using std::vector type) of size 3x3 and fill it with random values (0-10). Write a function that multiplies elements in the array by 2 (in place).

algorithm module

The algorithm header file defines a set of utility functions designed mainly for collections. In order to use it include appropriate header:

#include <algorithm>

Interesting functions from algorithm header:

For more functions see: http://www.cplusplus.com/reference/algorithm/


🔨 🔥 Assignment 🔥 🔨

  1. Create a structure ExchangeRate containing fields:

    • date - std:string,
    • rate - float.
  2. Read the USD vs. PLN exchange rate data from file: USDvsPLN.csv. Store it in a std::vector of ExchangeRate structures.

  3. Using std::min_element find a date when USD vs. PLN exchange rate was the smallest. Print the date and corresponding rate.

  4. Using std::max_element find a date when USD vs. PLN exchange rate was the largest. Print the date and corresponding rate.

  5. Check if all the exchange rates stored in file are from year 2020 (std::all_of).

  6. Find a date when the exchange rate went over 4.22 for the first time (std::find_if).

  7. Count for how many days the exchange rate was over 4.22 (std::count_if).


Final assignments 🔥 🔨

Exercise 1 - Tic-tac-toe game

Write a simple Tic-tac-toe game in the following steps:

Exercise 2

This year, due to a lack of a snow, Santa Claus forgot to prepare gifts and now asks you to help. Write a program, that will help him plan a flight. Santa will fly from north-west and would like to visit children in specific order. Read children’s names and coordinates of their houses (integer numbers, x rises in the east direction, y rises in the north direction) and print in an order form the one living the most north-west to the one living the most south-east. Use a proper structure and a sort function with a proper comparator.

Example input:

Kacper 1 1
Melchior -4 1
Baltazar -3 4
Maria 3 1

Example output:

Baltazar
Melchior
Kacper
Maria


Authors: Michał Fularz, Piotr Kaczmarek, Dominik Pieczyński, Tomasz Mańkowski, Jakub Tomczyński