Computer Science, asked by BrainlyHulk, 1 year ago

Write the function ALTERNATE ( int A [ ] [ ] , int n , int m ) to display all alternate elements from 2D array A .

C++ || Class 11

Answers

Answered by poojan
2

Language using : C++

The function ALTERNATE() goes as follow:

void ALTERNATE ( int A [ ] [ ] , int n , int m )

{

  int count=0;

  for ( int i = 0 ; i < n ; i++ )

  {

     for ( int j = 0 ; j < m ; j++ )

       {

          if ( count%2==0 )

            {

               cout << A[i][j] << "  ";

            }

          count++;

        }

   }

}

Example :

9   32    54

5    16    28

6    93    72

Output will be :

9   54  16  6  72

Explanation :

  • In ALTERNATE ( int A [ ] [ ] , int n , int m ), int A[ ][ ] is the 2D array defined, n is the number of rows present, where m denotes the columns.

  • Count variable is initialized to 0. For every element in each row and column, starting from the first one, we keep on increasing count value by 1, after checking whether the current value it possess gives 0 as remainder when %2 or not.

  • Every alternate number on %2 leaves remainder 0. Starting from 0, 0%2 gives 0. So, first value is printed. next, count becomes 1 and 1%2 leaves remainder 1, so no value gets printed. In the same way, for every alternate count, from 0 to 2, 4, 6, 8, 10, 12... the corresponding array values get prints.

  • So, the count value at which %2 remainders 0, prints a value. On whole, the code prints the series of alternate values of 2D array. Taht's it!

Learn more :

  • What is array in C++

        https://brainly.in/question/1849908

  • WAP TO STORE 10 NUMBERS IN A ARRAY A[] ABD 5 NUMBERS IN A ARRAY B[] AND THEN MERGE INTO ARRAY C []

         https://brainly.in/question/16360859

Similar questions