A few days back I found an interesting problem which reads the following:
Given two vector spaces generate the resulting set of its cartesian product. \begin{gather} \text{Let: } \mathcal{V}, \mathcal{W} \text{ be vector spaces}\\ \mathcal{V} \times \mathcal{W} = \{ (v,w) \mid v \in \mathcal{V} \land w \in \mathcal{W} \} \end{gather}
- Hint 1: A vector space is a set of elements called vectors which accomplishes some properties
- Hint 2: Design the solution for finite vector spaces
- Tip 1: It is recommended to use structures
- Constraint: You are forbidden to use any stl class
I solved this problem with the next approach:
struct vector_pair
{
double *vector_a;
double *vector_b;
size_t a_dimension;
size_t b_dimension;
};
struct cartesian_product_set
{
vector_pair *pairs;
size_t pairs_number;
};
cartesian_product_set vector_spaces_cartesian_product(double **space_v, size_t v_vectors,
size_t v_dimension, double **space_w, size_t w_vectors, size_t w_dimension)
{
cartesian_product_set product_set{new vector_pair[v_vectors * w_vectors], v_vectors * w_vectors};
for (size_t i = 0, j, k = 0; i < v_vectors; i++)
for (j = 0; j < w_vectors; j++)
product_set.pairs[k++] = vector_pair{space_v[i], space_w[j], v_dimension, w_dimension};
return product_set;
}
How could I improve this code if possible?
Thank you.
vector_pairs
? The question might also suggest you have to return a single vector of dimensiona_dimension + b_dimension
. \$\endgroup\$