0

I want to turn a 2D array like this [ [x, y], [a, b], [z, e] ] into [x, y, a, b, z, e] I have tried multiple thing with indexes and for loops but most of them don't work and the one that did was very buggy and sketchy how do I do that i c++ i am a beginner, and I am trying to learn about matrices

Farouk
  • 261
  • 1
  • 10
  • Do you want it to work with standard arrays or maybe `std::vector<>`? – Miłosz Brzechczyn Aug 08 '21 at 13:34
  • i cant use std::vector or array i am using this array with modern opengl and it only supports float[] arrays – Farouk Aug 08 '21 at 13:37
  • It's not a problem as `std::vector<>` has a `.data()` method which returns `float*` array. – Miłosz Brzechczyn Aug 08 '21 at 14:19
  • it doesn't seem to work with my code its in modern opengl – Farouk Aug 08 '21 at 14:26
  • here is the code //creating a vertex buffer object VertexBuffer VBuff; VertexArray vertexArray; //creating a vertex array vector vertices = { -1, 0, 1, 0 }; //writing a vertex array to our vertex buffer VBuff.write(vertices.data(), sizeof(vertices.data())); shader.setColor(1,0,0,1); // bind buffers, ... VBuff.bind(); – Farouk Aug 08 '21 at 14:27
  • vertexArray.bind(); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0); glDrawArrays(GL_LINES, 0, sizeof(vertices.data()) / 4 / 2); // unbind buffers, ... VBuff.unbind(); vertexArray.unbind(); VBuff.~VertexBuffer(); vertexArray.~VertexArray(); glfwSwapBuffers(window); glfwPollEvents(); – Farouk Aug 08 '21 at 14:27
  • but even if it did work it still would not have solved the problem – Farouk Aug 08 '21 at 14:31
  • Could you please provide entire code, like paste it to the pastebin.com or edit the main post, I would be easier to read that. – Miłosz Brzechczyn Aug 08 '21 at 14:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235755/discussion-between-milosz-brzechczyn-and-farouk). – Miłosz Brzechczyn Aug 08 '21 at 14:39
  • it is a very big project with many files i'll post the main.cpp file – Farouk Aug 08 '21 at 14:40
  • https://pastebin.com/Sv3JL174 – Farouk Aug 08 '21 at 14:41

1 Answers1

1

The best approach would be to create a new array. Use the following code which is quite simple and clear as well

  int arr[rows*columns];
    int a=0;
    int array[rows][columns]={assign values}; 
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
           arr[a]=array[i][j];  
           a++;
        }
    }