Requisito previo: arrays multidimensionales en C/C++
Dada una array bidimensional, escriba un programa para imprimir la array triangular inferior y la array triangular superior.
- La array triangular inferior es una array que contiene elementos debajo de la diagonal principal, incluidos los elementos de la diagonal principal y el resto de los elementos son 0.
- La array triangular superior es una array que contiene elementos por encima de la diagonal principal, incluidos los elementos de la diagonal principal, y el resto de los elementos son 0.
TRIANGULAR INFERIOR : TRIANGULAR
SUPERIOR :
Ejemplos:
Input : matrix[3][3] = {1 2 3 4 5 6 7 8 9} Output : Lower : 1 0 0 Upper : 1 2 3 4 5 0 0 5 6 7 8 9 0 0 9 Input : matrix[3][3] = {7 8 9 3 2 1 6 5 4} Output : Lower : 7 0 0 Upper : 7 8 9 3 2 0 0 2 1 6 5 4 0 0 4
Pasos:
- Para la array triangular inferior, verificamos la posición del índice i y j, es decir, fila y columna, respectivamente. Si la posición de la columna es mayor que la posición de la fila, simplemente hacemos que esa posición sea 0.
- Para la array triangular superior, verificamos la posición del índice i y j, es decir, fila y columna, respectivamente. Si la posición de la columna es más pequeña que la posición de la fila, simplemente hacemos que esa posición sea 0.
C++
// C++ program to print Lower // triangular and Upper triangular // matrix of an array #include<iostream> using namespace std; // Function to form // lower triangular matrix void lower(int matrix[3][3], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i < j) { cout << "0" << " "; } else cout << matrix[i][j] << " "; } cout << endl; } } // Function to form upper triangular matrix void upper(int matrix[3][3], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { cout << "0" << " "; } else cout << matrix[i][j] << " "; } cout << endl; } } // Driver Code int main() { int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; cout << "Lower triangular matrix: \n"; lower(matrix, row, col); cout << "Upper triangular matrix: \n"; upper(matrix, row, col); return 0; }
C
// C program to print lower triangular and upper triangular matrix #include <stdio.h> // Function to print lower triangular matrix void lower (int a[3][3], int r, int c) { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { if(i > j) printf("0"); else printf("%d" , a[i][j]); printf(" "); } printf("\n"); } } //Function to print upper triangular matrix void upper (int a[3][3], int r, int c) { for(int i = 0; i < r; i++) { for(int j = 0; j < c; j++) { if(i < j) printf("0"); else printf("%d" , a[i][j]); printf(" "); } printf("\n"); } } int main() { // code int r = 3, c = 3; int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; printf("\nLower Triangular Matrix is :\n"); lower(a, r, c); printf("\nUpper Triangular Matrix is :\n"); upper(a, r, c); return 0; } // This code is contributed by aayushi2402.
Java
// Java program to print Lower // triangular and Upper triangular // matrix of an array class GFG { // method to form lower // triangular matrix static void lower(int matrix[][], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i < j) { System.out.print("0" + " "); } else System.out.print(matrix[i][j] + " "); } System.out.println(); } } // Method to form upper // triangular matrix static void upper(int matrix[][], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { System.out.print("0" + " "); } else System.out.print(matrix[i][j] + " "); } System.out.println(); } } // Driver Code public static void main(String args[]) { int matrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; System.out.println("Lower triangular matrix: "); lower(matrix, row, col); System.out.println("Upper triangular matrix: "); upper(matrix, row, col); } }
Python3
# Python3 program to print Lower # triangular and Upper triangular # matrix of an array # Function to form lower triangular # matrix def lower(matrix, row, col): for i in range(0, row): for j in range(0, col): if (i < j): print("0", end = " "); else: print(matrix[i][j], end = " " ); print(" "); # Function to form upper triangular matrix def upper(matrix, row, col): for i in range(0, row): for j in range(0, col): if (i > j): print("0", end = " "); else: print(matrix[i][j], end = " " ); print(" "); # Driver Code matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; row = 3; col = 3; print("Lower triangular matrix: "); lower(matrix, row, col); print("Upper triangular matrix: "); upper(matrix, row, col); # This code is contributed by # Shivi_Aggarwal
C#
// C# program to print // Lower triangular and // Upper triangular // matrix of an array using System; class GFG { // method to form lower // triangular matrix static void lower(int [,]matrix, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i < j) { Console.Write("0" + " "); } else Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } } // Method to form upper // triangular matrix static void upper(int [,]matrix, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { Console.Write("0" + " "); } else Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } } // Driver Code static public void Main () { int [,]matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int row = 3, col = 3; Console.WriteLine("Lower triangular matrix: "); lower(matrix, row, col); Console.WriteLine("Upper triangular matrix: "); upper(matrix, row, col); } } // This code is contributed by ajit
PHP
<?php // PHP program to print Lower // triangular and Upper triangular // matrix of an array // Function to form // lower triangular matrix function lower($matrix, $row, $col) { $i; $j; for ($i = 0; $i < $row; $i++) { for ($j = 0; $j < $col; $j++) { if ($i < $j) { echo "0" , " "; } else echo $matrix[$i][$j] , " "; } echo "\n"; } } // Function to form // upper triangular matrix function upper($matrix, $row, $col) { $i; $j; for ($i = 0; $i < $row; $i++) { for ($j = 0; $j < $col; $j++) { if ($i > $j) { echo "0" , " "; } else echo $matrix[$i][$j] ," "; } echo "\n"; } } // Driver Code $matrix = array (array (1, 2, 3), array (4, 5, 6), array (7, 8, 9)); $row = 3; $col = 3; echo "Lower triangular matrix: \n"; lower($matrix, $row, $col); echo "Upper triangular matrix: \n"; upper($matrix, $row, $col); // This code is contributed by jit_t ?>
Javascript
<script> // Java script program to print Lower // triangular and Upper triangular // matrix of an array // method to form lower // triangular matrix function lower( matrix,row,col) { let i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i < j) { document.write("0" + " "); } else document.write(matrix[i][j] + " "); } document.write("<br>"); } } // Method to form upper // triangular matrix function upper(matrix,row,col) { let i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (i > j) { document.write("0" + " "); } else document.write(matrix[i][j] + " "); } document.write("<br>"); } } // Driver Code let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; let row = 3, col = 3; document.write("Lower triangular matrix: <br>"); lower(matrix, row, col); document.write("Upper triangular matrix: <br>"); upper(matrix, row, col); // contributed by sravan kumar </script>
Lower triangular matrix: 1 0 0 4 5 0 7 8 9 Upper triangular matrix: 1 2 3 0 5 6 0 0 9
Complejidad temporal: O(fila x columna)
Espacio auxiliar: O(1)
Este artículo es una contribución de Rishabh Jain . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA