Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to check for the first empty element in an array?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
GreatPumpkin




PostPosted: Wed Aug 21, 2013 11:39 pm   Post subject: How to check for the first empty element in an array?

What is it you are trying to achieve?
This is my first time working with arrays since I started using Turing last September, I also have no experience with for loops because nothing I have done has required them. Well now I'm working on a game that will be using multiple units with different stats on both the players side and the enemies side, for this I will use a multidimensional array. The columns will be the unit ID and the rows will be the stats. The first row will indicate the health of the unit in the given column, the second row will indicate the attack of the unit in the given column, so it will end up something like this:

Unit # 0 1 2 3 4
HP: 100 75 80 20 70
Att: 7 7 6 10 6

That's my final goal, if it makes any sense. But like I said I've never really used arrays so right now I'm just getting used to indexing and learning how to work with the arrays.

What I'm trying to do right now is find the first empty element in an array, so that I can create the stats for one unit in that column/element, let the program do something else for a few loops then add another enemy in the first empty element again. This is for two reasons, so I can create a new unit in the correct column, and so that I can reuse the elements of a unit that has been destroyed (stats set back to 0)
It's seems like this is one of those things that there is a command or simple "for loop" script for, but I can't seem to find anything online.


What is the problem you are having?
Even after looking up how to, I can't figure out how to go to the first element in an array that has a no value.


Describe what you have tried to solve this problem
Until now I have been using a variable called "myArray"Pointer which I increased by one every time I put information into the array, and I'd put the value into myArray (myArrayPointer), but that seems like a really lazy/improper way of going about doing this. I assumed an empty element was equal to zero, but it appears that it's not, as when I try to read from an element that hasn't had a value assigned to it, it returns the "variable has no value." error.

I looked into using "upper(myArray)", but that doesn't seem to do the job, as if I have an array consisting of 5 elements, of which the first 3 are filled out, and the last 2 are empty it will still return 5, the last available element in the array. So I don't think this is what I'm looking for.



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
This is the script I'm playing around with to learn, pretty much it makes an array 0 .. 5 (so 6 elements), fills element 0 with 25, 1 with 20, 2 with 15 and 3 with 10, leaving 4 and 5 empty. Then it goes through and checks to see if each element starting # 0, to see if it contains a 0. However when it gets to element number 4 (the fifth one and first empty one) it returns the "variable has no value." error.

Turing:


var num : int := 25
var array1 : array 0 .. 5 of int
var array1Pointer : int := 0
for i : 0 .. 3
    array1 (array1Pointer) := num
    put array1 (array1Pointer)
    array1Pointer := array1Pointer + 1
    num := num - 5
    delay(100)
end for
array1Pointer:=0
cls
for i : 0 .. upper(array1)
put "1"
    if array1(array1Pointer) = 0 then
        quit
    else
    put array1(array1Pointer)
    end if
    array1Pointer:=array1Pointer+1
end for
put "Done"





Please specify what version of Turing you are using
4.1.1

Any help would be appreciated, this is my first post but I've been referring to this website for the past year to get answers for problems that the schools computer teacher was unable to help me with, but I couldn't seem to find any topics on this website or anywhere out there about this problem.
 
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Thu Aug 22, 2013 10:47 am   Post subject: RE:How to check for the first empty element in an array?

Check out flexible arrays.

http://compsci.ca/v3/viewtopic.php?p=135268#135268
 
DemonWasp




PostPosted: Thu Aug 22, 2013 12:58 pm   Post subject: RE:How to check for the first empty element in an array?

I don't think there is any way to detect whether a variable has been assigned a value, short of reading it and having that crash your program.

If you want to continue using parallel arrays (this is what you describe above: several arrays describing the different parts of several objects), then you could just add another array of booleans that says whether arrays(i) is in use or not.

You really should learn about record and type though, they'll make your life a lot easier. Instead of:

Turing:

var unitActive : array 1 .. MAZ_UNITS of boolean
var unitHealth : array 1 .. MAX_UNITS of int
var unitName : array 1 .. MAX_UNITS of string
...

for i : 1 .. MAX_UNITS
    if unitActive(i) then
        put "Unit #", i, " named '", unitName(i), "' has ", unitHealth(i), " hp."
    end if
end for


You can group all the values for one unit together:
Turing:

type Unit :
    record
        active : boolean
        health : int,
        name : end
    end record

var units : array 1 .. MAX_UNITS of Unit

for i : 1 .. MAX_UNITS
    if units(i).active then
        put "Unit #", i, " named '", units(i).name, "' has ", units(i).health, " hp."
    end if
end for


For more details, see the Turing Walkthrough: http://compsci.ca/v3/viewtopic.php?t=8808
 
Insectoid




PostPosted: Thu Aug 22, 2013 2:25 pm   Post subject: RE:How to check for the first empty element in an array?

A very simple way to solve this problem is to define one value as 'empty'. For example, if unit IDs start at 1, then 0 can refer to an empty slot.
 
GreatPumpkin




PostPosted: Thu Aug 22, 2013 4:19 pm   Post subject: Re: How to check for the first empty element in an array?

Hey thanks guys, I figured it out this morning, doing something like what Insectiod said, I set every value of the array to 0 using a for loop with the lower and upper commands. My mind came across something like this yesterday, but I thought it seems like poor programming practice to me for some reason, especially if there was an already made command..
I'm sure I'll check out record and type soon, but I'm still trying to figure out arrays, and if I don't have array's fully understood yet, I don't think it would help me very much.

Demonwasp, is a parallel array just another name for multidimensional ?
 
Raknarg




PostPosted: Thu Aug 22, 2013 4:35 pm   Post subject: RE:How to check for the first empty element in an array?

It's not bad practice at all imo, if there are values you don't expect to be using they work great for arbitrary constants. And when he says parallel arrays, I think he means arrays of the same size where element n is related to object n in all cases.

var health : array 1 .. 5 of int
var damage : array 1 .. 5 of int
var speed : array 1 .. 5 of int

They're different arrays, but they all refer to the same array of objects (ie. objects 1 through 5).
 
GreatPumpkin




PostPosted: Thu Aug 22, 2013 11:57 pm   Post subject: Re: How to check for the first empty element in an array?

So I've ran into another problem involving the GUI console, another thing I haven't really used at school. The problem is that when the cls command is used the buttons disappear, this wasn't a problem as I made a procedure to clear the screen and redraw the buttons, called to by a button that is created within the clear procedure, which is ran before the main loop.
What I've done works so far, but I can't call the clear procedure from within any other procedure, this is because the clear procedure has to be at the end, because the buttons have to be made after the procedure that they are calling to. So it's kind of a catch 22. I only planned on using the buttons for debugging purposes, because I don't really know how to use them and I know how to create click able menus and buttons without the GUI. So it isn't a huge problem, because I can click the clear button in between each button I press for debugging reasons, but is there any way around this?




Turing:


import GUI
View.Set ("graphics:600;400")

var removeThisUnit:int

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Arrays for units
var attackArray : array 1 .. 10 of int %%% make attackArray with 10 elements all 0
var attackArrayPointer : int := lower (attackArray)   %%% with pointer@1
for i : lower (attackArray) .. upper (attackArray)
    attackArray (i) := 0
end for                         %%%%

var healthArray : array 1 .. 10 of int %%% make health with 10 elements all 0
var healthArrayPointer : int := lower (healthArray)   %%% with pointer@1
for i : lower (healthArray) .. upper (healthArray)
    healthArray (i) := 0
end for                         %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%Draw GUI and clear command
proc clear
    cls
var addUnitButton : int := GUI.CreateButton (50, 100, 0, "Add Unit", addUnit)
var clsButton : int := GUI.CreateButton (130, 100, 0, "cls", clear)
var checkUnitsButton : int := GUI.CreateButton (180, 100, 0, "Check Units", checkUnits)
var removeUnitButton : int := GUI.CreateButton (280, 100, 0, "Remove Unit", removeUnit)
var battleButton : int := GUI.CreateButton (380, 100, 0, "Battle 2 Units", battleSetUp)
end clear%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



proc addUnit%%%%%%%%%%%%%%%%%%%%%%% Procedure to add unit
    var attackArrayCheckPointer : int := lower (attackArray) %variable to check through attackArray
    for i : lower (attackArray) .. upper (attackArray)
        if attackArray(attackArrayCheckPointer) = 0 then
            exit
        else
            attackArrayCheckPointer := attackArrayCheckPointer + 1
        end if
    end for         %%% attackArrayCheckPointer will now be pointing to
                    %%% the first available unit space.
    if attackArrayCheckPointer <= upper(attackArray) then
    attackArray(attackArrayCheckPointer) := ((Rand.Int (4, 6)) + (Rand.Int (0, 4)))      %set available spaces's attack to 4-10
    healthArray(attackArrayCheckPointer) := ((Rand.Int (20, 25)) + (Rand.Int (0, 15))) %set available spaces health to 20-40
    end if
end addUnit%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% And Remove Units
proc removeUnit
    put "What unit # would you like to remove?"
    get removeThisUnit
    attackArray (removeThisUnit) := 0
    healthArray (removeThisUnit) := 0
end removeUnit%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

proc checkUnits
    for i : lower (attackArray) .. upper (attackArray)
        put "ATT: ", attackArray(i)," HP: ", healthArray(i)
    end for
end checkUnits

%%%%%%%%%%%%% BATTLE STUFF %%%%%%%%%%%%
var battleUnit1, battleUnit2 : int

proc battleSetUp
    clear
   
end battleSetUp


%%%%%% WHERE GUI AND CLEAR COMMAND SHOULD BE.




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
loop
    exit when GUI.ProcessEvent
    delay (50)
end loop


 
Nathan4102




PostPosted: Sat Aug 24, 2013 2:44 pm   Post subject: RE:How to check for the first empty element in an array?

Not sure if this will be of use to you, but I'll tell you how I've always made my buttons in turing.

Basically, I have buttons drawn onto my GUI (http://i.imgur.com/kKO0AET.png) and then instead of "Exit when GUI.ProcessEvent", something like this could be done:

Turing:
if mousebutton1 = true then
    if x > 5 and x < 10 and y > 5 and y < 10 then
        ....
    elsif x > 20 and x < 25 and y > 30 and y < 38 then
        ....
 
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Aug 24, 2013 2:52 pm   Post subject: RE:How to check for the first empty element in an array?

If you want a simple solution, I can give you a button program I made and show you how to use it after I finish work
 
Dreadnought




PostPosted: Sat Aug 24, 2013 3:03 pm   Post subject: Re: How to check for the first empty element in an array?

To be able to reference a procedure before you declare its body declared beforehand, read the documentation for forward.
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: