#1✎ 3ENIGMAI 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?
Posted
#2✎ 86SimeonScholarReceived for knowing a great deal about programming topicsAchievementsAmazing PageHiddenAchievementsDrawingI like to draw!HobbiesHere 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
END
Usage: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.
Posted
Edited
by Simeon
#3✎ 90HackTheWorldsSecond YearMy account is over 2 years oldWebsiteSummer 2016 Contest ParticipantI participated in the SmileBASIC Source Summer 2016 Contest!Programming ContestReadingI like to read books!HobbiesNon-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 ArraysChanging dimensions of an array
Posted
Edited
by HackTheWorlds
#4✎ 90HackTheWorldsSecond YearMy account is over 2 years oldWebsiteSummer 2016 Contest ParticipantI participated in the SmileBASIC Source Summer 2016 Contest!Programming ContestReadingI like to read books!HobbiesOn a side note, welcome to the website! :)
Posted
#5✎ 3ENIGMAI don't really know a lot about this language including a bunch of syntax stuff so really i just dont know whats going on with that. I started to understand a bit but I dont fully get it. You'll basically have to explain it like i dont know the lanuage at all.
Posted
#6✎ 86SimeonScholarReceived for knowing a great deal about programming topicsAchievementsAmazing PageHiddenAchievementsDrawingI like to draw!HobbiesWell, 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: ";H
Typing 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.
Posted
Edited
by Simeon
#7✎ 86SimeonScholarReceived for knowing a great deal about programming topicsAchievementsAmazing PageHiddenAchievementsDrawingI like to draw!Hobbies
'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
Posted
Edited
by Simeon
#8✎ 3ENIGMAI 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
Posted
#9✎ 246spaceturtlesVideo GamesI like to play video games!HobbiesAvatar BlockI didn't change my avatar for 30 days.WebsiteIntermediate ProgrammerI can make programs, but I still have trouble here and there. Programming StrengthWhich two other things separated by commas on which line?
Posted
#10✎ 91niconiiVideo GamesI like to play video games!HobbiesExpert ProgrammerProgramming no longer gives me any trouble. Come to me for help, if you like!Programming StrengthDrawingI like to draw!HobbiesVAR T=A[1,0],B[0],L=LEN(A)
is the same as
VAR T=A[1,0]
VAR B[0]
VAR L=LEN(A)
Posted