I want to initialize an entire array at once but I can't find any examles of this being done anywhere.
I want to do something like this:
int a [][] = {{0,1,0},
{0,1,1},
{2,1,0}};
Unfortunately GML is not like many other languages in the sense that GML does not have single line array initialization. You can easily write a script to accomplish this, but the GML docs use this method to initialize arrays. The example they gave initializes a 10 record array (0-9) with zeros.
var i;
i = 9;
repeat(10)
{
array[i] = 0;
i -= 1;
}
If you want different values for every record then you have to manually type every position. This is the example the docs gave.
count = 3;
array[count] = "you?"
count -= 1;
array[count] = "are "
count -= 1;
array[count] = "How "
count -= 1;
array[count] = "Hello!"
count -= 1;
In regards to a script: Here is a simple one for 1D arrays. Used as var myArray = array(record 1, record 2, 3, 4, ...)
///array(*args);
var arr;
for (var i=0;i<argument_count;i+=1)
{
arr[i] = argument[i];
}
return arr;
If you are using a current version of GameMaker, there's array literal syntax in form of [...items] (documentation). So you can do
a = [[0,1,0],
[0,1,1],
[2,1,1]];
and that'll work fine.
The only thing to note that this'll produce an array of arrays (which is how arrays work in most languages) rather than GML-specific legacy 2d array, so you'd need to use a pair of [index] accessors rather than [index1, index2].
So in c I have a data matrix that is n x m. How can I extract a single row to pass it to a new variable. My python code looks like this:
new_var = data[i, :]
Please help me translate into C.
My way is
int i = rand(num_rows);
double new_var[num_cols];
for (j = 0; j < num_cols; j++)
{
new_var[j] = data[i][j];
}
Please comment.
Many thanks
What you are doing is correct. But I don't get what you are trying to do with this -
int i = rand(num_rows); // using rand like this is incorrect syntax.
If you want a random number between 0 to num_rows. Correct will be -
int i = rand()%num_rows;
See here how to use rand.
Okay, so first off you should note that I am using visual studio 2010. Anyways, I am getting a very weird error in my for each loop that doesn't make any sense to me.
BYTE CPUKeys[][0x10] =
{
{0x28,0x53,0x71,0xD9,0x7B,0x47,0xCC,0x50,0xAF,0x45,0x8D,0xB3,0xED,0x53,0x22,0x13},//Randy//encrypted
{0x6E,0x38,0xB0,0xEF,0x6E,0x96,0x20,0x16,0xE5,0xCA,0x4B,0xE9,0x23,0x4E,0xC6,0xA5},//Josh//encrypted
{0x95,0x2C,0x20,0x98,0xF9,0x99,0x28,0x0F,0xEE,0xA7,0x8F,0x48,0x58,0x01,0xB7,0x07},//Falcon//encrypted
{0x64,0xDC,0x1E,0xFA,0xD2,0xAE,0x57,0x6C,0x0B,0xD0,0x39,0x6A,0x13,0x2C,0x91,0xE0},//Justin//encrypted
{0x8D,0x82,0x6D,0x71,0x82,0xDC,0x83,0x8F,0x79,0x51,0xB8,0x7C,0x1F,0xC1,0xBF,0xD4},//Bypass//encrypted
{0x27,0xF2,0xA7,0xF3,0xE2,0xDC,0x01,0x19,0x17,0xF4,0x11,0xFE,0xE9,0xB5,0x0C,0x2C},//Surge//encrypted
{0x6D,0x7C,0x86,0x9A,0x6A,0xE1,0xD4,0x10,0x76,0x16,0x11,0x7C,0xED,0xB3,0x4D,0x56},//Noel//encrypted
{0x3D,0x6C,0x88,0x8C,0x9D,0x3A,0xA5,0x40,0x64,0xDF,0xDF,0x8D,0x94,0xFD,0x28,0xF1},//XexRgh//encrypted
{0x06,0x0D,0x8C,0xB6,0xB6,0x6D,0x29,0xD7,0x41,0xE1,0x13,0x35,0x84,0x80,0x68,0x31}//Zoobzy//encrypted
};
Alright so there is my multi-dimensional BYTE array.
for each(BYTE ID[0x10] in CPUKeys)
{
And there is my for each loop. The weird thing that is happening with it is my error that I am getting on ID. The error states: 'for each' iterator type "BYTE[16]" is not compatible with element type "BYTE[16]". Any idea why it might say this? What am I doing wrong, and how do I fix it?
Try this if it works:
for(auto& rows: CPUKeys)// Iterating over rows
{
for(auto& elem: rows)
{
// iterate every row's column
}
}
As far as i know there is not foeach loop in c++.
Use two for loops instead. Something like this
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 16; ++j)
{
BYTE value = CPUKeys[i][j];
}
}
So i have this piece of code:
int* get_lotto_draw() //Returns an array of six random lottery numbers 1-49
{
int min = 1;
int max = 49;
int counter = 0;
srand(time(NULL));
int *arrayPointer = malloc(6 * sizeof(int));
for(counter = 0; counter <= 5; counter++)
{
arrayPointer[counter] = rand()%(max-min)+min;
}
return arrayPointer;
}
This gives me 6 int* but sometimes these int* values can be the same. How can i compare each of them to eachother, so that if they are the same number, it will re-calculate on of the values that are equal ? Thanks for your time.
make a search in the array for same number before storing the number as,
for(counter = 0; counter <= 5; counter++)
{
int x1 = 1;
while(x1)
{
int temp = rand()%(max-min)+min;
for(i = 0; i < counter; i++)
{
if(arrayPointer[i] == temp)
{
break;
}
}
if(i == counter)
{
x1 = 0;
arrayPointer[counter] = temp;
}
}
}
The problem is that random-number generators don't know or care about history (at least, not in the sense that you do--software random number generators use recent results to generate future results). So, your choices are generally to...
Live with the possibility of duplicates. Obviously, that doesn't help you.
Review previous results and discard duplicates by repeating the draw until the value is unique. This has the potential to never end, if your random numbers get stuck in a cycle.
Enumerate the possible values, shuffle them, and pick the values off the list as needed. Obviously, that's not sensible if you have an enormous set of possible values.
In your case, I'd pick the third.
Create an array (int[]) big enough to hold one of every possible value (1-49).
Use a for loop to initialize each array value.
Use another for loop to swap each entry with a random index. If you swap a position with itself, that's fine.
Use the first few (6) elements of the array.
You can combine the second and third steps, if you want, but splitting them seems more readable and less prone to error to me.
I am messing around with this topic a day now. I tried to use Vectors, IVectors and Arrays.
Arrays can't have a higher dimension than 1 in WinRT, Vectors seem to be impossible to use in a public context. (If you can tell me how, please do!) and IVectors are interfaces, so you can't make an IVector of IVectors.
Is there any, and I mean any way to make a real two-dimensional array or an array of arrays like it was possible in C++/CLI?
(And yes, I know I can simulate 2 dimensions with a one dimensional array, but I don't really want to do that.)
I used this workaround for this issue. Not pretty, but functional.
Rather than creating a Vector of Vectors create a Vector of Objects. Then use safe_cast to access Vectors within your containing Vector.
Platform::Collections::Vector<Object^ >^ lArrayWithinArray = ref new Platform::Collections::Vector<Object^ >();
//Prepare some test data
Platform::Collections::Vector<Platform::String^>^ lStrings = ref new Platform::Collections::Vector<Platform::String^>();
lStrings->Append(L"One");
lStrings->Append(L"Two");
lStrings->Append(L"Three");
lStrings->Append(L"Four");
lStrings->Append(L"Five");
//We will use this to show that it works
Platform::String^ lOutput = L"";
//Populate the containing Vector
for(int i = 0; i < 5; i++)
{
lArrayWithinArray->Append(ref new Platform::Collections::Vector<String^>());
//Populate each Vector within the containing Vector with test data
for(int j = 0; j < 5; j++)
{
//Use safe_cast to cast the Object as a Vector
safe_cast<Platform::Collections::Vector<Platform::String^>^>(lArrayWithinArray->GetAt(i))->Append(lStrings->GetAt(j));
}
}
//Test loop to verify our content
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
lOutput += lStrings->GetAt(i) + L":" + safe_cast<Platform::Collections::Vector<Platform::String^>^>(lArrayWithinArray->GetAt(i))->GetAt(j) + ", ";
}
}