Page 178 - C++
P. 178

Q3.  (a) Write C++ function swap() that accepts an array and its size as arguments and swaps the first half
        elements with the second half.                                                                       (2)

        For example the following array










        Ans.
        void swap(int b[6], int size)
        {
               int r=size/2, i=r, k, temp;
               for(k=0; k<r; k++)
               {
                       temp=b[k];
                       b[k]=b[i];
                       b[i]=temp;
                       i++;
               }
               cout<<"Array after swap : ";
               for(k=0;k<size;k++)
               {
                       cout<<b[k];
               }
        }
                                                             OR

        (a) Write a function int SKIPSUM(int A[ ] [3], int N, int M) to find and return the sum of elements from all
            alternate elements of a two dimensional array starting from A[0][0].                      (2)

        Ans.

        int SKIPSUM(int A[ ][ 3 ], int N, int M)

        {

        int S=0:

         for(int I = 0; 1< N; 1++)

                for (int J = (I%2)?1:0; J<M; J = J+2)

                       S = S + A [I][J];

         return S;

        }
   173   174   175   176   177   178   179   180   181   182   183