Application.Transpose 2D array assigment - arrays

For example I have 2d array
tKey(0, 0) = 1
tKey(1, 0) = 2
tKey(2, 0) = 3
So
testArray = Application.Transpose(tKey)
assign like 1D array
testArray(1) = 1
testArray(2) = 2
testArray(3) = 3
Instead of expected
testArray(1,1) = 1
testArray(1,2) = 2
testArray(1,3) = 3
But if tKey
tKey(0, 0) = 1
tKey(1, 0) = 2
tKey(2, 0) = 3
tKey(0, 1) = 1
tKey(1, 1) = 2
tKey(2, 1) = 3
then
testArray = Application.Transpose(tKey)
assign like 2D array, like expected
testArray(1,1) = 1
testArray(1,2) = 2
testArray(1,3) = 3
testArray(2,1) = 1
testArray(2,2) = 2
testArray(2,3) = 3
test code
Sub testss()
Dim tKey(0 To 2, 0 To 0) As Variant
tKey(0, 0) = 1
tKey(1, 0) = 2
tKey(2, 0) = 3
testArray1 = Application.Transpose(tKey) ' wrong assignment
Dim tKey2(0 To 2, 0 To 1) As Variant
tKey2(0, 0) = 1
tKey2(1, 0) = 2
tKey2(2, 0) = 3
tKey2(0, 1) = 1
tKey2(1, 1) = 2
tKey2(2, 1) = 3
testArray2 = Application.Transpose(tKey2) ' good assignment
Stop
End Sub
Is that a bug? How to assign correct one value to 2d array using Application.Transpose?

Related

Getting memory Address like values of my array when printing (C programming)

I am trying to create 8x8 array, but when I am printing that array, after [7][7] Element I am not getting the exact values that I assigned while creating the array.
My array is a follows
#include <stdio.h>
int main() {
int puzzle[8][8] = {
// 0 1 2 3 4 5 6 7 8
{0,0,2,0,4,0,5,9,3},//0
{7,0,0,3,1,0,4,0,8},//1
{4,0,0,8,0,5,1,0,2},//2
{8,3,0,2,0,0,0,0,0},//3
{0,9,6,0,0,0,3,5,0},//4
{0,0,0,0,0,4,0,8,6},//5
{3,0,1,5,0,9,0,0,7},//6
{6,0,5,0,2,8,0,0,1},//7
{0,4,0,0,7,0,6,0,0} //8
};
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
printf("\n puzzle[%d][%d] = %d",i,j,puzzle[i][j]);
}
}
return 0;
}
and I am getting output as
puzzle[0][0] = 0
puzzle[0][1] = 0
puzzle[0][2] = 2
puzzle[0][3] = 0
puzzle[0][4] = 4
puzzle[0][5] = 0
puzzle[0][6] = 5
puzzle[0][7] = 9
puzzle[0][8] = 7
puzzle[1][0] = 7
puzzle[1][1] = 0
puzzle[1][2] = 0
puzzle[1][3] = 3
puzzle[1][4] = 1
puzzle[1][5] = 0
puzzle[1][6] = 4
puzzle[1][7] = 0
puzzle[1][8] = 4
puzzle[2][0] = 4
puzzle[2][1] = 0
puzzle[2][2] = 0
puzzle[2][3] = 8
puzzle[2][4] = 0
puzzle[2][5] = 5
puzzle[2][6] = 1
puzzle[2][7] = 0
puzzle[2][8] = 8
puzzle[3][0] = 8
puzzle[3][1] = 3
puzzle[3][2] = 0
puzzle[3][3] = 2
puzzle[3][4] = 0
puzzle[3][5] = 0
puzzle[3][6] = 0
puzzle[3][7] = 0
puzzle[3][8] = 0
puzzle[4][0] = 0
puzzle[4][1] = 9
puzzle[4][2] = 6
puzzle[4][3] = 0
puzzle[4][4] = 0
puzzle[4][5] = 0
puzzle[4][6] = 3
puzzle[4][7] = 5
puzzle[4][8] = 0
puzzle[5][0] = 0
puzzle[5][1] = 0
puzzle[5][2] = 0
puzzle[5][3] = 0
puzzle[5][4] = 0
puzzle[5][5] = 4
puzzle[5][6] = 0
puzzle[5][7] = 8
puzzle[5][8] = 3
puzzle[6][0] = 3
puzzle[6][1] = 0
puzzle[6][2] = 1
puzzle[6][3] = 5
puzzle[6][4] = 0
puzzle[6][5] = 9
puzzle[6][6] = 0
puzzle[6][7] = 0
puzzle[6][8] = 6
puzzle[7][0] = 6
puzzle[7][1] = 0
puzzle[7][2] = 5
puzzle[7][3] = 0
puzzle[7][4] = 2
puzzle[7][5] = 8
puzzle[7][6] = 0
puzzle[7][7] = 0
puzzle[7][8] = -445142480
puzzle[8][0] = -445142480
puzzle[8][1] = 32765
puzzle[8][2] = 480800256
puzzle[8][3] = -129200335
puzzle[8][4] = 0
puzzle[8][5] = 0
puzzle[8][6] = -2108403533
puzzle[8][7] = 32522
puzzle[8][8] = -2106304992
As you can see I am not getting exact values that I assigned to [7][8]th position.
The output I am getting looks like address or ids. I am not getting why it is happening, is it ide problem or is there any mistake in my code?
As you already seem to understand, array elements are indexed beginning at position zero. So when you define an array of size n arr[n], you can only hold n elements(0 to n - 1) in this array. Same applies for multi-dimensional arrays.
In your case you have only defined an array of size 8x8 which can hold only 64 elements. But you are trying to assign 9x9 81 elements to your array. Thus, only indices puzzle[0][0] to puzzle[7][7] are accessible.
The compiler shall issue a message for this initialization
int puzzle[8][8] = {
// 0 1 2 3 4 5 6 7 8
{0,0,2,0,4,0,5,9,3},//0
{7,0,0,3,1,0,4,0,8},//1
{4,0,0,8,0,5,1,0,2},//2
{8,3,0,2,0,0,0,0,0},//3
{0,9,6,0,0,0,3,5,0},//4
{0,0,0,0,0,4,0,8,6},//5
{3,0,1,5,0,9,0,0,7},//6
{6,0,5,0,2,8,0,0,1},//7
{0,4,0,0,7,0,6,0,0} //8
};
because elements of the array are arrays with 8 elements bur you are supplying 9 initializers for each element.
And if an array has N elements then the valid range of indices is [0, N).

