So i have stumbled upon an exercise on an C++ book called - "Guide To Scientific Computing in C++"
Here is the exercise: "Write code that dynamically allocates memory for three 2 × 2 matrices of double precision floating point numbers, A, B, C, and assigns values to the entries of A and B. Let C = A + B. Extend your code so that it calculates the entries of C, and then prints the entries of C to screen. Finally, de-allocate memory. Again, check you have de-allocated memory correctly by using a for loop as in the previous exercise."
This caught my eye, i tried to do this problem using both 2D Arrays (i was able to do it very easily) and with vector of vectors (i failed).
I did a lot of research and read some post on StackOverFlow and basically the opinion was universal - when dealing with matrices the option is always 2D arrays.
But knowing that there are a lot of programmers here (and that i am a newbie with C++), i would really like to read more opinions about this topic!
P.S: Here is a little snip of my failed attempt at trying to create a matrix using vector of vectors:
for (int row{ 0 }; row < 2; row++) { // Create Matrix A - goes through the matrix rows for (int col{ 0 }; col < 2; col++) { // goes through the matrix columns temp.push_back(rand() % 201); // add random number to the temporary vector } matrixA.push_back(temp); } // Outputing for (int row{ 0 }; row < matrixA.size(); row++) { // goes through the matrix rows for (int col{ 0 }; col < matrixA.at(row).size(); col++) { // goes through the vectors inside matrixA cout << matrixA.at(row).at(col) << "\t"; } cout << endl; }
This is the output: enter image description here