C++ to matlab conversion [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to convert some C++ code into the matlab
in the C++ there is following statement:
static double *L[2];
for (int y=0;y<2;y++)
L[y] = new double[size];
in matlab I'll need to initialize the L parameter with some initial value like 0.
Can you please explain what does the C++ code means?
I need the same for the following:
static double **a[2];
for (int x=0;x<2;x++)
{
a[x] = new double*[size];
for (int y=0;y<size;y++)
{
a[x][y] = new double[numstates];
}
}
BR

L is an array of two arrays, each of them is an array of size doubles. It's the same as
L[0] = new double[size];
L[1] = new double[size];
You may interpret L as a 2xsize matrix.
Likewise, a is 2xsizexnumstates matrix of doubles.

Can you please explain what does the C++ code means?
The first snippet is just initializing a (2 * size) matrix of double-precision floating point numbers.
The second snippet is doing the same, but it's initializing a (2 * size * numstates) matrix.

Related

Can someone tell me what is wrong with this statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How is this statement
int* p = new int[10];
p[10] = 5;
delete[] p;
different with this statement ?
int* p = new int[10];
*p = 5;
delete[] p;
I wanna what is wrong with the first code. I'm kinda new to C++, so any explanation is appreciated. Thanks.
The behaviour of p[10] is undefined as the array has only 10 elements and the first element is at position 0. You can access p[0] (which is the same as *p) to p[9] inclusive.
In other words, arrays in C++ are zero-based. Cf. Fortran, for example, where they are one-based.

Concatenate two double matrices [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I tried to write a simple function to concatenate two 2D double matrices.
double** concat(double **upmat,double **lowmat,int row,int col,int filecount)
{
double **temp=new double* [filecount*row];
for(int i=0;i<row*filecount;i++){
temp[i]=new double [col];
}
if (filecount>1)
std::copy(upmat,upmat+(filecount-1)*row*col,temp);
std::copy(lowmat,lowmat+row*col,temp+(filecount-1)*row*col);
return temp;
}
This function returns a 2D pointer. When I tried to access data from that pointer it shows invalid memory access error!!
temp, low, and high are all double** (pointer to pointer to double) , but you do std::copy as if they are double* (pointer to double) .

Writing 2D array to binary file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
The following code is supposed to write a 2D array (quad_array) to a binary file. The code creates the file (quads.dat) but does not write anything to it (0 bytes).
XMFLOAT3 * quad_array;
quad_array = new XMFLOAT3 * [quad_width];
for (unsigned int x = 0; x < quad_width; x++) {
quad_array[x] = new XMFLOAT3[quad_height];
}
// ... fills quad_array with data...
ofstream ofs("quads.dat", std::ofstream::binary);
ofs.open("quads.dat");
streamsize size = sizeof(XMFLOAT3)*quad_height*quad_width;
ofs.write((char*)&quad_array[0][0], size);
ofs.close();
I think that you should remove the
ofs.open("quads.dat");
statement, because you already gave the filename at the previous line (constructor).
But that is not enough. You don't have a 2D array, but an array of pointers to arrays, so they are not contiguous in memory.
If quad_height and  quad_width are compile-time constants, you could use nested std::array with C++11.
See also this.

taking sorted array and value in a function and placing that value in correct location [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having problem in this particular question, Please guide me with simple c++. in a function we have to take 2 arguments one is sorted array and the other is value, function should place that value in correct location so that array remains in sorted order. There will be a use of pointers but i m confused in this question just guide me how to make use of pointers in this particular question. It would be better if you provide me with the sample good on how it works. Thanks in advance.
Seems trivial:
template <typename T>
int insert(T* array, int size, T const& value) {
T* position = std::upper_bound(array, array + size, value);
std::rotate(position, array + size, array + size + 1);
*position = value;
return size + 1;
}

C++ How to resize a dynamic array without losing data? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to resize a dynamic array without losing data.
Like this:
double * pVecDin;//POINTER
int num_values = 2;
pVecDin = new double[num_values];
pVecDin[0]=5;
pVecDin[1]=6;
int new_num_values=4;
pVecDin = new double[new_num_values];
//Next I lost value of pVecDin[0] and pVecDin[1]
pVecDin[2]=8;
pVecDin[3]=9;
Do I need make an Auxiliar Dynamic Array to copy the old values?
Like:
int new_num_values=4;
double * pVecDin_aux; //POINTER
pVecDin_aux = new double[new_num_values];
pVecDin_aux = pVecDin;
for(int i=0; i < n; i++)
{
pVecDin_aux[i] = pVecDin[i];
}
Make a new, empty array with the new desired size.
Copy everything in the old array to the new array.
Delete the old array.
Assign the pointer of the old array to the address of the new array.
Or use a vector.
As nhgrif has mentioned, you need to make copy of the array.
There is another way if you can use malloc for memory allocation, then you can use realloc for resizing the array.
There are better ways as suggested by other like to use std::vector or list.