Using a sub to call an array within a function

I have a little problem with my sub. This sub is calling to different functions, by using the sub's data. The first function finds one is finding the amount of unique values and the second function finds these values. However, the first function works fine because its output is a scalar value. However, the second function's output is an array. I have tried to search for a solution, but so far I have not succeeded. I have a theory that the issue has something to do with the ByRef A() As Integer. I have written the codes below, both for the sub and the second function.
Sub Test()
Dim A() As Integer
Dim n As Integer
Dim BB As Integer
n = 10
ReDim A(n, 2) '5 unikke
A(1, 1) = 1
A(2, 1) = 7
A(3, 1) = 2
A(4, 1) = 6
A(5, 1) = 3
A(6, 1) = 5
A(7, 1) = 1
A(8, 1) = 1
A(9, 1) = 1
A(10, 1) = 4
A(1, 2) = 1
A(2, 2) = 7
A(3, 2) = 2
A(4, 2) = 6
A(5, 2) = 3
A(6, 2) = 5
A(7, 2) = 1
A(8, 2) = 1
A(9, 2) = 1
A(10, 2) = 4
BB = Unikke(A) 'Unikke is the second function that provides the amount of unique values
Dim FF() As Integer
ReDim FF(BB, 1)
FF = HvilkeUnikke(A) 'the second function, which has the output of an array a.k.a the problem
End Sub
This is the function:
Public Function HvilkeUnikke(ByRef A() As Integer) As Integer
Dim L() As Integer
Dim B As Integer
Dim i As Integer
Dim i2 As Integer
Dim A2() As String
Dim BB As Integer
Dim C() As Integer
BB = Unikke(A)
ReDim C(UBound(A), 2)
ReDim A2(BB, 1)
ReDim L(BB, 1)
For i = 1 To UBound(A)
C(i, 1) = A(i, 1)
C(i, 2) = A(i, 2)
Next
For i = 1 To UBound(C)
B = 0
For i2 = 1 To UBound(C)
If C(i, 1) = C(i2, 2) Then
B = B + 1
If B > 1 Then
C(i2, 2) = 0
End If
End If
Next i2
Next i
B = 0
For i2 = 1 To UBound(C)
If C(i2, 2) > 0 Then
B = B + 1
L(B, 1) = C(i2, 2)
End If
Next i2
HvilkeUnikke = L
End Function
The results are as expected, but they should be in a variable inside my sub.
(The solution)
Sub test()
Dim FF() As Integer
Dim i As Integer
Dim A() As Integer
Dim n As Integer
Dim BB As Integer
n = 10
ReDim A(n, 2) '7 unikke
A(1, 1) = 1
A(2, 1) = 7
A(3, 1) = 2
A(4, 1) = 6
A(5, 1) = 3
A(6, 1) = 5
A(7, 1) = 1
A(8, 1) = 1
A(9, 1) = 1
A(10, 1) = 4
A(1, 2) = 1
A(2, 2) = 7
A(3, 2) = 2
A(4, 2) = 6
A(5, 2) = 3
A(6, 2) = 5
A(7, 2) = 1
A(8, 2) = 1
A(9, 2) = 1
A(10, 2) = 4
BB = Unikke(A)
ReDim FF(BB)
FF = HvilkeUnikke(A)
'Testing on the worksheet
For i = 1 To BB
Cells(i, 1) = FF(i)
Next
End Sub
And the function
Public Function HvilkeUnikke(ByRef A() As Integer) As Integer()
Dim L() As Integer
Dim B As Integer
Dim i As Integer
Dim i2 As Integer
Dim A2() As String
Dim BB As Integer
Dim C() As Integer
BB = Unikke(A)
ReDim C(UBound(A), 2)
ReDim A2(BB, 1)
ReDim L(BB)
For i = 1 To UBound(A)
C(i, 1) = A(i, 1)
C(i, 2) = A(i, 2)
Next
For i = 1 To UBound(C)
B = 0
For i2 = 1 To UBound(C)
If C(i, 1) = C(i2, 2) Then
B = B + 1
If B > 1 Then
C(i2, 2) = 0
End If
End If
Next i2
Next i
B = 0
For i2 = 1 To UBound(C)
If C(i2, 2) > 0 Then
B = B + 1
L(B) = C(i2, 2)
End If
Next i2
HvilkeUnikke = L
End Function

