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

Username:   Password: 
 RegisterRegister   
 Dynamic Arrays?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Guest




PostPosted: Thu Jul 13, 2006 1:15 am   Post subject: Dynamic Arrays?

Im pretty sure a dynamic array is an array that let's you keep a list and remembers that list or something because I was reading up on it. Here is a program I want to make, could someone help me here, I'm trying to list all the items I've entered but it just shows the last item entered for the length of my for loop. The reason I am guessing is because they array just keeps staying at 1 I think (I can't rememeber I had it a second ago but I'm like half asleep). I could use a file but I want to do it this way. I also tried 2 arrays but it wasn't working, I think that's how your supposed to do it though. Thanks.

code:

var item : int := 0
var word : string

loop
    var database : array 0 .. item of string
    locate (1, 1)
    put ""
    locate (1, 1)
    put "Enter a word: " ..
    get word : *
    database (item) := word
    item += 1
    colour (green)
    locate (2, 1)
    put "Item ", item, ": ", database (item - 1)
    locate (3, 1)
    colour (black)
    for i : 1 .. item
        put database (item - 1)
    end for
end loop
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Thu Jul 13, 2006 2:20 am   Post subject: (No subject)

A dynamic array is just an array that has a variable as an upper bound. Instead of the upper bound being determined at compile time (statically), it can be determined at run time, like so:
code:
var population : int
put "How many students? "
get population
var students : array 1 .. population of int
for i : 1 .. upper (students)
    put "Enter student ", i, "'s average: " ..
    get students (i)
end for


What you're looking for is a flexible array, which you've already been told to look up. Either stop being lazy and read the tutorials we already have or solve your own problems.
Cervantes




PostPosted: Thu Jul 13, 2006 7:37 am   Post subject: Re: Dynamic Arrays?

vahnx wrote:

code:

    var database : array 0 .. item of string
    ...
    for i : 1 .. item
        put database (item - 1)
    end for

Now, why would you have that minus one in there? If you want to index your array starting at zero, then index your for loop starting at zero.
code:

for i : 0 .. item

Or since your code is all wierd (the value of item changes), you'd have to do this:
code:

for i : 0 .. item - 1


So yes. Gandalf is absolutely right. Go read tutorials. Specifically, this one.
Delos




PostPosted: Thu Jul 13, 2006 8:54 am   Post subject: Re: Dynamic Arrays?

vahnx wrote:
I'm trying to list all the items I've entered but it just shows the last item entered for the length of my for loop.
code:

    for i : 1 .. item
        put database (item - 1)
    end for


Additionally, that should be something to the effect of
code:

   put database (i)

as opposed to 'item', as 'item' is a static counter at this point in the proggie; whereas 'i' is the incremental variable in the for loop. If, for instance, you had 5 items in your database, this statement would execute 'put database (4)' 5 times...as opposed to calling each item successively.

Now, on to those flexy arrays tuts!
TokenHerbz




PostPosted: Thu Jul 13, 2006 11:55 am   Post subject: (No subject)

infact wouln't this make an error??

code:

for i : 1 .. item
        put database (item - 1)
    end for


if item is the first one, trying to display item - 1 will call 'item0' which you dont have.


to me it looks like you want to display the items from max to lowest, well there are several ways to do this, but id go with simple...

code:

for decreasing i: item .. 1
    put database (i)
end for
TokenHerbz




PostPosted: Thu Jul 13, 2006 11:59 am   Post subject: (No subject)

i assume you know enough about flexible array's too?? your posts are high so you should have basic knowledge on everything in turing...

But, if you where trying to post (i) and then remove upper(i) then you need to go with flexible array's.


**change the site so i can edit** i had to repost.. o well more bits Smile
Delos




PostPosted: Thu Jul 13, 2006 3:02 pm   Post subject: (No subject)

TokenHerbz wrote:
infact wouln't this make an error??


code:

var database : array 0 .. item of string


Therefore, 1-1 = 0, which is the lower bound of the array.
/sigh#1

Cervantes wrote:

Now, why would you have that minus one in there? If you want to index your array starting at zero, then index your for loop starting at zero.


/sigh#2

TokenHerbz wrote:

**change the site so i can edit** i had to repost.. o well more bits


/sigh#3

