I want to create a 2-dimensional List in C++\CLI. Question is how to declare it?
I have tried this:
List<List<int>^>^ H = gcnew List<List<int>>(); // Scoring matrix H
H->Add(gcnew List<int>() );
for (i = 0; i < n; i++) // Fill matrix H with 0
{
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Then I get a lot of syntax errors, starting with this one:
error C3225: generic type argument for 'T' cannot be 'System::Collections::Generic::List', it must be a value type or a handle to a reference type
In this declaration
List<List<int>^>^ H = gcnew List<List<int>>();
The right type specifier does not correspond to the left type specifier. Should be
List<List<int>^>^ H = gcnew List<List<int>^>();
With advice from Hans and Vlad, this seems to work:
List<List<int>^>^ H = gcnew List<List<int>^>(); // Scoring matrix H
for (i = 0; i < n; i++) // Fill matrix H with 0
{
H->Add(gcnew List<int>() );
for (j = 0; j < m; j++)
{
H[i]->Add(0);
}
}
Thx, Jan
Related
I'm tring to access the values of a dictionary property in a grid,such as Fluvial facies or lithologies etc.I have read the coursebook and help docs, but didn't find anything relevant.The coursebook only has examples of creating properties, but not accessing properties.Below is the code I tried:
Grid grid = arguments.Input_Grid;
if (grid == null)
{
PetrelLogger.ErrorStatus("HelloGrid: Arguments cannot be empty.");
return;
}
Index3 currentCell = new Index3();
int maxI = grid.NumCellsIJK.I;
int maxJ = grid.NumCellsIJK.J;
int maxK = grid.NumCellsIJK.K;
for (int i = 0; i < maxI; i++)
{
for (int j = 0; j < maxJ; j++)
{
for (int k = 0; k < maxK; k++)
{
currentCell.I = i; currentCell.J = j; currentCell.K = k;
if (grid.IsCellDefined(currentCell) && grid.HasCellVolume(currentCell))
{
//DictionaryProperty p = ???
//int val = p[currentCell] ???
}
}
}
}
You need to use the "FastDictionaryPropertyIndexer" or "FastPropertyIndexer" for regular properties.
foreach (var dictProp in grid.DictionaryProperties)
{
int numCellsI = dictProp.NumCellsIJK[0];
int numCellsJ = dictProp.NumCellsIJK[1];
int numCellsK = dictProp.NumCellsIJK[2];
float[] values = new float[dictProp.NumCells];
var dpsa = dictProp.SpecializedAccess;
using (var fdpi = dpsa.OpenFastDictionaryPropertyIndexer())
{
int index = 0;
for (int k = 0; k < numCellsK; k++)
{
for (int j = 0; j < numCellsJ; j++)
{
for (int i = 0; i < numCellsI; i++)
{
values[index] = fdpi[i, j, k];
index++;
}
}
}
}
}
You also need to be careful about the indexing since it varies by project. For instance, you may need to reverse the order of traversal in the J direction or you could end up with some strange results.
I tried to find the time complexity of the following two functions:
the first one
public static int myMethod1(int[] arr) {
int x = 0;
for (int i = 0; i < arr.length / 2; i++) {
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr.length; k++) {
x++;
if (k == arr.length / 2) {
break;
}
}
}
}
return x;
}
So with this one i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k…
i and j, and k are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with three N’s.., which gives is O(N^3)
The next one is:
public static int myMethod(int N) {
int x = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N / 2; j++) {
for (int k = 1; k < N;) {
x++;
k *= 2;
}
}
}
return x;
}
With this i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k… i and j are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with two N’s.. The last loop k doubles, which gives is log(n).
The result of the this problem is therefore O(N^2· log (N))
is this correct? and if it is not, why?
You are right. In both of the questions
My problem is that I am trying to load in joint/bone data from an fbx file in Direct X c++ using Assimp, but I want to store the eights and indices inside the same vertex struct that I store position, uv, etc.
I can make a loop for every vertex, but I also want to make a loop over each bone.
That means I can't have both the joint data and the other data in the same loop.
Should I create multilpe vertec objects then combine them afterwards?
I also am not sure how to find the bone ID and the weight for each vertex, I am counting on 4 per bone, but maybe I should not have that last loop at all?
Im not sure how to set it up.
I would appreciate some help, thank you very much.
for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
for (UINT n = 0; n < 4; n++)
{
//if
}
}
}
//////////////////////////////////////////////
for (UINT k = 0; k < currentMesh->mNumVertices; k++)
{
Vertex vert;
vert.position.x = currentMesh->mVertices[k].x;
vert.position.y = currentMesh->mVertices[k].y;
vert.position.z = currentMesh->mVertices[k].z;
vert.TexCoord.x = currentMesh->mTextureCoords[0][k].x;
vert.TexCoord.y = currentMesh->mTextureCoords[0][k].y;
vert.normal.x = currentMesh->mNormals[k].x;
vert.normal.y = currentMesh->mNormals[k].y;
vert.normal.z = currentMesh->mNormals[k].z;
vertexVector.push_back(vert);
}
for (UINT k = 0; k < currentMesh->mNumBones; k++)
{
aiBone* bone = currentMesh->mBones[k];
for (UINT m = 0; m < bone->mNumWeights; m++)
{
aiVertexWeight weight = bone->mWeights[m];
if (vertexVector[weight.mVertexId].joints.x == 0)
{
vertexVector[weight.mVertexId].joints.x = k;
vertexVector[weight.mVertexId].weights.x = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.y == 0)
{
vertexVector[weight.mVertexId].joints.y = k;
vertexVector[weight.mVertexId].weights.y = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.z == 0)
{
vertexVector[weight.mVertexId].joints.z = k;
vertexVector[weight.mVertexId].weights.z = weight.mWeight;
}
else if (vertexVector[weight.mVertexId].joints.w == 0)
{
vertexVector[weight.mVertexId].joints.w = k;
vertexVector[weight.mVertexId].weights.w = weight.mWeight;
}
}
}
I accessed the same structure I already have and added the stuff afterwards.
I pick the first row and multiply each element by its cofactor,
but in some cases the method is returning nan.
For example,
1 0 0 1
0 2 0 0
0 0 3 0
0 0 0 4
in this case the method returns nan.
Does anyone know what I did wrong?
getDet3 returns determinant of a 3x3 matrix and it works fine.
-(double) getDet4:(double[4][4])mat {
double det = 0;
double small[3][3];
int i, j, k;
int i_ = 1, j_;
for ( i=0; i<4; i++ ){
if (mat[0][i] == 0) continue;
// get the small matrix here
for ( j=0; j<3; j++ ){
j_ = 0;
for ( k=0; k<3; k++ ){
if ( i == j_ ) j_++;
small[j][k] = mat[i_][j_];
j_++;
}
i_++;
}
det += mat[0][i] * [self getDet3:small] * pow(-1, i+j);
}
return det;
}
Well, there are a few mistakes in your code.
1) The initialization of i_ = 1 should be done just before the j loop, otherwise it will keep the old value.
2) The computation of pow(-1, i+j) should only depend on i, since j has the same value every time in that expression (namely, 3).
So, assuming that getDet3 is correct, the mistake is introduced by i_ going out of bounds. As a whole, the code should look like:
-(double) getDet4:(double[4][4])mat {
double det = 0;
double small[3][3];
int i, j, k;
int i_, j_;
for ( i=0; i<4; i++ ){
if (mat[0][i] == 0) continue;
// get the small matrix here
i_ = 1;
for ( j=0; j<3; j++ ){
j_ = 0;
for ( k=0; k<3; k++ ){
if ( i == j_ ) j_++;
small[j][k] = mat[i_][j_];
j_++;
}
i_++;
}
det += mat[0][i] * [self getDet3:small] * pow(-1, i);
}
return det;
}
Personally, I find your variable names confusing. If I understand your idea correctly, you expect i_ to have the value j + 1 and j_ to be k < i ? k : k + 1. IMHO, it would have been less confusing to have named them j_p andk_`, or even to just use the equivalent expression.
In any event, you don't reinitialize i_ inside the outer for loop. So it actually just keeps on incrementing, resulting in array indices outside of the array bounds.
I am trying to print out arrays incrementally like this;
TractMultBox->Text = rows[0] + newline;
TractMultBox->Text += rows[1] + rows[0] + newline;
TractMultBox->Text += rows[2] + rows[1] + rows[0] + newline;
which would give an output like this
3
43
543
I can do fine with this code, however. It would like to use a for loop, that would make it easier, since I would like it to output all arrays until max is reached automatically.
I'm assuming you want to concatenate and not sum.
string text;
for (int i = 0; i < rows.count; ++i)
{
text = rows[i] + text;
TractMultBox->Text = text + newline;
}
for less lines of code.
string text = newline;
for (int i = 0; i < rows.count; ++i)
{
TractMultBox->Text = (text = rows[i] + text);
}
but that's a little hard to read.
Sounds like a job for a for loop indeed perhaps something like this:
#include <iostream>
int main()
{
int rows[3] = {3, 4, 5};
for (int i(0); i < 3; ++i)
{
for (int j(i); j >= 0; --j)
std::cout << rows[j];
std::cout << "\n";
}
std::cin.get();
return 0;
}
If rows contained 345 this would give you the following output:
3
43
543
Not sure if that's what you wanted but you can adjust the loops accordingly. The key is to have 2 for loops.
Edit: Changed to self contained example you can play with
What about a double loop like:
for (int i = 0; i < maxNRows; ++i)
{
for (int j = 0; j < i; ++j)
{
TractMultBox->Text += rows[j];
}
TractMultBox->Text += newline;
}