Finding number(s) that is(are) repeated consecutively most often

Given this array for example:
a = [1 2 2 2 1 3 2 1 4 4 4 5 1]
I want to find a way to check which numbers are repeated consecutively most often. In this example, the output should be [2 4] since both 2 and 4 are repeated three times consecutively.
Another example:
a = [1 1 2 3 1 1 5]
This should return [1 1] because there are separate instances of 1 being repeated twice.
This is my simple code. I know there is a better way to do this:
function val=longrun(a)
b = a(:)';
b = [b, max(b)+1];
val = [];
sum = 1;
max_occ = 0;
for i = 1:max(size(b))
q = b(i);
for j = i:size(b,2)
if (q == b(j))
sum = sum + 1;
else
if (sum > max_occ)
max_occ = sum;
val = [];
val = [val, q];
elseif (max_occ == sum)
val = [val, q];
end
sum = 1;
break;
end
end
end
if (size(a,2) == 1)
val = val'
end
end
Here's a vectorized way:
a = [1 2 2 2 1 3 2 1 4 4 4 5 1]; % input data
t = cumsum([true logical(diff(a))]); % assign a label to each run of equal values
[~, n, z] = mode(t); % maximum run length and corresponding labels
result = a(ismember(t,z{1})); % build result with repeated values
result = result(1:n:end); % remove repetitions
One solution could be:
%Dummy data
a = [1 2 2 2 1 3 2 1 4 4 4 5 5]
%Preallocation
x = ones(1,numel(a));
%Loop
for ii = 2:numel(a)
if a(ii-1) == a(ii)
x(ii) = x(ii-1)+1;
end
end
%Get the result
a(find(x==max(x)))
With a simple for loop.
The goal here is to increase the value of x if the previous value in the vector a is identical.
Or you could also vectorized the process:
x = a(find(a-circshift(a,1,2)==0)); %compare a with a + a shift of 1 and get only the repeated element.
u = unique(x); %get the unique value of x
h = histc(x,u);
res = u(h==max(h)) %get the result

