Can I define an array index using a variable?

New to FreeBASIC? Post your questions here.
Post Reply
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Can I define an array index using a variable?

Post by RNBW »

If I have a program where I need to change the index of an array, can I use a variable in the array? For example,

DIM AS INTEGER a(10,5)

next time I use it I need to make it 35, 12

Can I set it up so that:

DIM AS INTEGER x,y
INPUT x
INPUT y
DIM AS INTEGER a(x,y)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can I define an array index using a variable?

Post by fxm »

Yes, you can.
In that case (bounds define by one variable at least), the array is automatically dynamic (resizable).

Code: Select all

DIM AS INTEGER x, y
INPUT x
INPUT y
DIM AS INTEGER a(x, y)
PRINT LBOUND(a, 1), UBOUND(a, 1)
PRINT LBOUND(a, 2), UBOUND(a, 2)

SLEEP
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Can I define an array index using a variable?

Post by RNBW »

I thought I should be able to, but my program crashed when I did it. I used a work-around declaring an index larger than I required, but then used INPUT to generate a figure and placed the variable in the array. This worked! The problem seemed to be with including the variable in the declaration.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can I define an array index using a variable?

Post by fxm »

Can you post an example of code which crashes?
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Can I define an array index using a variable?

Post by RNBW »

Hi fxm

I've found the problem.

I declared the index variable, put the variable in the array declaration and then input a value for the index. It quite rightly crashed. I've changed this to:
Declare index variable
Input a value for the variable
Put the variable in the Array declaration.

This works fine.
RNBW
Posts: 267
Joined: Apr 11, 2015 11:06
Location: UK

Re: Can I define an array index using a variable?

Post by RNBW »

fxm:

The working code is:

Code: Select all

'==================================
'InputBox_GridMultiCol_Dev4.bas
'Shows a grid of InputBoxes
'in 9 columns
'Includes Row and Column Headings
'Shows variable numbers of Rows
'----------------------------------
'Posted on 29 Mar 2016
'Last Change on 31 Mar 2016
'Using Lothar Schirm's 
'WinGui.bas Library
'==================================

#Include "WinGui.bi"

DIM AS INTEGER NumOfRows

INPUT "Number of Rows (Must NOT exceed 10) = "; NumOfRows
NumOfRows = NumOfRows+1

DIM SHARED AS HWND Window_Main, Button_Click, Static_Text
'Change suggested by Lother Schirm 30 Mar 2016 (ie to use array)
DIM AS HWND Edit_Text(1 TO NumOfRows, 1 TO 9)
DIM SHARED AS MSG msg
DIM AS STRING text
DIM AS INTEGER vPos, hPos, bWidth, bHeight, row, col

'Main window
SUB OpenWindow_Main()
	'Hauptfenster
   Window_Main = Window_New(100, 100, 950, 600, "Test Inputbox - Single Column Grid")	
END SUB

OpenWindow_Main()

'------------------------
'  SET UP THE GRID
'------------------------
vPos = 40: bHeight = 20
FOR row = 1 TO NumOfRows
   'hPos = 0 : bWidth = 0
   FOR col = 1 TO 9
      SELECT CASE col
      CASE 1 
         hPos = 10: bWidth = 65
      CASE 2 
         hPos = 75: bWidth = 380
      CASE 3 TO 8 
         hPos = col*65+(455-65*3) : bWidth = 65
      CASE 9 
         hPos = (10+65+380)+(col-3)*65 : bWidth = 75 
      END SELECT
      Edit_Text(row, col) = EditBox_New(hPos, vPos+bHeight*(row-1), bWidth+1, bHeight+1, "",, Window_Main)
   NEXT
NEXT

'--------------------------
' SET UP HEADINGS IN ROW 1
'--------------------------
text = "Col"
FOR row = 1 TO 1
   FOR col = 2 TO 9
      text = text + " " + STR(col-1)
      EditBox_SetText(Edit_Text(row,col), text) 
      text = "Col"
   NEXT
NEXT

