Announcement

Collapse
No announcement yet.

Desperately Need help with arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rodney Hicks
    replied
    One last attempt.
    If you are using PBCC change both MSGBOX statements to PRINT and immediately following the PRINT statements enter the line
    Code:
    somestring$=WAITKEY$
    Be sure to DIM somestring$ first, then run the code. You will get an output of sorts.

    Leave a comment:


  • Rodney Hicks
    replied
    Both PBCC and PBWin are 'windows versions'.
    PBCC creates a console window, very similar to 'Dos in a Window' and programming for PBCC is quite close to DOS programming.
    PBWin creates whatever kind of window you require, and has a host of features, like DDT(Dynamic Dialog Tools).

    You can Click "HELP" and if the bottom line is "ABOUT PBEdit" you have PBWin, "ABOUT CCEdit" will indicate PBCC. Clicking on whichever one that shows up will give you the version number that you are using.

    Leave a comment:


  • Michael Mattias
    replied
    >Nothing is printed from the array so I assume nothing was stored in the array,

    None of the code you have posted contains any output statements at all, so we can't tell if your problem is getting the data into your array or if that data are loading correctly but there is an error in your 'displaying' code.

    That "small compilable example demonstrating the problem" would go a long way to solving your challenge.

    Leave a comment:


  • Guest's Avatar
    Guest replied
    it is the windows version...

    Leave a comment:


  • Rodney Hicks
    replied
    If you are using PBCC then the program will compile but not give any output. We assumed since you were posting in the PBWin(There is a PBCC forum as well) forum that you were using the PBWin version.
    If you wait a bit, either I or someone else will develop a program that works in PBCC. In fact, the existing program likely needs very little change to work in PBCC. I haven't time at the moment, but possibly by morning I will have.
    Could you give us the values of d and x?

    Leave a comment:


  • Dave Biggs
    replied
    Sorry we couldn't help yet. Maybe if you post a small compilable sample you are using to learn by we could help you debug it?
    BTW what compiler are you using?

    Leave a comment:


  • Guest's Avatar
    Guest replied
    I appreciate all of your input very much, and I'm sure that the code you gave gives you something on your computers. The thing is, on mine it gives a blank (I tried both Rodney and Dave's code inserted as the main part of the loop and no message boxes appeared). Nothing is printed from the array, so I assume nothing was stored in the array, but again I do not know. I'm going to try another language because I need to learn, properly learn, arrays from the beginning before I have a hope in hell of achieving what I want. I know what they are and what they are for, but I don't really know how to create them yet or how my creation, when at long last I have one, looks.
    By the way, the
    Code:
    IF e = 1 THEN
    GOTO 3
    END IF
    that is to end the program earlier, when it happens that h < x happens after e=1

    Thanks again for your help, but truthfully all I did was cut and paste your ideas without great understanding. I need great understanding first.
    Michael.

    Leave a comment:


  • Michael Mattias
    replied
    Code:
    Open "Test.txt" For Output As #1
      Print #1, a()       ' NB numeric values saved as ASCII text equivalent values
      Close #1            '    delimited w/ $CRLF (new line per data value)
    You might want to consider using the JOIN$ function here, along with PARSE when loading from disk. That could preserve the "rows" nature of the data.

    Leave a comment:


  • Gösta H. Lovgren-2
    replied
    Originally posted by Rodney Hicks View Post
    Gösta
    You do not need the colons if you use line numbers, only with labels, nor do line numbers need to be on a separate line by themselves. Try it, you'll find that each line of code can have its own line number. Why we would reintroduce them though is a bit beyond me, unless to explain something to someone. I have no idea what the debugger thinks of them though.
    I introduced the line numbers in to quickly/efficiently/fastly/speedily/accurately/unaggravatinlgly narrow down where the gpf was getting tripped using a heretofore generally unknown/unused function of the new De Bug Display On feature of PBWin9 - See -
    http://www.powerbasic.com/support/pb...ad.php?t=40802

    Dunno what the Debugger thinks of them but I know I like them. {grin}

    I left them in the example code to show just how easily once formidably difficult to find errors can get tracked down in PB9. (JIC the sagging AUD rises once again.)

    ===================================================
    "If you were plowing a field,
    which would you rather use?
    Two strong oxen or 1024 chickens?"
    Seymour Cray (1925-1996), father of supercomputing
    ===================================================
    Last edited by Gösta H. Lovgren-2; 14 Jul 2009, 09:04 AM.

    Leave a comment:


  • Dave Biggs
    replied
    Sheet ! - I should have thought of that

    Leave a comment:


  • Rodney Hicks
    replied
    Dave
    I would think that if we were only to deal with three dimensions we would use x-axis, y-axis, and z-axis. Since we are using table terms to describe the dimensions (column, row) I would think 'sheet' might be the third term employed, as you might in a spreadsheet. Not a very intuitive term though!(Possibly because we're programmers, not users.)

    Gösta
    You do not need the colons if you use line numbers, only with labels, nor do line numbers need to be on a separate line by themselves. Try it, you'll find that each line of code can have its own line number. Why we would reintroduce them though is a bit beyond me, unless to explain something to someone. I have no idea what the debugger thinks of them though.

    None of us has figured out what the following lines were for in the original puzzle and it would be nice to hear from the perp.. poster of this puzzle.

    IF e = 1 THEN
    GOTO 3
    END IF

    Obviously data dependent and we don't know the values of his constants. Perhaps as a means to iterate the loop so the x value didn't get stored?

    Leave a comment:


  • Gösta H. Lovgren-2
    replied
    I took Dave's great example and modified it a tad using a 1 based array instead of 0 based. It may be easier to follow for a newby to PB (and even some old dogs who are slow on the pickup - chez moi for example) Note number alignment in second msgbox as well.

    Code:
    #Debug Display On     ' Useful when developing apps with arrays (traps out of bounds errr etc.)
    #Dim All
    #Register None
    #Include "WIN32API.INC"
    '------------------/
     
    Function PBMain()
     Dim sTest$, h&, e&, d&, x&, Rec&, n&
     Dim a&()
     Dim FileData$()
    1: 
        x = 6 : d = 8
      ReDim a(1 To 3, 1 To x)   ' 3 'columns' by 6 'rows'
    2: 
       h = 1
      Do While h <= x              ' h = 'row' number
        e = e + d
        e = e - x
        a(1,h) = h                ' assign a value to each column in this row
        a(2,h) = e                '
        a(3,h) = x                '
          sTest = sTest + Str$(a(1,h)) + $Tab + Str$(a(2,h)) + $Tab + Str$(a(3 ,h)) + $Cr
        h = h + 1                 ' INCR h 
      Loop
    3: 
        MsgBox  " h" + $Tab + " e" + $Tab + " x" + $Cr + $Cr + _
                sTest,,"Results" : sTest = ""
     
      Open "Test.txt" For Output As #1
      Print #1, a()       ' NB numeric values saved as ASCII text equivalent values
      Close #1            '    delimited w/ $CRLF (new line per data value)
     
      ' some time later :) ...
     
      Open "Test.txt" For Input As #1
      FileScan #1, Records To Rec
      ReDim FileData(1 To 3, 1 To Rec/3)
      Line Input #1, FileData()
      Close #1
     
      ReDim a(1 To 3, 1 To Rec/3)
      For n = 1 To Rec/3
                          ' Convert (ASCII) file data back to numeric array
        a(1,n) = Val(FileData(1,n)) : a(2,n) = Val(FileData(2,n)) : a(3,n) = Val(FileData(3,n))
        sTest = sTest + Using$("###",a(1,n)) + $Tab + Using$("###",a(2,n)) + $Tab + Using$("###",a(3,n)) + $Cr 
      Next
        MsgBox " h" + $Tab + " e" + $Tab + " x" + $Cr + $Cr + _
               sTest,,"From file"
     
    End Function
    '------------------/PBMain
     ' File Test.txt data values stored thus:
     ' column 0, row 0
     ' column 1, row 0
     ' column 2, row 0
     ' column 0, row 1
     ' column 1, row 1
     ' column 2, row 1
     ' column 0, row 2
     ' etc...__________________
    (Note the line numbers. Got a couple gpf's while doing it and line numbers pinpointed it in no time {grin})

    ===============================
    "I don't even butter my bread;
    I consider that cooking."
    Katherine Cebrian
    ===============================

    Leave a comment:


  • Dave Biggs
    replied
    Thank you Rod

    I fixed the comment, I guess I was thinking, first is the Column dimension then there's the Row dimension... I wonder, what would you call a third dimension? Axis maybe ??

    It's been an interesting exercise that was overdue for me and did help put a few things in place (The cell is easily boggled by array stuff!)

    Leave a comment:


  • Rodney Hicks
    replied
    Very nicely done Dave!!!
    The only change to what you've done that I would make is to change the comment
    ' 3 'columns' by 1 'row'
    to
    ' 3 'columns' by 6 'rows'

    Strangely enough, this program, and the description of what someone wanted to program are very similar to how I got started in programming many years ago, before even Turbo Basic. I had asked my little brother( a computer sciences student ) to show me how to do some input and calculations and display, no filing routines though(I hadn't thought that far ahead or didn't even know the concept).
    After muttering something about "gutter basic"(The first model Atari) he proceeded to write 30 lines of code and left. He also, a couple of years later, told me not to waste my time with the BASICA I believe it was that came with my first IBM clone but to get "TURBO BASIC"(and later the name change to POWERBasic).
    It seems it has taken more than twenty years to close the circle.

    Leave a comment:


  • Dave Biggs
    replied
    I've modified my previous sample to demo two dimensions as per Rod's post..
    Code:
    #Debug Display On     ' Useful when developing apps with arrays (traps out of bounds errr etc.)
    #Dim All
    #Register None
    #Include "WIN32API.INC"
    '------------------/
     
    Function PBMain()
     Dim sTest$, h&, e&, d&, x&, Rec&, n&
     Dim a&()
     Dim FileData$()
     
        x = 6 : d = 8
      Redim a(0 To 2, 0 To x-1)   ' 3 'columns' by 6 'rows'
     
      Do While h < x              ' h = 'row' number
        e = e + d
        e = e - x
        a(0,h) = h                ' assign a value to each column in this row
        a(1,h) = e                '
        a(2,h) = x                '
          sTest = sTest + Str$(a(0,h)) + $Tab + Str$(a(1,h)) + $Tab + Str$(a(2 ,h)) + $Cr
        h = h + 1                 ' INCR h 
      Loop
     
        MsgBox  " h" + $Tab + " e" + $Tab + " x" + $CR + $CR + _
                sTest,,"Results" : sTest = ""
     
      Open "Test.txt" For Output As #1
      Print #1, a()       ' NB numeric values saved as ASCII text equivalent values
      Close #1            '    delimited w/ $CRLF (new line per data value)
     
      ' some time later :) ...
     
      Open "Test.txt" For Input As #1
      FileScan #1, Records To Rec
      ReDim FileData(0 To 2, 1 To Rec/3)
      Line Input #1, FileData()
      Close #1
     
      ReDim a(0 To 2, 1 To Rec/3)
      For n = 1 To Rec/3
                          ' Convert (ASCII) file data back to numeric array
        a(0,n) = Val(FileData(0,n)) : a(1,n) = Val(FileData(1,n)) : a(2,n) = Val(FileData(2,n))
        sTest = sTest + Str$(a(0,n)) + $Tab + Str$(a(1,n)) + $Tab + Str$(a(2,n)) + $Cr 
      Next
        MsgBox " h" + $Tab + " e" + $Tab + " x" + $CR + $CR + _
               sTest,,"From file"
     
    End Function
    '------------------/PBMain
     ' File Test.txt data values stored thus:
     ' column 0, row 0
     ' column 1, row 0
     ' column 2, row 0
     ' column 0, row 1
     ' column 1, row 1
     ' column 2, row 1
     ' column 0, row 2
     ' etc...
    Last edited by Dave Biggs; 13 Jul 2009, 08:12 PM. Reason: Corrected number of rows in comment

    Leave a comment:


  • Rodney Hicks
    replied
    This works on your array construction and pretty much leaves the file arrangements alone since Dave did a good job with that. In some ways this is more like a puzzle than a request for help!
    Code:
    #COMPILE EXE
    #DIM ALL
    'CONSTANTS
    %d=10          '%d and %x are now the "constants previously declared" and the values never change
    %x=6
    FUNCTION PBMAIN () AS LONG
      LOCAL a() AS LONG
      LOCAL e,h,fnum AS LONG
      DIM a(0 TO %x-1)
      DO WHILE h < %x
        a(h)=e     'assign the value of e to the first element of the array a(0) because at this point h=0
        'left out the PRINT # statement, will use it once the array is full
        'left out the IF block because at this point e=0 and where line 3 is I have no idea
        'if it was meant to go to the PRINT # statement and succeeded you'd have created an endless loop.
        e=e+%d
        e=e-%x
        INCR h    'the same as h=h+1
      LOOP
      fnum=FREEFILE    'the use of FREEFILE means you don't need the first CLOSE statement in Dave's example
      'OPEN statement        '
      'PRINT # fnum, a()
      'CLOSE # fnum
      MSGBOX "1st a(0)="+FORMAT$(a(0))+$CRLF+"2nd a(1)="+FORMAT$(a(1))+$CRLF+"3rd a(2)="+FORMAT$(a(2))+$CRLF+ _
             "4th a(3)="+FORMAT$(a(3))+$CRLF+"5th a(4)="+FORMAT$(a(4))+$CRLF+"6th a(5)="+FORMAT$(a(5))
    'the array:      this would be your 'h' column
    '           a(0)=0
    '           a(1)=4
    '           a(2)=8
    '           a(3)=12
    '           a(4)=16
    '           a(5)=20
    'to get the %x and %d columns you need an array with 2 dimensions and you would make the following
    'changes to the above code.
    'Change DIM a(0 TO %x-1) to DIM a(0 TO 2,0 TO %x-1)
    'Change a(h)=e to a(0,h)=e
    'Insert between e=e+%d and e=e-%x the following a(1,h)=e
    'Insert after the line e=e-%x the following a(2,h)=e
    'Comment out the two lines of the MSGBOX or change to reflect the 2 dimensions
    'You can copy and paste them into position.
    
    'just those changes should give you an array like:
    '           the 'h' column  the %d column  the %x column
    
    '           a(0,0)=0        a(1,0)=10      a(2,0)=4
    '           a(0,1)=4        a(1,1)=14      a(2,1)=8
    '           a(0,2)=8        a(1,2)=18      a(2,2)=12
    '           a(0,3)=12       a(1,3)=22      a(2,3)=16
    '           a(0,4)=16       a(1,4)=26      a(2,4)=20
    '           a(0,5)=20       a(1,5)=30      a(2,5)=24
    
    'This is the closest I can figure from your description
    'You might try inserting your own values for %d and %x
    
    '  *********************************************COPIED so I didn't have to keep switching to and fro
    'DO WHILE h < x
    'ARRAY ASSIGN a()=e
    'PRINT #1, A()
    '
    'IF e = 1 THEN
    'GOTO 3
    'END IF
    '
    'e = e + d
    'e = e - x
    '
    'h = h + 1
    '
    'LOOP
    'x and d are constants previously declared. What I'd like to have is the array storing the values
    'in three columns, x, h and e (ideally,the x values would only show up in the first row and then
    'be blank thereafter; the h is the count). I can do this with the regular print function into a
    'file but then I'd need an arary to get the values back to work. I'm not actually sure if what I'm
    'asking makes sense or is doable, but I think it is.
    END FUNCTION
    A couple of things as yet unclear about your prob..puzzle!
    If 'h is the count' then the first assignment should be a(h)=h but then you would not be storing the calculated value of e in a single dimension array, you'd merely be storing the count. If you make the suggested changes to make it a two dimensional array, I suggest making the change to a(0,h)=h instead of a(0,h)=e, thus your 2d array will store the count in the 'h' column.
    "ideally, the x values would only show up in the first row and then be blank thereafter" does not make any sense to me unless that was what that IF block was supposed to do.

    Leave a comment:


  • Dave Biggs
    replied
    Comments on your posted example..

    ARRAY ASSIGN a()=e < Wrong use of ARRAY ASSIGN
    Should be like "ARRAY ASSIGN x&() = 1,2,3,4,5,.." i.e. one time assignment of
    successive array elements. Not successive assignments of individual elements.

    Also the use of Print #1, a() inside the loop could be an error?
    That form of Print# writes the whole array to the file at once. You'd probably
    want to wait until the array was built before writting it to the file.
    Something like this..
    Code:
      
    #Debug Display On     ' Useful when developing apps with arrays (traps out of bounds errr etc.)
    #Dim All
    #Register None
    #Include "WIN32API.INC"
    '------------------/
     
    Function PBMain()
     Dim sTest$, h&, e&, d&, x&, Rec&, n&
     Dim a&()
     Dim FileData$()
     
      x = 6 : d = 8
      Redim a(0 To x-1)
     
      Do While h < x
        e = e + d
        e = e - x
        a(h) = e
          sTest = sTest + Str$(a(h)) + $Cr
        h = h + 1                  
      Loop
     
      Close #1
        MsgBox sTest,,"Results" : sTest = ""
     
      Open "Test.txt" For Output As #1
      Print #1, a()       ' NB numeric values saved as ASCII text equivalent values
      Close #1            ' delimited w/ $CRLF
     
      Open "Test.txt" For Input As #1
      FileScan #1, Records To Rec
      ReDim FileData(1 To Rec)
      Line Input #1, FileData()
      Close #1
     
      ReDim a(1 To Rec)
      For n = 1 To Rec
                          ' Convert file data back to numeric array
        a(n) = Val(FileData(n))
        sTest = sTest + Str$(a(n)) + $Cr
      Next
        MsgBox sTest,,"From file"
     
    End Function
    '------------------/PBMain

    Leave a comment:


  • Michael Mattias
    replied
    >What I'd like to have is the array storing the values in three columns,

    I assume this means you may have "many" rows, each row with three columns?

    There are three ways to do this off the top of my head...

    1. Use a two-dimensional array, with one dimension being row number, the other dimension being column number. You'll want to put the column as the first subscript (this is counter-intuitive, I know, but it's what you'll need to do to resize the array) It will also require that all three columns are of the same data type (STRING, LONG, etc) or can be formatted to the same type.
    Code:
       REDIM   MyARray (2, nRow-1)   'columns 0, 1,2; as many rows as needed
    2. Use a single dimension array of UDTs
    Code:
    TYPE MyRow 
      Col1         AS    whatever 
      Col2         AS    whatever
      Col3         AS    whatever
    END TYPE
    ....
         REDIM Row(nRow-1) AS MyRow
    3. Use three separate arrays
    Code:
        REDIM   COL1 (nRow-1) AS whatever
        REDIM   COL2 (nRow-1) AS whatever
        REDIM   COL3 (nRow-1) AS whatever
    All three methods allow for fairly straightforward storage to and retrieval from a disk file.

    (I personally like option two here).

    BTW, when you post code, please use Code Tags. THis page can be had by clicking the "BB code" link at the lower left of the page.

    Also, this line of code...
    Code:
    ARRAY ASSIGN a()=e
    ...is probably not what you want. That will only assign the first element of the array (subscript 0 if one dimension, subscript 0,0 if two-dimension, etc). So if you do it in a loop all you are doing is changing that one element each time thru.

    MCM

    Leave a comment:


  • Guest's Avatar
    Guest replied
    This is my loop with the array printing in the middle of it. As it stands, there is only one dimension to the array. I'd ultimately like it to have three but will settle for two.
    the array has previously been dimmed
    DO WHILE h < x
    ARRAY ASSIGN a()=e
    PRINT #1, A()

    IF e = 1 THEN
    GOTO 3
    END IF

    e = e + d
    e = e - x

    h = h + 1

    LOOP

    x and d are constants previously declared. What I'd like to have is the array storing the values in three columns, x, h and e (ideally, the x values would only show up in the first row and then be blank thereafter; the h is the count). I can do this with the regular print function into a file but then I'd need an arary to get the values back to work. I'm not actually sure if what I'm asking makes sense or is doable, but I think it is.

    Leave a comment:


  • Rodney Hicks
    replied
    I originally wrote the code to just print to a file, and it looked really good, but I think that to make the file more usable later on, pulling the values and reusing them for a second program, an array would be a better choice than a regular file.
    Arrays and Files are two separate and distinct features and are not interchangeable. Arrays can be stored in a File, thus Files can contain arrays. Arrays are a type of holder of data in memory and Files are a storage of data, usually on disc.
    The following code shows the development of an array, putting data into each array element, and, via the MSGBOX, displaying each element to help you with this part of your puzzle.
    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
      LOCAL myarray() AS LONG
      LOCAL x, y, counter,calculation AS LONG
      DIM myarray(0 TO 2, 0 TO 3)
      FOR x = 0 TO 2
        FOR y = 0 TO 3
          INCR counter
          calculation=2*counter
          myarray(x,y)=calculation
          MSGBOX "Element #"+FORMAT$(counter)+" myarray(x,y) where x="+FORMAT$(x) _
          +" y="+FORMAT$(y)+" equals "+FORMAT$(calculation)
        NEXT y
      NEXT x
      MSGBOX "Element # 1  myarray(0,0)="+FORMAT$(myarray(0,0))+$CRLF+ _
             "Element # 2  myarray(0,1)="+FORMAT$(myarray(0,1))+$CRLF+ _
             "Element # 3  myarray(0,2)="+FORMAT$(myarray(0,2))+$CRLF+ _
             "Element # 4  myarray(0,3)="+FORMAT$(myarray(0,3))+$CRLF+ _
             "Element # 5  myarray(1,0)="+FORMAT$(myarray(1,0))+$CRLF+ _
             "Element # 6  myarray(1,1)="+FORMAT$(myarray(1,1))+$CRLF+ _
             "Element # 7  myarray(1,2)="+FORMAT$(myarray(1,2))+$CRLF+ _
             "Element # 8  myarray(1,3)="+FORMAT$(myarray(1,3))+$CRLF+ _
             "Element # 9  myarray(2,0)="+FORMAT$(myarray(2,0))+$CRLF+ _
             "Element # 10 myarray(2,1)="+FORMAT$(myarray(2,1))+$CRLF+ _
             "Element # 11 myarray(2,2)="+FORMAT$(myarray(2,2))+$CRLF+ _
             "Element # 12 myarray(2,3)="+FORMAT$(myarray(2,3))
       MSGBOX FORMAT$(myarray(0,0))+"  "+FORMAT$(myarray(1,0))+"  "+FORMAT$(myarray(2,0))+$CRLF+ _
             FORMAT$(myarray(0,1))+"  "+FORMAT$(myarray(1,1))+"  "+FORMAT$(myarray(2,1))+$CRLF+ _
             FORMAT$(myarray(0,2))+"  "+FORMAT$(myarray(1,2))+"  "+FORMAT$(myarray(2,2))+$CRLF+ _
             FORMAT$(myarray(0,3))+"  "+FORMAT$(myarray(1,3))+"  "+FORMAT$(myarray(2,3))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    END FUNCTION

    Leave a comment:

Working...
X