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.
Related
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?
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'm trying to build an incomplete binary tree from data provided in a text file
2
3 8
2 10 0 12
1 8 0 0 4 0
which would form a tree like this:
I know roughly what I need to do, I think:
read in from the file line-by-line.
pass an array to the build function.
As it's building the tree, the tree data type will keep track of the last nodes that it added in an array of pointers (eg, if we just finished adding the 3rd row of the text file, the a previous nodes array would look like pointers to the nodes containing [2 10 12]
as we start adding the 4th line, we create an array of 3*2=6 length to store pointers to the nodes as we add them.
we go through the array of pointers from the previous run (containing [2 10 12]) and create left and right children nodes for any non-zero keys that got passed through. We put pointers to those nodes in our 6-long array.
when we get a blank line, we're done.
The problem I'm running into is, I'm not sure how to store a array of pointers to nodes that changes size each time I call the build function and is a class variable (for the Tree class). Am I going about this the right way? Is there an easier way to approach this?
Like 2D array, array[row][column], you can use 2D vector ie. vector< vector<int> > V is a vector of vectors.
In vector< vector<int> > V, inside vector indicate row, and outside vector indicate column (for your understanding I just tell).
Here 2D vector is resize able. Not necessary that every column size will be equal. You can handle push, pop and you can store data according your memory size.
Example:
You can resize like of that:
int num_of_col = 5;
int num_of_row = 9;
double init_value = 3.14;
vector< vector<double> > matrix;
//now we have an empty 2D-matrix of size (0,0). Resizing it with one single command:
matrix.resize( num_of col , vector<double>( num_of_row , init_value ) );
// and we are good to go ...
You can learn here vector
Why don't you use a std::vector instead of array? It behaves exactly like an array but is resizable.
Example of usage:
using namespace std;
Node *a = new Node;
vector<Node*> vec;
vec.push_back(a);
I am trying to use memcy to copy an sub-array of an updated array A into an array B. Both A and B array have the same number of dimensions. The data going into array A is in the order of A.get_size(3) (Let's call dimension N here). Each update N will increase by 3(That means 3 set of data is added queueing in terms of dimension N). I need B to obtain the updated data (sub-array)only and the next copy will replace the previous 3. So I guess copy array A starting from (N-2) into B could do the job.
//initializing a new array B to obtain sub-array from A
array B(A.get_size(0), A.get_size(1), A.get_size(2), 3, A.get_size(4));
//Get the forth dimension N
N=A.get_size(3);
//copy the sub-array of A (from N-2 to N) into B(from 1 to 3 in dimension N)
memcpy(B.begin()+A.get_size(0)*A.get_size(1)*A.get_size(2)*(1)*A.get_size(4), A.begin()+A.get_size(0)*A.get_size(1)*A.get_size(2)*(N-2)*A.get_size(4), sizeof(T)*(A.get_size(0)*A.get_size(1)*A.get_size(2)*A.get_size(4)));
However, this could copy the number of elements but not the data value inside. Does it mean it gets memory leak? How should I make changes? Thanks!
Edited:
Thanks to the comments, I tried to use std::copy instead of memcpy, but this does not work in this case because the array A contain complex float. It gives this error when compiling,
error: no matching function for call to ‘copy(std::complex<float>*, const std::complex<float>*, long unsigned int)’
Anymore ideas except from using std::copy? Thanks!
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>