That can be changed. Very easily. And by 'that', I mean your bits. In fact, for making me sigh 3 times I think you deserve a little bit of a bit reduction. Heh...
Guest




PostPosted: Thu Jul 13, 2006 3:15 pm   Post subject: (No subject)

Gandalf, I tried Flexible arrays still didnt work. And if your bringing up upperbound stuff, explain?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Jul 13, 2006 3:30 pm   Post subject: (No subject)

vahnx wrote:
Gandalf, I tried Flexible arrays still didnt work.

Flexible arrays work. There's nothing very complicated about them. Why don't you post the code you tried?
Guest




PostPosted: Thu Jul 13, 2006 5:04 pm   Post subject: (No subject)

Ok here is what I recently tried, I think I'm getting closer. Although the flexible array has no effect here I see, as the flexible array is staying at 0 if I am correct, hence the looping. I haven't done Turing in a while so I may be abit crusty.

code:

var item : int := 0
var word : string

loop
    var database : flexible array 0 .. item of string
    locate (1, 1)
    put ""
    locate (1, 1)
    put "Enter a word: " ..
    get word : *
    database (item) := word
    item += 1
    colour (green)
    locate (2, 1)
    put "Item ", item, ": ", database (item - 1)
    locate (3, 1)
    colour (black)
    for i : 1 .. item
        put database (item - 1)
    end for
end loop



And as for my number of posts the person who said something which I can't remember, most of them are in the help section asking for help and not giving help, therefore my lack of knowledge. Shame on you for not researching all my posts! Shame shame shame!

And I just realized, there is a small problem with the html on this site. If I click somewhere in my text and press the code button, it starts it at the end of all my text in my post, then puts code. It really messes me up especially when I click to end my coding, so I started to manually put in the html.
Cervantes




PostPosted: Thu Jul 13, 2006 5:56 pm   Post subject: (No subject)

I don't understand what you're trying to accomplish. What is the purpose of this program, in words?

I suspect you are trying to create a "database" of words that the user enters. How about something like this?
code:

var words : flexible array 1 .. 0 of string
var input : string
loop
    put ""
    put "Type 'exit' to quit."
    put "Type 'print' to print the current stored words."
    put "Type anything else to add that to the stored words."
    get input : *
    exit when input = "exit"
    if input = "print" then
        put "Printing all stored words."
        for i : 1 .. upper (words)
            put words (i)
        end for
    else
        new words, upper (words) + 1
        words (upper (words)) := input
    end if
end loop


vahnx wrote:

And I just realized, there is a small problem with the html on this site. If I click somewhere in my text and press the code button, it starts it at the end of all my text in my post, then puts code. It really messes me up especially when I click to end my coding, so I started to manually put in the html.

Yeah. I think most of us just write it in manually. It's faster that way, even if the buttons did work.

Also, that's BBCode, not html.
Clayton




PostPosted: Thu Jul 13, 2006 7:26 pm   Post subject: (No subject)

well vahnx if you actually read the tut you wouldnt be having these problems, however, look at his code and then look at yours, what do you see in his code that you dont see in your code? the word new. This will allow you to add a new element to your array. the reason you kept getting the only word that you had was because you redeclared your array in the loop, thereby erasing all items from the original, and allowing you to keep all previous elements active/used in the array.

Cervantes wrote:

Yeah. I think most of us just write it in manually. It's faster that way, even if the buttons did work.

Also, that's BBCode, not html.


yes that is true, i never use the buttons along the top, they screw you up too much... also they dont allow you to use the ever so cool [syntax] tags, go highlighted code that doesnt work for turing!
Cervantes




PostPosted: Thu Jul 13, 2006 8:40 pm   Post subject: (No subject)

SuperFreak82 wrote:
go highlighted code that doesnt work for turing!


Ah, but it works in v3 Wink
Using syntax highlighting is still good because when v3 comes, all that will automatically get highlighted and there's no need to go back in the database and change things.
Guest




PostPosted: Fri Jul 14, 2006 1:57 am   Post subject: (No subject)

new and upper, im unfirmillar with those... hence i dont program much anymore. the tutorial looked long and confusing for my small brain. but thanks for the advice. i will try it out later today when i wake up.
Guest




PostPosted: Fri Jul 14, 2006 1:58 am   Post subject: (No subject)

btw it would be helpful if you showed me how to email using turing with some smtp protocol action!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: