Last cell in array that modified - arrays

Is there any way to find out which cell in an array is the last cell that modified? (e.g. change its value)
In any compile-based languages.
For example assume we defined array1 with n cells. Now value of array1[2] changed to 1, after that value of array1[7] will change to 1, I want a solution to find out the 7th cell as last cell that modified.

Just don't access the array directly, write to it in a function/method and keep track as it changes. In C# there are properties/indexers which wrap the call X = 2 in a method for you.

Neither of these seem like particularly great solutions, but they will both accomplish what you want in C++:
int indexToChange, lastIndexChanged;
int array1 [10];
indexToChange = 2;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
indexToChange = 7;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
Then at any point array1[lastIndexChanged] would be the most recently updated. But this is annoying since it requires 3 commands for every array update. Instead, you could wrap it into a function:
void changeArray(int array[], int length, int indexToChange, int newVal){
array[indexToChange] = newVal;
lastIndexChanged=indexToChange;
}
Which would then require all your array updates to look like this (assuming lastIndexChanged was declared globally and array1 has size 10):
changeArray(array1, 10, 2, 1);
changeArray(array1, 10, 7, 1);
Then, to update the most recently changed element to 0:
changeArray(array1, 10, lastIndexChanged, 0);
However, both of these examples will only work for a single array in a program, which does not seem particularly useful to me.
Only other idea I have involves creating an array of tuples (or something similar) and using the 2nd element of the tuple as one of the following:
A bool flag indicating if the element was the last one changed
An int flag indicating the "age" of the element
However, both of those methods require accessing every element of the array for every single array update.
For (1), with each update you would have to make sure to find the previously last updated element and set it's flag to false as you set the flag of the element you were updating to true.
For (2), with each array update you would increment the "age" flag of every element, but set the "age" of the element you updated to 0. This has the advantage of enabling you to also find the nth last updated element.
In either case, if your original array is an array of ints, you may be able to implement this with a 2-dimensional array with two rows. So, if array1 is size n, you would have int array1[2][n], giving you an array like this (for n=4, assuming all cells initialized to 0):
[0][0][0][0]
[0][0][0][0]
where you would use the top row for your values and your bottom row for flags. Thus, an array update would look something like this:
For "bool" flags (just using 0 and 1 to simulate a bool value):
array1[0][2] = 1; //set value of element 2 to 1
array1[1][getLastUpdated()]=false; //resetting the previous "last updated" flag
array1[1][2] = true;
For "age" flags:
array1[0][2] = 1; //set value of element 2 to 1
for (int i=0; i<array1Length; i++){
array1[1][i]++; //increment ages of every element
}
array1[1][2] = 0; //reset age of the element you just updated
Then, to find the most recently updated element, you would search for array[1][n] to be true or 0, respectively.

Related

Better way for making an array for Connect 4?

I am having more trouble with these array as it seems I have limited understanding on building and utilizing an array. How should I pass a specific cell to an array? In this example I am trying to pass the chip value of a specific column that occupies row 8. How would you manipulate the value of a 2D array cell based on the location of the cell as shown below?
Below is the link to a compiler I have been using online, I also used a minimalist portion of the code to illustrate my overall goal. I am trying to push the
https://onlinegdb.com/mB1kiOtOBM
int row;
int col;
int array [row][col];
int const chip = 1;
while(left == 1)
{
row = 8;
for(int col = 8; 8 > col > 0; col ++)
{
chip = array [row][col];
}
}
How should I pass a specific cell to an array? In this example I am trying to pass the chip value of a specific column that occupies row 8.
If I'm understanding your question correctly, in this case the verb you want is assign instead of pass.
We usually say pass a thing to another when it comes to function calls. Say you define your function foo(int x) and you call it foo(2). You're passing the value 2 to the function.
So if you're trying to change one value in the array, you want something like array[7][0] = chip. This sets the first cell of 8th row to whatever value that chip holds. Note that arrays are 0-indexed in C.

How to re-arrange elements of Array after Deleting

Recently I was reading a Programming book and found this question:
I have an array :
array = [2,3,6,7,8,9,33,22];
Now, Suppose I have deleted the element at 4th position i.e. 8 .
Now I have to rearrange the array as:
Newarray = [2,3,6,7,9,33,22];
How Can I do this. And I have to also minimize the complexity.
Edit I have no choice to make another copy of it.I have to only modify it.
You can "remove" a value from an array by simply copy over the element by the next elements, that's easy to do with memmove:
int array[8] = {2,3,6,7,8,9,33,22};
memmove(&array[4], &array[5], sizeof(int) * 3);
The resulting array will be {2,3,6,7,9,33,22,22}.
And from that you can see the big problem with attempting to "remove" an element from a compile-time array: You can't!
You can overwrite the element, but the size of the array is fixed at time of compilation and can't actually be changed at run-time.
One common solution is to keep track of the actual number of valid elements in the array manually, and make sure you update that size as you add or remove elements. Either that or set unused elements to a value that's not going to be used otherwise (for example if your array can only contain positive numbers, then you could set unused elements to -1 and check for that).
If you don't want to use a library function (why not?) then loop and set e.g.
array[4] = array[5];
array[5] = array[6];
and so on.
Do this, just use these two functions and it will work fine
index=4;//You wanted to delete it from the array.
memcpy(newarray,array,sizeof(array));
memmove(&newarray[index], &newarray[index + 1], sizeof(newarray)-1);
now the newarray contains your exact replica without the character that you wished to remove
You can simply displace each element from the delIdx(deletion index) one step forward.
for(int i=delIdx; i<(arr_size-1);i++)
{
arr[i]= arr[i+1];
}
If required you can either set the last element to a non-attainable value or decrease the size of the array.

AS-3 - How to get min/max INDEX of Array

This is NOT about min/max values!
I use Array with all kinds of indices like 0, 1, 2, 1000, -1, -100. Yes, negative indices (I like this feature and I'm thankful for it.). The problem is I need them to be negative otherwise I would have to move all items. Now I wonder how can I retrieve the min index like -100. Most elements between min and max index would be undefined.
var a: Array = new Array();
a[-100] = "hello";
a.forEach(...); // starts with 0
Is there a simple way to get min index ?
No, there is no simple way to get the minimum index of the negative keys. You would have to loop through all the keys, convert them to numbers, and get the lowest.
The reason for that is that you are using the array both as an array and as an object. The array only has items with positive indexes, when you assign anything to it using a negative index, it's instead added as a property to the array object, not as an item in the array.
The forEach method loops through the items in the array, so it will only access the positive indexes. The data that you added using negative indexes are not array items, they are properties of the array object. So, it's not a bug that the forEach method starts from index 0.
If you want to loop through everything that you stored in the array, you can loop through its properties, then you will get both the object properties and array items:
for (i in a) {
// Here i is the key, and a[i] is the value
}
It's true that you can have negative indices in AS3 Arrays. The problem (and it's quite a serious one) is that none of the Array methods recognise the negative indices - they only recognise whole numbers.
For example:
var my_array:Array = new Array();
var my_array[-100] = "hello";
trace(my_array[-100]) // hello
trace(my_array.length) // 0
trace(my_array) // [empty]
So standard iteration through the array can only be done with absolute references to the index. Or by treating the array as an Object when looping:
var lowest:int = -100000 /* some deep number */
for(var i in a){
if(int(i) < lowest){
lowest = int(i);
}
}

How can i splice current index in a foreach?

I have this foreach loop to check for collision and i want platform(movieclip) to be removed in case of collision. So far i've come up with this:
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
//platformsCloud.splice(platformCloud);
}
What this is doing is, removing the movieclip (ok so far so good) but without the splice, when the loop runs again through the array it is still there. So with the splice that is commented out there's 1 little problem, it removes all the movieclips from the array, apprently.
How can i splice only the current index that is being checked?
.splice() accepts a start index and an amount of items to remove, not the object you want to remove from the array.
Parameters
startIndex:int — An integer that specifies the index of the element in the array where the insertion or deletion begins. You can use a negative integer to specify a position relative to the end of the array (for example, -1 is the last element of the array).
deleteCount:uint — An integer that specifies the number of elements to be deleted. This number includes the element specified in the startIndex parameter. If you do not specify a value for the deleteCount parameter, the method deletes all of the values from the startIndex element to the last element in the array. If the value is 0, no elements are deleted.
You want to do this:
var index:int = platformsCloud.indexOf(platformCloud);
platformsCloud.splice(index, 1);
Why not just create a new array of the items to keep? Use Array.push to add new items. This may actually be more efficient than modifying the existing array. It also doesn't require keeping track of indices (which are required to use Array.splice).
Example code:
var keptPlatforms = [];
// do stuff
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
} else {
keptPlatforms.push(platformCloud);
}
// later, after this cycle, use the new Array
platformClouds = keptPlatforms;
Now, the reason platformsCloud.splice(platformCloud) removes all items is because the first argument is coerced to an integer so it is equivalent to platformsCloud.splice(0) which says "remove the 0th-indexed item to the end of the array". And, this does indeed clear the array.
To use Array.splice, you'd have to do something like:
// inside a loop this approach may lead to O(n^2) performance
var i = platformClouds.indexOf(platformCloud);
if (i >= 0) {
platformClouds.splice(i, 1); // remove 1 item at the i'th index
}

How can add element to array in AS3?

How can add element to array in ActionScript3
If i have an array:
var myArray:Array;
How can add element to this array "myArray", something like this:
myArray[] = value;
My second question is: How can compare if variable value exist in array element value?
Something like in_array function in php
1. All of these are different ways of adding item to array.
someArray.push(someValue); : add last item
someArray.unshift(someValue); : add first item
someArray[index] = someValue; : set item somewhere
someArray.splice(index, 0, someValue); : insert item somewhere
2. Checking if a value is present in array.
if (someArray.indexOf(someValue) == -1) { /*value is not present*/ }
Refer to ActionScript language reference on Adobe livedocs.
To answer both your questions here, you can add to an array by direct access or by the push() method, like so:
myArray[7] = something;
or
myArray.push(something);
Also as Nox noted, you can use the splice method as well to add in elements. This method is used to delete N amount of elements at a specific index, but you can also simultaneously inject one or more elements at the same index.
For your second question about how to check values or compare them in an array, here is one method:
var i:int = 0;
for(i; i < myArray.length; ++i){
if(myArray[i] == 10){
trace('found');
}
}

Resources