Do I need an Array a List?

I am making a program for a game. It takes the level of the class (of which you can have 8 simultaneous different ones of different level). Each class has different values for one Dim I have known as Defense, it has 6 different possibly levels. Each of those 6 levels corresponds with a value based on the level of each class.
I'm almost certain I need an array or a list, I've seen some examples here but I haven't seen something that quite fits my case.
Do I need to have an array within an array? Is that even possible?
I apologize if I'm horrible at explaining what I'm trying to accomplish.
An example would be if the person chose Strong Hero and then chose Level 4. Following that they'd pick Fast Hero and chose Level 5. I'm trying to get it to look at what the value for 4 would be for Strong Hero and the value for Fast Hero, find those and then add them into Def at the end.
Thank you in advance for anyone who is willing to put up with my ignorance!
Dim Def As Integer = 0
'Declares a single-dimension array of 10 values
Dim DefLvl(9) As Integer
Dim DefType As String = ""
Dim DefLvl1 As Integer = 0
Dim DefLvl2 As Integer = 0
Dim DefLvl3 As Integer = 0
Dim DefLvl4 As Integer = 0
Dim DefLvl5 As Integer = 0
Dim DefLvl6 As Integer = 0
Dim DefLvl7 As Integer = 0
Dim DefLvl8 As Integer = 0
Dim DefLvl9 As Integer = 0
Dim DefLvl10 As Integer = 0
'This sets the cmblevel.text = to an integer to be multiplied later
'Level 1
If cmbLevel1.Text = "1" Then
Level1 = 1
ElseIf cmbLevel1.Text = "2" Then
Level1 = 2
ElseIf cmbLevel1.Text = "3" Then
Level1 = 3
ElseIf cmbLevel1.Text = "4" Then
Level1 = 4
ElseIf cmbLevel1.Text = "5" Then
Level1 = 5
ElseIf cmbLevel1.Text = "6" Then
Level1 = 6
ElseIf cmbLevel1.Text = "7" Then
Level1 = 7
ElseIf cmbLevel1.Text = "8" Then
Level1 = 8
ElseIf cmbLevel1.Text = "9" Then
Level1 = 9
ElseIf cmbLevel1.Text = "10" Then
Level1 = 10
End If
'
If cmbClass1.Text = "Strong Hero" Then
HD1 = "8"
DefType = "Avg+"
DefLvl1 = 1
DefLvl2 = 2
DefLvl3 = 2
DefLvl4 = 3
DefLvl5 = 3
DefLvl6 = 3
DefLvl7 = 4
DefLvl8 = 4
DefLvl9 = 5
DefLvl10 = 5
End If
'This formula will calculate Defense for each class and level
If DefType = "Poor" Then
DefLvl1 = 0
DefLvl2 = 1
DefLvl3 = 1
DefLvl4 = 1
DefLvl5 = 2
DefLvl6 = 2
DefLvl7 = 2
DefLvl8 = 3
DefLvl9 = 3
DefLvl10 = 3
ElseIf DefType = "Avg" Then
DefLvl1 = 1
DefLvl2 = 1
DefLvl3 = 2
DefLvl4 = 2
DefLvl5 = 3
DefLvl6 = 3
DefLvl7 = 4
DefLvl8 = 4
DefLvl9 = 5
DefLvl10 = 5
ElseIf DefType = "Avg+" Then
DefLvl1 = 1
DefLvl2 = 2
DefLvl3 = 2
DefLvl4 = 3
DefLvl5 = 3
DefLvl6 = 3
DefLvl7 = 4
DefLvl8 = 4
DefLvl9 = 5
DefLvl10 = 5
ElseIf DefType = "Good" Then
DefLvl1 = 1
DefLvl2 = 2
DefLvl3 = 2
DefLvl4 = 3
DefLvl5 = 4
DefLvl6 = 4
DefLvl7 = 5
DefLvl8 = 6
DefLvl9 = 6
DefLvl10 = 7
ElseIf DefType = "Good+" Then
DefLvl1 = 1
DefLvl2 = 2
DefLvl3 = 2
DefLvl4 = 3
DefLvl5 = 4
DefLvl6 = 4
DefLvl7 = 5
DefLvl8 = 6
DefLvl9 = 7
DefLvl10 = 7
ElseIf DefType = "Great" Then
DefLvl1 = 3
DefLvl2 = 4
DefLvl3 = 4
DefLvl4 = 5
DefLvl5 = 5
DefLvl6 = 6
DefLvl7 = 6
DefLvl8 = 7
DefLvl9 = 7
DefLvl10 = 8
End If
'This adds up to all 8 classes' DefLvls
Def = DefLvl1 + DefLvl2 + DefLvl3 + DefLvl4 + DefLvl5 + DefLvl6 + DefLvl7 + DefLvl8 + dexM

