Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Arrays
Author Message
azndragon

Posted: Sun Jan 05, 2003 3:18 pm   Post subject: Arrays

I was just wondering what an array is, and what it does. I checked the tutorial that comes with Turing, and read it. Problem is, the examples that they give you is all wrong, so I was wondering if anyone could post a little description of what it does, along with a working example, etc. Thanks.

Sie_Kensou

Posted: Sun Jan 05, 2003 4:32 pm   Post subject: Arrays

An array is a set of variables of the same type. What is so good about arrays is that you can use them along with loops. For example, let's say you want to have two balls bouncing at the same time. You COULD use two different sets of variables such as 'ball1' and 'ball2', the problem comes when the are, say, 100 balls.... imagine assigning values to each (coordinates for example):

ball1X := 0
ball2X := 0
ball3X := 0
...
ball100X = 0

Instead, you can group them all with one variable name (such as 'ballX') and then simply use and index to refer to each:

var ballX : array 1.. 100 of int

for c: 1.. 100
ballX(c) := 0
end for

This is mainly what arrays are useful for... but I guess there are other applications which come in handy as well.

Ps. Talking about grouping, I also find the following type of coding extremly useful in turing (since there are no "classes" as in Java)

type coordinates:
record
x : int
y: int
end record

var ball : array 1 .. 100 of coordinates

Then you can access each item as:

ball('index').x := 0
ball('index').y := 0
DarkHelmet

Posted: Sun Jan 12, 2003 1:55 am   Post subject: (No subject)

You can also have multi dimensional arrays. These are basically an array inside an array. say you want to store 2 coordinates for a set of 5 things, you could use a multidimensional array. They go as following:

var intnum : array 1..5, 1..2 of int

in order to use these you would have to tell it both the numbers in the brackets. The following example would fill this array with numbers from 1 to 10:

for i : 1..5
for x : 1..2
randint(intnum(i, x), 1, 10)
end for
end for
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: