I don't have a lot of experience with smilebasic and I am trying to find the length of a single array within a 2x2 matrix, or thr length of the row. Technically, becaude the matrix im using is a square, i could just use the length of the matrix, but I want the program to work on non-square matrices as well. I tried len(mat[0]) assuming it would work but it didnt. Everything ive seen usually used a variable to set up the matrix deminsions and used that same variable again later, but I reading in the array, so that won't work. I'm at a loss for what to do which really sucks because I'm pretty sure my idea for my program will work. So, does anybody know a way to find the length of a row in a 2d array? Ot any work arounds?
Length of arrays in matrices
Root / Programming Questions / [.]
ENIGMACreated:
Here is the code:
DEF SIZE A OUT X,Y VAR T=A[1,0],B[0],L=LEN(A) A[1,0]=1<<31 COPY B,A FOR Y=0 TO L-1 IF B[Y]==A[1,0] THEN BREAK NEXT A[1,0]=T X=L/Y ENDUsage:
DIM MYARRAY[12,8] SIZE MYARRAY OUT X,Y ?"DIM MYARRAY[";X;",";Y;"]"How it works: When you have a two dimensional array, copying it to a new array will convert it into a one dimensional array. So we can change the value in [1,0] and see what index it gets mapped to. That will give us the size of one dimension, we can easily calculate the other. Note: The smallest array that this works on is [2,1]. The time complexity of this algorithm is O(X*Y+X), copying the whole array is linear, then we have to loop through Y once more, to get O(X*(Y+1)). I believe this is the fastest algorithm to do this. I'd love to see if someone can do better.
Non-square arrays are not possible in SmileBASIC, and there is no built in way to determine the dimensions of an array.
However I would recommend checking out these posts:
Getting the Dimension Lengths from 2D Arrays
Changing dimensions of an array
On a side note, welcome to the website! :)
Well, okay, although it's a fun programming challenge, you have no reason to calculate the dimensions of a two dimensional array since you'll need those dimensions to initialize it in the first place.
VAR W=8,H=12 VAR MYARRAY[H,W] ?"Width: ";W ?"Height: ";HTyping a "?" is the same thing as typing "PRINT", it's just easier to use Inside a print statement, we can use ";" to concatenate variables. Also note that VAR and DIM are the same.
'Make a function named SIZE that takes in 'a variable and outputs two variables X and Y DEF SIZE A OUT X,Y 'T is a temporary backup of A[1,0] 'B is an empty array 'L is width*height VAR T=A[1,0],B[0],L=LEN(A) 'Set A[1,0] to -2147483648 (bitshifting) A[1,0]=1<<31 'Copy the contents of A into B 'setting B's size to width*height COPY B,A 'Y will loop from 0 to width*height-1 FOR Y=0 TO L-1 'Check if we reach the -2147483648 inside B 'Once we do, break out of the loop 'Then Y will be the index of -2147483648 IF B[Y]==A[1,0] THEN BREAK NEXT 'Put back the value inside of A[1,0] A[1,0]=T 'width=width*height/height X=L/Y END
I am seeing now that i could have used the variables i used to instantiate the 2d array, since i did actually need those numbees to instantiate it. I was thinking I guess that i just was given an array with unknown values but the only way to instatiate the array it seems is to make it blank and add everything after. And i now get that i was incorrect in assuming len(mat) still gave number of rows because i guess its not an array of arrays so when this finds the size it returns the size of both dimensions.
I then see T is a temporary variable that for the first element of the second row because youre going to change that elements value, though I dont know why 2 other things seperated by commas are next to it exactly. I think i otherwise get it though
Which two other things separated by commas on which line?