Large array indexes scala

(Language: scala)
I have a problem where I want to iterate over 1 million numbers, but for some reason I get an arrayindexOutofbounds exception. The function I am using works perfectly for 100 000 numbers, but I get the exception if I add a zero.
There cannot be a problem with the array size, because I have built a sort of flex-array, where the array is about 1000 elements and each element consists of a list of elements.
So the problem looks something like this:
for (x <- 1 to 1000000) {
// Do a thing
}
Can for loops only handle a certain number of elements?
I have tried running the program with the "extra-space-flag"
I include the whole code below for reference, in case it makes a difference
object Problem14 {
class FlexArray (n : Int){
var array = new Array[List[Tuple2[Int, Int]]](n)
val size = n
for(x <- 0 until size) {
array(x) = List()
}
def insert (n : Int, value : Int) {
if (find(n) != -1) {
val i = n % size
array(i) = (n, value) :: array(i)
}
}
def read (i : Int) : List[Tuple2[Int, Int]] = {
(array(i))
}
def findAux (list : List[Tuple2[Int, Int]], n : Int) : Int = {
if (list == Nil) {
-1
} else {
val (num, value) = list.head
if (n == num) {
value
} else {
findAux(list.tail, n)
}
}
}
def find (n : Int) : Int = {
val i = n % size
findAux(array(i), n)
}
}
var accArray = new FlexArray(10000)
// denna funktion bör kallas med 1 som andra argument
def chainLength (n : Int, acc : Int) : Int = {
if (n == 1)
acc
else {
val value = accArray.find(n)
if (value != -1)
acc + value
else if (n % 2 == 0)
chainLength(n/2, acc+1)
else
chainLength(3*n+1, acc+1)
}
}
def main(args: Array[String]) {
var max = 0
var maxnum = 0
for (x <- 1 to 1000000) {
var value = chainLength(x, 1)
accArray.insert(x, value)
if (max < value) {
max = value
maxnum = x
}
println(maxnum + ": " + max)
}
}
The problem isn't with array indexes, scala functions just like java and will go to Int.MaxValue 2,147,483,647. The problem is with this line of in your chainLength function:
chainLength(3 * n + 1, acc + 1)
Since chainLength is recursive on itself switching between n/2 and n * 3 + 1 you run into this with numbers larger than 113383. It leads to integer overflow so you end up with a negative value passed to the array index which throws your error. I'm guessing you'll need to add some integer overflow handling to your function.
Full output of calls to chainLength in chainLength(113383, 1) (it ends with overflow at the bottom):
Starting at 113383
3 * 113383 + 1 = 340150
340150 / 2 = 170075
3 * 170075 + 1 = 510226
510226 / 2 = 255113
3 * 255113 + 1 = 765340
765340 / 2 = 382670
382670 / 2 = 191335
3 * 191335 + 1 = 574006
574006 / 2 = 287003
3 * 287003 + 1 = 861010
861010 / 2 = 430505
3 * 430505 + 1 = 1291516
1291516 / 2 = 645758
645758 / 2 = 322879
3 * 322879 + 1 = 968638
968638 / 2 = 484319
3 * 484319 + 1 = 1452958
1452958 / 2 = 726479
3 * 726479 + 1 = 2179438
2179438 / 2 = 1089719
3 * 1089719 + 1 = 3269158
3269158 / 2 = 1634579
3 * 1634579 + 1 = 4903738
4903738 / 2 = 2451869
3 * 2451869 + 1 = 7355608
7355608 / 2 = 3677804
3677804 / 2 = 1838902
1838902 / 2 = 919451
3 * 919451 + 1 = 2758354
2758354 / 2 = 1379177
3 * 1379177 + 1 = 4137532
4137532 / 2 = 2068766
2068766 / 2 = 1034383
3 * 1034383 + 1 = 3103150
3103150 / 2 = 1551575
3 * 1551575 + 1 = 4654726
4654726 / 2 = 2327363
3 * 2327363 + 1 = 6982090
6982090 / 2 = 3491045
3 * 3491045 + 1 = 10473136
10473136 / 2 = 5236568
5236568 / 2 = 2618284
2618284 / 2 = 1309142
1309142 / 2 = 654571
3 * 654571 + 1 = 1963714
1963714 / 2 = 981857
3 * 981857 + 1 = 2945572
2945572 / 2 = 1472786
1472786 / 2 = 736393
3 * 736393 + 1 = 2209180
2209180 / 2 = 1104590
1104590 / 2 = 552295
3 * 552295 + 1 = 1656886
1656886 / 2 = 828443
3 * 828443 + 1 = 2485330
2485330 / 2 = 1242665
3 * 1242665 + 1 = 3727996
3727996 / 2 = 1863998
1863998 / 2 = 931999
3 * 931999 + 1 = 2795998
2795998 / 2 = 1397999
3 * 1397999 + 1 = 4193998
4193998 / 2 = 2096999
3 * 2096999 + 1 = 6290998
6290998 / 2 = 3145499
3 * 3145499 + 1 = 9436498
9436498 / 2 = 4718249
3 * 4718249 + 1 = 14154748
14154748 / 2 = 7077374
7077374 / 2 = 3538687
3 * 3538687 + 1 = 10616062
10616062 / 2 = 5308031
3 * 5308031 + 1 = 15924094
15924094 / 2 = 7962047
3 * 7962047 + 1 = 23886142
23886142 / 2 = 11943071
3 * 11943071 + 1 = 35829214
35829214 / 2 = 17914607
3 * 17914607 + 1 = 53743822
53743822 / 2 = 26871911
3 * 26871911 + 1 = 80615734
80615734 / 2 = 40307867
3 * 40307867 + 1 = 120923602
120923602 / 2 = 60461801
3 * 60461801 + 1 = 181385404
181385404 / 2 = 90692702
90692702 / 2 = 45346351
3 * 45346351 + 1 = 136039054
136039054 / 2 = 68019527
3 * 68019527 + 1 = 204058582
204058582 / 2 = 102029291
3 * 102029291 + 1 = 306087874
306087874 / 2 = 153043937
3 * 153043937 + 1 = 459131812
459131812 / 2 = 229565906
229565906 / 2 = 114782953
3 * 114782953 + 1 = 344348860
344348860 / 2 = 172174430
172174430 / 2 = 86087215
3 * 86087215 + 1 = 258261646
258261646 / 2 = 129130823
3 * 129130823 + 1 = 387392470
387392470 / 2 = 193696235
3 * 193696235 + 1 = 581088706
581088706 / 2 = 290544353
3 * 290544353 + 1 = 871633060
871633060 / 2 = 435816530
435816530 / 2 = 217908265
3 * 217908265 + 1 = 653724796
653724796 / 2 = 326862398
326862398 / 2 = 163431199
3 * 163431199 + 1 = 490293598
490293598 / 2 = 245146799
3 * 245146799 + 1 = 735440398
735440398 / 2 = 367720199
3 * 367720199 + 1 = 1103160598
1103160598 / 2 = 551580299
3 * 551580299 + 1 = 1654740898
1654740898 / 2 = 827370449
3 * 827370449 + 1 = -1812855948

Resources