Results 1 to 6 of 6

Thread: Arrays!

  1. #1
    Join Date
    Nov 2006
    Location
    NSW, Australia
    Posts
    3,487
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default Arrays!

    Arrays


    An array can be defined as a variable that holds a specified number of the same variable. What does this mean?


    Basically, an array of variables holds a certain number of a common variable. The amount of values in the array ‘Length’ can either be defined when declaring the variable, or can be defined inside a procedure or function.

    The position of a value in an array ‘Index’ is important when dealing with array. The first value in an array usually starts with an index of zero, with each successive value in the array having an index one more than the index of the previous value.

    Observe this piece of code:

    SCAR Code:
    Var
      NumberArray : Array [0..10] of Integer;

    Begin
      NumberArray := [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132];
    End.

    In the above example, the length of the array is defined in the variable declaration ‘Array [0..10] of Integer’. This means that the first value in the array will have an index of zero, while the last value in the array will have an index of ten. The values of the array are then declared.

    Splitting the array:

    • NumberArray[0] := 12
    • NumberArray[1] := 24
    • NumberArray[2] := 36
    • NumberArray[3] := 48
    • NumberArray[4] := 60
    • NumberArray[5] := 72
    • NumberArray[6] := 84
    • NumberArray[7] := 96
    • NumberArray[8] := 108
    • NumberArray[9] := 120
    • NumberArray[10] := 132;


    It is much easier and professional to declare an array in one line if possible, but the splitting of the array can also be used to declare it:

    SCAR Code:
    Var
      NumberArray : Array [0..10] of Integer;

    Begin
      NumberArray[0] := 12
      NumberArray[1] := 24
      NumberArray[2] := 36
      NumberArray[3] := 48
      NumberArray[4] := 60
      NumberArray[5] := 72
      NumberArray[6] := 84
      NumberArray[7] := 96
      NumberArray[8] := 108
      NumberArray[9] := 120
      NumberArray[10] := 132
    End.

    Length(S) / High(X)



    When looping through arrays, knowing its length is just as important as knowing what an array is. To find the length of an array, we use the function ‘Length’. The length of the array means the number of values that are contained in it. Note that since the array index usually starts from zero, an array length of one means that there will only be an array index of zero. We can then deduce that if the first index of an array is zero, the last index of the array is equal to the length of the array minus one.

    Observe this piece of code:

    SCAR Code:
    Var
      NumberArray : Array [0..10] of Integer;

    Begin
      NumberArray := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
    End.

    The first index of the array is zero, and its value is one. The second index of the array is one, and its value is two, and so on.

    Simply put:

    • NumberArray[0] := 1
    • NumberArray[1] := 2
    • NumberArray[2] := 3
    • NumberArray[3] := 4
    • NumberArray[4] := 5
    • NumberArray[5] := 6
    • NumberArray[6] := 7
    • NumberArray[7] := 8
    • NumberArray[8] := 9
    • NumberArray[9] := 10
    • NumberArray[10] := 11


    Notice that the number in between the parentheses ‘[ ]’ is always one less than the value contained in the array index.

    The length of the array is usually used for looping through the array. A for/to/do loop is used in order to check every value in the array.

    Observe this piece of code:

    SCAR Code:
    Program New;

    Var
      NumberArray : Array [0..10] Of Integer;
      I : Integer;

    Begin
     NumberArray := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
     
     For I := 0 To Length(NumberArray) - 1 Do
       WriteLn(IntToStr(NumberArray[I]));
    End.

    In this example, the value of our integer ‘I’ changes, going through all the numbers from the first array index ‘zero’ to the last array index ‘Length(NumberArray) - 1’. It then does a WriteLn of the number that is contained in each specific index of the array.

    The ‘High’ function ultimately returns the length of an array minus one, but it does have a certain formula that is unimportant.

    Note : Arrays can hold any type of value in SCAR, ranging from strings to TPoints. The ‘Length’ and ‘High’ functions are compatible with any type of arrays.

    ______________________________________


    Well, there goes another tutorial. I really do hope that some of you are learning something. If you have any questions, feel free to ask me. Till the next tutorial, ciao!
    [CENTER][img]http://signatures.mylivesignature.com/54486/113/4539C8FAAF3EAB109A3CC1811EF0941B.png[/img][/CENTER]
    [CENTER][BANANA]TSN ~ Vacation! ~ says :I Love Santy[/BANANA][/CENTER]

    [CENTER][BANANA]Raymond - Oh rilie? says :Your smart[/BANANA][/CENTER]

  2. #2
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Hah Very good tut, just like mine, everyone's writing tuts now .

    Explains it well rep++

    W00t first post.

  3. #3
    Join Date
    Nov 2006
    Location
    NSW, Australia
    Posts
    3,487
    Mentioned
    1 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks for the reputation. And yes, it's more interesting writing a tutorial for everyone to see rather than explaining one thing over and over to different people on different occasions.
    [CENTER][img]http://signatures.mylivesignature.com/54486/113/4539C8FAAF3EAB109A3CC1811EF0941B.png[/img][/CENTER]
    [CENTER][BANANA]TSN ~ Vacation! ~ says :I Love Santy[/BANANA][/CENTER]

    [CENTER][BANANA]Raymond - Oh rilie? says :Your smart[/BANANA][/CENTER]

  4. #4
    Join Date
    Jan 2008
    Location
    Ontario, Canada
    Posts
    7,805
    Mentioned
    5 Post(s)
    Quoted
    3 Post(s)

    Default

    Small but concise tut. Repped. You should go more into Array of Strings etc though. Not just mention them.

    Nava2
    Writing an SRL Member Application | [Updated] Pascal Scripting Statements
    My GitHub

    Progress Report:
    13:46 <@BenLand100> <SourceCode> @BenLand100: what you have just said shows you 
                        have serious physchological problems
    13:46 <@BenLand100> HE GETS IT!
    13:46 <@BenLand100> HE FINALLY GETS IT!!!!1

  5. #5
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice tutorial. If you even look at this again, you should add that if you want speed while looping through an array, you should create an integer and do YourInt:= High(TheArray);, and then, for the loop, do for SomeVar:= 0 to YourInt do ...
    It's quite a bit faster.

  6. #6
    Join Date
    May 2006
    Posts
    111
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    nice tut! I quickly wanted to jump into array's but had no idea how to declare or anything, very helpful.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Arrays, stuck on arrays
    By Camaro' in forum OSR Help
    Replies: 1
    Last Post: 03-08-2008, 02:02 AM
  2. Help with Arrays
    By kooldude in forum OSR Help
    Replies: 16
    Last Post: 06-15-2007, 05:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •