0

the code i'm working on takes a matrix of different dimensions every input so i need to be dynamic and i need to make it with a vector but it gets me tons of errors each time i try

const int n=6;   const int m=3;
float matrix1 [n][m];
float matrix2 [n][m];
float matrixsum [n][m];

how to make this into a vector instead of a 2d array here is the full code that takes the string then cut it and then takes the numbers in it and make it into an array so you can see the big picture https://ideone.com/4MwJVF it takes an input like this

[1 -2.5 3;4 5.25 6;7 8 9.12]
+
[3 4.2 10.2;12 -1 0;67 2 13]

and an output like this

[4 1.7 13.2;16 4.25 6;74 10 22.12]
  • Cannot give a good answer unless you let us know what is going wrong with what you tried. Please add code from what you attempted. [mcve] is recommended. – user4581301 Apr 29 '19 at 21:57
  • @user4581301 well.. i linked the complete code i've tried – Yusuf Sameh Apr 29 '19 at 22:09
  • Links rot and get blocked by firewalls. Don't use them for anything important to understanding and answering the question. – user4581301 Apr 29 '19 at 22:10
  • the problem is that i need the 2d array to be up to 100*100 so i'm going to need to use vectors for such a thing but it doesn't seem to work properly in my code – Yusuf Sameh Apr 29 '19 at 22:11
  • Looked at the link. It shows nothing of how you are using `vector`s. – user4581301 Apr 29 '19 at 22:12
  • its a big code like 170 lines or something i can't post it here – Yusuf Sameh Apr 29 '19 at 22:12
  • Start with a few simple experiments to get a feel for how to use `vector`s, then try to apply vectors to your code. – user4581301 Apr 29 '19 at 22:14
  • thats the one where i used vectors https://ideone.com/QJohqt – Yusuf Sameh Apr 29 '19 at 22:14
  • Pass the `vector`s by reference or they get copied, you change the copy, and your work is lost. Eg; `void splitter(string x,vector > & matrix)`. Unrelated, consider passing the `string` by reference as well, but since you don't intend to change the `string` make it `const`. `void splitter(const string & x,vector >matrix)` – user4581301 Apr 29 '19 at 22:19
  • Near duplicate: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – user4581301 Apr 29 '19 at 22:20
  • i don't think that's the case the code runs just fine if it was a static 2d array the thing is i need it to be 2d but dynamic because the input is dimensions might vary – Yusuf Sameh Apr 29 '19 at 22:26
  • [Arrays decay to pointers.](https://stackoverflow.com/questions/1461432/what-is-array-decaying) They are ALWAYS passed by reference. Everything else follows different rules and you have to ask for pass by reference. This is because back in early days of C you didn't have the processor power or the memory to pass an array by value. 50 years later things are different, but because billions of lines of code would be ruined if the behaviour was changed, the behaviour is not being changed. – user4581301 Apr 29 '19 at 23:20

1 Answers1

0

A dynamic-sized matrix would look something like this

std::vector<std::vector<float>> matrix;

matrix.push_back(...) // push a row vector
matrix[0].push_back(...) // push an element
Brady Dean
  • 2,150
  • 2
  • 19
  • 38