Skip to content
Related Articles

Related Articles

Improve Article

Program to print elements of a Matrix row-wise skipping alternate elements

  • Last Updated : 30 Apr, 2021

Given a matrix of N*N size where N is always odd, the task is to print elements of a matrix row-wise skipping alternate elements.
Examples
 

Input: mat[3][3] = 
{{1, 4, 3},
 {2, 5, 6},
 {7, 8, 9}}
Output: 1 3 5 7 9

Input: mat[5][5] = 
{{1, 2, 3, 4, 9},
 {5, 6, 7, 8, 44},
 {19, 10, 11, 12, 33},
 {13, 14, 15, 16, 55},
 {77, 88, 99, 111, 444}}
Output: 1 3 9 6 8 19 11 33 14 16 77 99 444 

 

Approach: 
 

  1. Start traversing the matrix.
  2. For each row, check if the row number is even or odd. 
    • If the row is even, then print the elements at the even index of that row.
    • If the row is odd, print the elements at the odd index of that row.

Below is the implementation of above approach: 
 

C++




// C++ program to print elements of a Matrix
// row-wise skipping alternate elements
#include <bits/stdc++.h>
using namespace std;
#define R 100
#define C 100
 
// Function to print the alternate
// elements of a matrix
void printElements(int mat[][C], int n)
{
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                cout << mat[i][j] << " ";
        else
            for (int j = 1; j < n; j += 2)
                cout << mat[i][j] << " ";
    }
}
 
// Driver code
int main()
{
    int n = 3;
    int mat[R][C] = { { 1, 5, 3 },
                      { 2, 4, 7 },
                      { 9, 8, 6 } };
 
    printElements(mat, n);
 
    return 0;
}

Java




// Java Program to print elements of a Matrix
// row-wise skipping alternate elements
import java.util.*;
import java.lang.*;
 
class GFG{
// Function to print the alternate
// elements of a matrix
static void printElements(int[][] mat, int n)
{
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                System.out.print(mat[i][j] + " ");
        else
            for (int j = 1; j < n; j += 2)
                System.out.print(mat[i][j] + " ");
    }
}
 
// Driver code
public static void main(String args[])
{
    int n = 3;
    int[][] mat = new int[][]{ { 1, 5, 3 },{ 2, 4, 7 },{ 9, 8, 6 } };
 
    printElements(mat, n);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

Python3




# Python 3 program to print elements of a Matrix
# row-wise skipping alternate elements 
 
# Function to print the alternate
# elements of a matrix
def printElements(mat, n) :
 
    for i in range(n) :
        if i % 2 == 0 :
            for j in range(0, n, 2) :
                print(mat[i][j],end = " ")
        else :
            for j in range(1, n, 2) :
                print(mat[i][j],end =" ")
             
     
# Driver Code
if __name__ == "__main__" :
 
    n = 3
    mat = [ [ 1, 5, 3],
            [ 2, 4, 7],
            [ 9, 8, 6] ]
 
    printElements(mat , n)
 
 
# This code is contributed by ANKITRAI1

C#




// C# Program to print elements
// of a Matrix row-wise skipping
// alternate elements
using System;
 
class GFG
{
// Function to print the alternate
// elements of a matrix
static void printElements(int[,] mat,
                          int n)
{
    for (int i = 0; i < n; i++)
    {
        if (i % 2 == 0)
            for (int j = 0; j < n; j += 2)
                Console.Write(mat[i, j] + " ");
        else
            for (int j = 1; j < n; j += 2)
                Console.Write(mat[i, j] + " ");
    }
}
 
// Driver code
public static void Main()
{
    int n = 3;
    int[,] mat = new int[,]{ { 1, 5, 3 },
                             { 2, 4, 7 },
                             { 9, 8, 6 }};
 
    printElements(mat, n);
}
}
 
// This code is contributed
// by ChitraNayal

PHP




<?php
// PHP program to print elements of a Matrix
// row-wise skipping alternate elements
 
// Function to print the alternate
// elements of a matrix
function printElements(&$mat, $n)
{
    for ($i = 0; $i < $n; $i++)
    {
        if ($i % 2 == 0)
            for ($j = 0; $j < $n; $j += 2)
            {
                echo $mat[$i][$j] ;
                echo " ";
            }
        else
            for ($j = 1; $j < $n; $j += 2)
            {
                echo $mat[$i][$j] ;
                echo " ";
            }
    }
}
 
// Driver code
$n = 3;
$arr = array(array(1, 2, 4),
             array(5, 6, 8));
$mat = array(array(1, 5, 3),
             array(2, 4, 7),
             array(9, 8, 6));
 
printElements($mat, $n);
 
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




<script>
// javascript Program to prvar elements of a Matrix
// row-wise skipping alternate elements
 
    // Function to prvar the alternate
    // elements of a matrix
    function printElements(mat , n) {
        for (i = 0; i < n; i++) {
            if (i % 2 == 0)
                for (j = 0; j < n; j += 2)
                    document.write(mat[i][j] + " ");
            else
                for (j = 1; j < n; j += 2)
                    document.write(mat[i][j] + " ");
        }
    }
 
    // Driver code
     
        var n = 3;
        var mat =[[ 1, 5, 3 ],
        [2, 4, 7 ], [
        9, 8, 6 ] ];
 
        printElements(mat, n);
 
// This code contributed by umadevi9616
</script>
Output: 
1 3 4 9 6

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :