Page 293 - Computer_Science_F5
P. 293

Computer Science  referenced. An array should be specified  Examples of declared array are:
           by its name and index. Instead of
                                                          int a[5];
           declaring  each variable  separately, an
                                                          double sum[7];
           array is used. Each index must be within
           square brackets. Array can be classified  Initialisation of one dimensional array
          FOR ONLINE READING ONLY
           into two types: One dimensional  array
           and Multidimensional array.               Array initialisation  means introducing
                                                     values to the arrays. The following is the
                                                     syntax of array initialisation:
           One dimensional array                     dataType  arrayName[arraySize]  =
           One dimensional  array is the type of  {value0, value1, value3, …….., valuek}
           array  that involve  one  array  name and
           only one subscript.                       Example: int marks[5] = {10, 35, 77,
                                                     98, 100};
           Declaration of one-dimensional array      The given example of initialisation can
           An array can be declared using the syntax:  also be defined as:
           dataType arrayName[arraySize];               int marks[5];
           where:                                       marks[0] = 10;
             (a) dataType represents the data type
                of all elements;                        marks[1] = 35;
             (b) arraySize must be an integer greater   marks[2] = 77;
                than zero; and                          marks[3] = 98;
             (c) arrayName must be a character or
                any word to represent an array.         marks[4] = 100;

           The following C++ Program Example  4.33 uses array to  print  the  sum  of  five
           integers. The output of the program is shown in Figure 4.63.

                  Program Example 4.33:

              Using array to print the sum of integers

                  #include<iostream>
                  #include<conio.h>
                  using namespace std;
                  int main()
                  {
                        int a[5], sum = 0, i;
                        cout<<”Enter 5 integers to store in array”<<endl;
                        for(i=0; i<5; i++)
                  {
                        cin>>a[i];
                        sum = sum+a[i];
                  }
                        cout<<”The sum of the given numbers is: “<<sum<<endl;
                        return 0;
                  }


                                                 284
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   284                                                     23/07/2024   12:34
   288   289   290   291   292   293   294   295   296   297   298