Question about Arrays - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Question about Arrays

Dave_EnglandDave_England Posts: 1Member
Hi I'm studying Pascal and using TP7. I have some questions about arrays. I'm not 100% clear on.

How do I reset an array, do I have to loop through it blanking everything out?

Is there a way of adding an item on to an array just like a stack? without needing to know which place to put it in but the next avaible point.

how can I tell how many elements are already in the array?

Thanks for your help

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi I'm studying Pascal and using TP7. I have some questions about arrays. I'm not 100% clear on.
    :
    : How do I reset an array, do I have to loop through it blanking everything out?
    :
    : Is there a way of adding an item on to an array just like a stack? without needing to know which place to put it in but the next avaible point.
    :
    : how can I tell how many elements are already in the array?
    :
    : Thanks for your help
    :
    1: There are two ways for this. The easiest is the loop as you described. The second is to use FillChar() with the length of he array multiplied by the size of each element.

    2: A Pascal array is a static structure, which means that you cannot grow beyond its initial size. So a 10-string array cannot hold more than 10 strings. If you want to have a dynamic array-like structure, you'll need to use a linked list. As for the next available point the Pascal language doesn't have any way of knowing what the "next available point" is, so you'll need to write your own procedure to determine that.

    3: Since arrays are static, you already know the number of elements at the compile-time. If you still want to check the length, then perhaps the Length() or SizeOf() function will do the trick. In case of the SizeOf() you might need to divide by the size of the elements.
  • bpajkbpajk Posts: 156Member
    : : Hi I'm studying Pascal and using TP7. I have some questions about arrays. I'm not 100% clear on.
    : :
    : : How do I reset an array, do I have to loop through it blanking everything out?
    : :
    : : Is there a way of adding an item on to an array just like a stack? without needing to know which place to put it in but the next avaible point.
    : :
    : : how can I tell how many elements are already in the array?
    : :
    : : Thanks for your help
    : :
    : 1: There are two ways for this. The easiest is the loop as you described. The second is to use FillChar() with the length of he array multiplied by the size of each element.
    :
    : 2: A Pascal array is a static structure, which means that you cannot grow beyond its initial size. So a 10-string array cannot hold more than 10 strings. If you want to have a dynamic array-like structure, you'll need to use a linked list. As for the next available point the Pascal language doesn't have any way of knowing what the "next available point" is, so you'll need to write your own procedure to determine that.
    :
    : 3: Since arrays are static, you already know the number of elements at the compile-time. If you still want to check the length, then perhaps the Length() or SizeOf() function will do the trick. In case of the SizeOf() you might need to divide by the size of the elements.
    :
    You don't need to reset an array. When an array is created it is filled with unknown data. If you decide to fill it with your data, you don't need to loop through it zeroing it out. You're data overwrites the unknown data.

    An array in pascal is just like any variable located in the memory. When you're program runs, it reserves space in RAM for static variables, including arrays. An array in the memory looks like a sequence of memory addresses. The number of addresses reserved for the array is dependent of the number of cells in you're array and the size of the type of the cell.
    Examle:
    [CODE]
    var a:array [0..9] of byte; //size: 10 <--10*1 (size of byte is 1)
    b:array [0..9,0..9] of byte; //size: 100 <--100*1
    c:array [0..9] of word; //size: 20 <--10*2 (size of word is 2)
    begin
    writeln(sizeof(a),' ',sizeof(b),' ',sizeof(c));
    readln
    end.
    //total memory reserved is 130B
    [/CODE]

    a[0] points to the first memory address, a[9] points to the last memory address. You can not go pass the initial limits that are 0-9. Well at least not in pascal, but you can do that in Delphi, where you have dynamic arrays. If you want to have dynamic structures in pascal you have to use pointers, which use memory reserved especially for dinamic variables called heap.
Sign In or Register to comment.