ICT
the blog for ICT
ken013194
暱稱: ホロ
性別: 男
國家: 香港
地區: 葵青區
« July 2026 »
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
最新文章
Text File
Arrays
String Procedures
String Functions
String Operation
文章分類
全部 (23)
Programming Language (23)
訪客留言
最近三個月尚無任何留言
每月文章
日誌訂閱
尚未訂閱任何日誌
好友名單
尚無任何好友
網站連結
尚無任何連結
最近訪客
最近沒有訪客
日誌統計
文章總數: 23
留言總數: 0
今日人氣: 2
累積人氣: 77
站內搜尋
RSS 訂閱
RSS Feed
2009 年 9 月 11 日  星期五   晴天


Arrays 分類: Programming Language

High-level Programming Language (Pascal)

Arrays

  • structured data type (e.g. array and record)
  • can store a collection of data items of the same type into a table-like structure but the variables of simple data type (e.g. integer, real, char , Boolean and string) can only hold a single value at a time
  • each data item stored in an array is called array element
  • each element can be accessed by the array name followed by a subscript (or an index) enclosed in square brackets

    for example,
      StudentName[3] which access the third element of the array StudentName

    Declaration of a One Dimensional Array

      var ArrayName : array [<Index>] of <Element_type>

    • <Index>
      • can be any ordinal types (char, integer, Boolean) or subrange type (2..10 or 'a' .. 'e')
      • the range of <Index> indicates the size of one dimensional array , for example
        var StudentName : array [LowerIndex .. UpperIndex] of string;
      • the array StudentName contains (UpperIndex - LowerIndex + 1) elements
    • <Element_type>
      • represents the type of each element in an array
      • for example,
      • var StudentName : array [1..10] of string ; {string array}
      • TestScore : array [1..ClassSize] of real ; {real number array}

       

Copying an Array

    • Using assignment statement
       Array1 := Array2 ;
      Possible only when the arrays involved have the same array type
      i.e. var Array1, Array2 : array [1..6] of char;

    • Using for loop, copy the element one by one

     for i := 1 to SizeOfArray do
      Array1[i] := Array2[i] ;

Initialization of Array

    • Using for loop to initialize the StudentName and TestScore to null strings and zeros respectively

     for i := 1 to ClassSize do
      begin
       StudentName[i] := '' ;

       TestScore[i] := 0
      end;

Input Data in an Array

    • Using for loop to input the names of students in the array StudentName

     for i := 1 to ClassSize do
      begin
       write ('Please enter the name : ');
       readln (StudentName[i])
      end;

Declaration of a Two Dimensional Array

  var ArrayName : array [<Row_Index>, <Col_Index>] of <Element_type>

    • <Row_Index> and <Col_Index>
      • can be any ordinal types (char, integer, Boolean) or subrange type (2..10 or 'a' .. 'e')
      • the product of the range of <Row_Index> and <Col_Index> indicates the size of two dimensional array , for example


       var StudentActivity : array [1 .. 40, 1..3] of string;

      the array StudentActivity contains (40 - 1 + 1) ’ (3 - 1 + 1) = 120 elements

    • <Element_type>
      • represents the type of each element in an array
      • for example,


         var StudentName : array ['A'..'E', 1..40] of string ; {string array}
           TestScore : array ['A'..'E', 1..40] of real ; {real number array}

Using nested for loop to initialize, copy, input and output two dimensional array

    for example, to initialize 5 rows × 7 columns Num_Table to zero

     for Row := 1 to 5 do
      for Column := 1 to 7 do
       Num_Table [Row, Column] := 0

 






訪客留言 (返回 ken013194 的日誌)

訪客名稱:
電郵地址: (不會公開)
驗證碼:  按此更新驗證碼 (如看不清楚驗證碼請點擊圖片刷新)
俏俏話: (必需 登入 後才能使用此功能)
[ 開啟多功能編輯器 ]