I have 4 arrays that I need to combine into 1 by adding all the corresponding cells. So I would add up Cell 1,1 (row 1, column 1) of all 4 matrices and put that into Cell 1,1 of the resultant matrix.
Well the 4 matrices are coming from another program and instead of writing an adder function that takes 4 matrices as the argument, I just had 1 vector. So my function looks like this:
void combine_matrix(vector<int*> all_arrays) {
vector<int> cell_values; //Vector that stores the value of a particular cell of each matrix
for (int i = 0; i<all_arrays[0].size(); i++)
}
vector<int*> all_arrays is a vector of pointers that points to the 4 arrays. This way, the matrices can be of any size (all 4 have to be the same dimensions though) and the function would still work.
Basicaly I want a vector of pointers pointing to the array as a whole so I can use the size() function but all the examples I've seen are only giving me pointers to the actual elements in the matrix. Any idea on how to do what I need? Thanks
Make a Matrix class that contains the pointer to array and the size and other things you might need to work with, and even better, make a CombinedMatrix class that contains vector<Matrix>
Related
I'm new to vectors.
I have two vectors, vector1 and vector2, both have two values each. Now, using these two vectors, I have made a 2 dimensional vector, vector_2d whose value(contents) I want to print. I use the below code and everything works fine.
vector<int> vector1;
vector<int> vector2;
vector1.push_back(10);
vector1.push_back(20);
vector2.push_back(100);
vector2.push_back(200);
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);
cout<<"The elements in vector_2d are: "<<vector_2d.at(0).at(0)<<" "<<vector_2d.at(0).at(1)<<" "<<vector_2d.at(1).at(0)<<" "<<vector_2d.at(1).at(1)<<endl;
Now, I want to replace the first value in vector1 (which is 10) with 1000. I do it by a simple assignment operator:
vector1.at(0) = 1000;
Now, I try to print vector1 and vector_2d again. I get the result that I expected with vector1:
cout<<vector1.at(0)<<endl; //1000
But when I print vector_2d, I get the same result as before. The changes done in vector1 are not being reflected in the 2D vector. Why is this happening?
The value in your 2D vector is not changed, because while pushing back 1D vectors
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);
you made copies of each of them. Therefore, changing values in vector1 will not change the values in its copy stored as element 0 of vector_2d. If you would like to change the values in 2D vector, you can do it directly:
vector_2d.at(0).at(0) = 0;
Another possibilities were mentioned in the comments to your question (like having a vector of pointers or references), but I do not recommend those because of possible memory violations (e.g. if your 2D vector will live longer than 1D vectors that are referenced).
I want to do this in C++ code:
Initialize an array with 4 rows and 5 columns;
Ask the user about two row numbers in the range 0 to 4;
Swap the rows defined by the user in a 2D array.
You can use the following algorithm:
let arr be array
let a,b be row indices that are swapped
for each column index i
swap arr[a][i] and arr[b][i]
However, if instead of a 2d array you were to use an array of row pointers, you could instead simply swap the pointers.
You could declare an extra variable to temporarily hold each value as you swap it from row A to B.
Not sure what you mean by "best", is this in terms of speed or memory requirements?
I have a 2d vector of string and need to isolate out three of the columns into 3 separate 1d arrays so i can convert them to doubles and perform operations on them.
Simply using:
for (int i = 0; i < 100; i++)
{
vectorname[i][2] = arrayname[i];
}
doesn't work and I don't understand why.
Sorry im new to coding and thanks in advance.
Thanks to first reply, i don't care if i remove the data or not, i just need it so i can operate on it, my vectors are declared as:
string vectorname[101][5];
string arrayname[99];
string arrayname2[99];
string arrayname3[99];
Ok I dont have my vectorname defined as a vector, it's just a 2d string, can i extract a column from that?
Do the columns still need to remain in the 2D vector, or be pulled out completely as independent data?
If you need independent data, I would do
vector<double> col(vectorname[i]);
Then manipulate col as needed. You could even do move construction if you don't need that data in the 2D vector anymore.
I think you have actual logic problems elsewhere though. Show us your vector declarations.
EDIT: What...? You're not declaring vectors at all. You're using pure arrays. And a 2D array shouldn't be called a vector. You're confusing other programmers with your name scheme.
The proper way to declare a vector of strings is
std::vector<string> vectorname(100);
And a vector of vector of strings (a 2D vector) is
std::vector<std::vector<string>(5)> vectorname(101);
But moreover, your dimensions mismatch. Your 1D vectors need to be the same length as the given dimension of your 2D, or 5 (101?) in this case.
If you're trying to copy the values along the first dimension of values, there's no direct constructor for that. You have to manually loop from 0 to 100 and say
arrayname[i] = vectorname[i][n];
Where n is your column number.
Might sound like a stupid question:
Is it possible to pass 2D array as an argument to a function which takes 1D array
For example:
I have an array players[4][5] and also a function bestCard(), the header for that function looks something like this void bestCard(int arr[5]) <- this 5 is referring to the second dimension of array players
I want to pass players[4][...] in the function bestCard() in a way that the only last dimension is considered to be as an array i.e players[5]
Not sure if my question is comprehensive.
A 2D array is an array of 1D arrays.
In your case, that means player[i] is an array of five elements, for i between 0 and 3. So passing player[i] to your function will pass an array of 5 elements (or more strictly, a pointer to the first element of an array of 5 elements).
I am a beginner at C++, but as far as I know, it would not be possible to pass a 2-d array to your function bestCard(), assuming that you have not overloaded your bestCard() function to take different types of parameters.
Based on the information you have given to me in your question, I am guessing that there are 4 players, and each holds a hand of 5 cards.
If you want to pass bestCard() a single player's hand, you will have to write something like this:
bestCard(players[0]) // Determine the bestCard for the first player.
bestCard(players[1]) // Determine the bestCard for the second player.
...
bestCard(players[3]) // Determine the bestCard for the fourth player.
Even though players is in fact a 2-d array, each of the players[i] (for 0 < i < 4) evaluate to 1-dimensional arrays. Thus, bestCard() ought to be able to accept them as an argument.
I have a quesion that I found in internet but it was not has best solution. My question is that I have one 2D matrix and I want to delete one column at ith position. Example the matrix can be represent as A[2][3]={1,2,3,4,5,6}. And I want to delete the column at postion 2. So the output is B={1,3,4,6}. Can you help me please?
A=[1 2 3
4 5 6]
The output
B=[1 3
4 6]
The function is
int** delete_column(int** inputMatrix,int position)
{
//The size of outMatrix must be smaller than inputMatrix
return outMatrix;
}
You can not do this in-place with static arrays in c++. You will need to create another array and copy data there. If you use an array of pointers to dynamic arrays however, you can move the elements in-place and than call realloc to shrink the arrays. You can Also use a vector of vectors and call remove on them.