Page 55 - Touchcode_C8_Flipbook
P. 55

Array Declaration
                 Declaration of array with variables of integer data type is:

                 arr=[10, 20, 30, 40, 50, 60];

                            10       20        30       40        50       60


                            0         1        2         3        4         5                Array Indices
                 Array Length=6, First index=0, Last index=5

                 Explanation:

                 An array named ‘arr’ is declared with data type as integer i.e. this array can only hold integer

                 values. We have initialized this array with six values i.e. 10, 20, 30, 40, 50, 60.

                 In the real world, you can consider books in a library to be the example of arrays where all
                 the shelves have a common data type (read – book) in it.

                   Coding           uiz   01                                                          Critical Thinking



                    State whether the following statements are true (T) or false (F).

                    a.  An array is a collection of similar data type variables.


                    b.  Variables in an array are ordered non-sequentially.





                 PLAYING WITH COLORS

                 Suppose you have a list of colors, and storing them in individual variables will be:

                                                   Color1=“red”

                                                   Color2=“green”
                                                   Color3=“blue”


                 Single variable to store all the colors will be:

                 Color=[“red”, “green”, “blue”]
                 So, you can say that arrays are the special type of variable which can store a collection of
                 values. The values of an array can be accessed by referring to an index number.

                 To access the first item of an array, you will write:

                 X=Color[0]




                                                                                     Programming with Arrays     53
   50   51   52   53   54   55   56   57   58   59   60