Programa para convertir array dada a una array diagonal

Dada una array N*N. La tarea es convertir la array en una array diagonal . Eso es cambiar los valores de los elementos no diagonales de una array a 0.
Array Diagonal : Una array se llama Array Diagonal si todos los elementos no diagonales de la array son cero.
Ejemplos: 
 

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

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

Atraviese todos los elementos no diagonales de la array usando dos bucles anidados como se muestra en el siguiente código y conviértalos en cero.
A continuación se muestra el programa para hacer que todos los elementos no diagonales de una array sean cero: 
 

C++

// C++ program to change the value of
// non-diagonal elements of a matrix to 0
 
#include <iostream>
using namespace std;
 
const int MAX = 100;
 
// Function to print the resultant matrix
void print(int mat[][MAX], int n, int m)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to change the values of all
// non-diagonal elements to 0
void makenondiagonalzero(int mat[][MAX], int n, int m)
{
    // Traverse all non-diagonal elements
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i != j && i + j + 1 != n)
 
                // Change all non-diagonal
                // elements to zero
                mat[i][j] = 0;
        }
    }
 
    // print resultant matrix
    print(mat, n, m);
}
 
// Driver Code
int main()
{
    int mat[][MAX] = { { 2, 1, 7 },
                       { 3, 7, 2 },
                       { 5, 4, 9 } };
 
    makenondiagonalzero(mat, 3, 3);
 
    return 0;
}

C

// C program to change the value of
// non-diagonal elements of a matrix to 0
#include <stdio.h>
#define M 100
#define N 100
 
// Function to print the resultant matrix
void print(int mat[M][N], int n, int m)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ",mat[i][j]);
        }
        printf("\n");
    }
}
 
// Function to change the values of all
// non-diagonal elements to 0
void makenondiagonalzero(int mat[M][N], int n, int m)
{
    // Traverse all non-diagonal elements
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (i != j && i + j + 1 != n)
 
                // Change all non-diagonal
                // elements to zero
                mat[i][j] = 0;
        }
    }
 
    // print resultant matrix
    print(mat, n, m);
}
 
// Driver Code
int main()
{
    int mat[][100] = { { 2, 1, 7 },
                       { 3, 7, 2 },
                       { 5, 4, 9 } };
 
    makenondiagonalzero(mat, 3, 3);
 
    return 0;
}
 
// This code is contributed by kothvvsaakash.

Java

// Java program to change the value of
// non-diagonal elements of a matrix to 0
import java.io.*;
 
class GFG
{
static int MAX = 100;
 
// Function to print the resultant matrix
static void print(int mat[][], int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            System.out.print( mat[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Function to change the values of all
// non-diagonal elements to 0
static void makenondiagonalzero(int mat[][],
                                int n, int m)
{
    // Traverse all non-diagonal elements
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (i != j && i + j + 1 != n)
 
                // Change all non-diagonal
                // elements to zero
                mat[i][j] = 0;
        }
    }
 
    // print resultant matrix
    print(mat, n, m);
}
 
// Driver Code
public static void main (String[] args)
{
    int mat[][] = { { 2, 1, 7 },
                    { 3, 7, 2 },
                    { 5, 4, 9 } };
 
    makenondiagonalzero(mat, 3, 3);
}
}
 
// This code is contributed by inder_verma

Python3

# Python 3 program to change the value of
# non-diagonal elements of a matrix to 0
 
# Function to print the resultant matrix
def printmatrix(mat, n, m) :
     
    for i in range(n) :
        for j in range(m) :
            print(mat[i][j], end = " ")
         
        print()
 
# Function to change the values
# of all non-diagonal elements to 0
def makenondiagonalzero(mat, n, m) :
     
    # Traverse all non-diagonal elements
    for i in range(n) :
        for j in range(m) :
            if i != j and i + j + 1 != n :
                 
                # Change all non-diagonal
                # elements to zero
                mat[i][j] = 0
     
    # print resultant matrix
    printmatrix(mat, n, m)
     
# Driver code
if __name__ == "__main__" :
     
    mat = [ [2, 1, 7],
            [3, 7, 2],
            [5, 4, 9] ]
                     
    makenondiagonalzero(mat, 3, 3)
     
# This code is contributed by Ryuga

C#

// C# program to change the value
// of non-diagonal elements of a
// matrix to 0
using System;
 
class GFG
{
     
// static int MAX = 100;
 
// Function to print the resultant
// matrix
static void print(int [,]mat,
                  int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            Console.Write(mat[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Function to change the values of all
// non-diagonal elements to 0
static void makenondiagonalzero(int [,]mat,
                                int n, int m)
{
    // Traverse all non-diagonal elements
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (i != j && i + j + 1 != n)
 
                // Change all non-diagonal
                // elements to zero
                mat[i, j] = 0;
        }
    }
 
    // print resultant matrix
    print(mat, n, m);
}
 
// Driver Code
public static void Main ()
{
    int [,]mat = { { 2, 1, 7 },
                   { 3, 7, 2 },
                   { 5, 4, 9 } };
 
    makenondiagonalzero(mat, 3, 3);
}
}
 
// This code is contributed by anuj_67..

PHP

<?php
// PHP program to change the value of
// non-diagonal elements of a matrix to 0
 
// Function to print the resultant matrix
function print1($mat, $n, $m)
{
    for ($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $m; $j++)
        {
            echo $mat[$i][$j] . " ";
        }
        echo "\n";
    }
}
 
// Function to change the values of
// all non-diagonal elements to 0
function makenondiagonalzero($mat, $n, $m)
{
    // Traverse all non-diagonal elements
    for ($i = 0; $i < $n; $i++)
    {
        for ($j = 0; $j < $m; $j++)
        {
            if ($i != $j && $i + $j + 1 != $n)
 
                // Change all non-diagonal
                // elements to zero
                $mat[$i][$j] = 0;
        }
    }
 
    // print resultant matrix
    print1($mat, $n, $m);
}
 
// Driver Code
$mat = array( array( 2, 1, 7 ),
              array( 3, 7, 2 ),
              array( 5, 4, 9 ) );
 
makenondiagonalzero($mat, 3, 3);
 
// This code is contributed
// by Arnab Kundu
?>

Javascript

<script>
// Java Script program to change the value of
// non-diagonal elements of a matrix to 0
let MAX = 100;
 
// Function to print the resultant matrix
function print(mat,n,m)
{
    for (let i = 0; i < n; i++)
    {
        for (let j = 0; j < m; j++)
        {
            document.write( mat[i][j] + " ");
        }
        document.write("<br>");
    }
}
 
// Function to change the values of all
// non-diagonal elements to 0
function makenondiagonalzero(mat,n,m)
{
    // Traverse all non-diagonal elements
    for (let i = 0; i < n; i++)
    {
        for (let j = 0; j < m; j++)
        {
            if (i != j && i + j + 1 != n)
 
                // Change all non-diagonal
                // elements to zero
                mat[i][j] = 0;
        }
    }
 
    // print resultant matrix
    print(mat, n, m);
}
 
// Driver Code
 
    let mat = [[ 2, 1, 7 ],
                    [ 3, 7, 2 ],
                    [ 5, 4, 9 ]];
 
    makenondiagonalzero(mat, 3, 3);
 
// This code is contributed by sravan kumar G
</script>
Producción: 

2 0 7 
0 7 0 
5 0 9

 

Complejidad del tiempo : O(n*m)
 

Publicación traducida automáticamente

Artículo escrito por gfg_sal_gfg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *