Page 100 - Computer Science Class 11 Without Functions
P. 100

Table 4.1 comprises the data of the annual GDP growth rate in percent (%), for a period of ten years between 2005–06
        and 2014–15. Don't worry if you do not know the meaning of GDP. For now, you may consider GDP as the growth rate.

                                            Year                 GDP Growth Rate (%)

                                           2005-06                       9.5
                                           2006-07                       9.6
                                           2007-08                       9.3
                                           2008-09                       6.7
                                           2009-10                       8.6
                                           2010-11                       8.9
                                           2011-12                       6.7
                                           2012-13                       5.4
                                           2013-14                       6.4
                                           2014-15                       7.4

                                  Table 4.1 GDP growth rate during the decade beginning 2005-06
        As the values in the table appear sequentially, one after the other, let us call these values growthRate[1], growthrate[2],
        …, growthRate[10]. You are required to find the maximum growth rate during the decade 2005-06 to 2014-15. To
        solve this problem, you will perhaps look at the first value in the table and register it in your memory. Next, you will
        compare it with the second value. If you find that the value in your memory is smaller than the second value, you
        forget the value registered in your memory and replace it with the second value. However, in the other case (the
        second value is not larger than the value registered in your memory), you ignore the second value. In either case, the
        value registered in your memory is the largest so far. So, you move ahead and compare the value registered in your
        memory with the third value. Again, if you find that the value in your memory is smaller than the third value, you forget
        the value registered in your memory and replace it with the third value. However, in the other case (the third value is
        not larger than the value registered in your memory), you ignore the third value. In either case, the value registered
        in your memory is the largest so far. So, you move ahead and compare the value registered in your memory with the
        fourth value, and so on until you have examined the last (tenth) value.

        As mentioned above, we associate the values stored in the computer memory with names that enable us to refer to
        them conveniently. Typically, the names of the memory locations are written in camelCase. For example, the names

        growthRate and maxGrowthRate may be used to denote the growth rate and maximum value of the growth rate.
        As there are ten different values of growth rate, we denote these values collectively by the name growthRate and
        individually by growthRate[1], growthRate[2], ,…, growthRate[10].
        It is interesting to note that computer memory also works in a similar manner. For instance, in the above example, to
        begin with, we set:
               maxGrowthRate ← growthRate[1]
        This sets the value of maxGrowthRate as 9.5. Subsequently, when we write:
               maxGrowthRate ← maxGrowthRate[2]
        The maxGrowthRate would have the value 9.6. Note that the value of the named location maxGrowthRate
        has changed from 9.5 to 9.6. Indeed, the named locations are called variables as their values change over time.
        As we need to examine all values of the growth rate, we need a construct that allows us to process each value of
        growthRate one by one. Although exact syntax may vary slightly from language to language, most programming
        languages provide a for-construct for this purpose. As it allows us to represent execution of steps for several values,
        it is called a for-loop:

        for i = 2 to nYears do
            Computations for each value of growthRate
        end-for
          98   Touchpad Computer Science-XI
   95   96   97   98   99   100   101   102   103   104   105