'--------------------------
' SET UP HEADINGS IN COL 1
'--------------------------
text = "Row"
FOR row = 2 TO NumOfRows
   FOR col = 1 TO 1
      text = text + " " + STR(row-1)
      EditBox_SetText(Edit_Text(row,col), text) 
      text = "Row"
   NEXT
NEXT

'--------------------------------
'Close out code and end program
'--------------------------------
DO
	WaitEvent(msg)
LOOP UNTIL Window_Event_Close(Window_Main, msg)

END
jmgbsas
Posts: 35
Joined: Dec 26, 2020 16:03

Re: Can I define an array index using a variable?

Post by jmgbsas »

RNBW wrote:fxm:

The working code is:

Code: Select all

'==================================
'InputBox_GridMultiCol_Dev4.bas
'Shows a grid of InputBoxes
'in 9 columns
'Includes Row and Column Headings
'Shows variable numbers of Rows
'----------------------------------
'Posted on 29 Mar 2016
'Last Change on 31 Mar 2016
'Using Lothar Schirm's 
'WinGui.bas Library
'==================================

#Include "WinGui.bi"

DIM AS INTEGER NumOfRows

INPUT "Number of Rows (Must NOT exceed 10) = "; NumOfRows
NumOfRows = NumOfRows+1

DIM SHARED AS HWND Window_Main, Button_Click, Static_Text
'Change suggested by Lother Schirm 30 Mar 2016 (ie to use array)
DIM AS HWND Edit_Text(1 TO NumOfRows, 1 TO 9)
DIM SHARED AS MSG msg
DIM AS STRING text
DIM AS INTEGER vPos, hPos, bWidth, bHeight, row, col

'Main window
SUB OpenWindow_Main()
	'Hauptfenster
   Window_Main = Window_New(100, 100, 950, 600, "Test Inputbox - Single Column Grid")	
END SUB

OpenWindow_Main()

'------------------------
'  SET UP THE GRID
'------------------------
vPos = 40: bHeight = 20
FOR row = 1 TO NumOfRows
   'hPos = 0 : bWidth = 0
   FOR col = 1 TO 9
      SELECT CASE col
      CASE 1 
         hPos = 10: bWidth = 65
      CASE 2 
         hPos = 75: bWidth = 380
      CASE 3 TO 8 
         hPos = col*65+(455-65*3) : bWidth = 65
      CASE 9 
         hPos = (10+65+380)+(col-3)*65 : bWidth = 75 
      END SELECT
      Edit_Text(row, col) = EditBox_New(hPos, vPos+bHeight*(row-1), bWidth+1, bHeight+1, "",, Window_Main)
   NEXT
NEXT

'--------------------------
' SET UP HEADINGS IN ROW 1
'--------------------------
text = "Col"
FOR row = 1 TO 1
   FOR col = 2 TO 9
      text = text + " " + STR(col-1)
      EditBox_SetText(Edit_Text(row,col), text) 
      text = "Col"
   NEXT
NEXT

'--------------------------
' SET UP HEADINGS IN COL 1
'--------------------------
text = "Row"
FOR row = 2 TO NumOfRows
   FOR col = 1 TO 1
      text = text + " " + STR(row-1)
      EditBox_SetText(Edit_Text(row,col), text) 
      text = "Row"
   NEXT
NEXT

'--------------------------------
'Close out code and end program
'--------------------------------
DO
	WaitEvent(msg)
LOOP UNTIL Window_Event_Close(Window_Main, msg)

END
------------------
if I have a loop for me is better to use REDIM
--- start loop
ReDim AS INTEGER a(ANY, ANY)
DIM AS INTEGER x, y
INPUT x
INPUT y
ReDim AS INTEGER a(X, Y)
PRINT LBOUND(a, 1), UBOUND(a, 1)
PRINT LBOUND(a, 2), UBOUND(a, 2)


INPUT x
INPUT y
ReDim AS INTEGER a(X, Y)
PRINT LBOUND(a, 1), UBOUND(a, 1)
PRINT LBOUND(a, 2), UBOUND(a, 2)
---contiue loop
SLEEP
Post Reply