📜  打印N阶下Hessenberg矩阵

📅  最后修改于: 2021-04-29 14:10:01             🧑  作者: Mango

给定正整数N ,任务是打印N阶的下Hessenberg矩阵,该矩阵包括任意一位随机正整数作为其非零元素。
下Hessenberg矩阵是一个正方形矩阵,其中所有在超对角线上的元素均为零。在数学上,对于所有j> i + 1,mat [i] [j] = 0

例子:

方法:对于打印带有一位正数元素的Lower Hessenberg矩阵,借助于rand()函数,对于j> i + 1的矩阵的所有单元格以及任何一位随机数均打印零。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the Lower Hessenberg
// matrix of order n
void LowerHessenbergMatrix(int n)
{
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
  
            // If element is above super-diagonal
            // then print 0
            if (j > i + 1)
                cout << '0' << " ";
  
            // Print a random digit for
            // every non-zero element
            else
                cout << rand() % 10 << " ";
        }
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    int n = 4;
    LowerHessenbergMatrix(n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to print the Lower Hessenberg
// matrix of order n
static void LowerHessenbergMatrix(int n)
{
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++) 
        {
  
            // If element is above super-diagonal
            // then print 0
            if (j > i + 1)
                System.out.print('0' + " ");
  
            // Print a random digit for
            // every non-zero element
            else
            {
                System.out.printf("%.0f",Math.random() * 10);
                System.out.print(" ");
            }
        }
        System.out.println("\n");
    }
}
  
// Driver code
public static void main(String[] args) 
{
    int n = 4;
    LowerHessenbergMatrix(n);
}
}
  
/* This code is contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach
import random
  
# Function to print the Upper Hessenberg
# matrix of order n
def UpperHessenbergMatrix(n):
  
    for i in range(1, n + 1):
  
        for j in range(1, n + 1):
  
        # If element is below sub-diagonal
        # then pr0
            if (j > i + 1):
                print('0', end = " ")
  
            # Pra random digit for
            # every non-zero element
            else:
                print(random.randint(1, 10), 
                                 end = " ")
        print()
  
# Driver code
n = 4;
UpperHessenbergMatrix(n)
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to print the Lower Hessenberg 
    // matrix of order n 
    static void LowerHessenbergMatrix(int n) 
    { 
        Random rand = new Random();
          
        for (int i = 1; i <= n; i++) 
        { 
            for (int j = 1; j <= n; j++) 
            { 
      
                // If element is above super-diagonal 
                // then print 0 
                if (j > i + 1) 
                    Console.Write(0 + " "); 
      
                // Print a random digit for 
                // every non-zero element 
                else
                    Console.Write(rand.Next(1, 10) + " "); 
            } 
            Console.WriteLine(); 
        } 
    } 
      
    // Driver code 
    static public void Main ()
    {
        int n = 4; 
        LowerHessenbergMatrix(n); 
    }
}    
  
// This code is contributed by AnkitRai01


输出:
3 6 0 0 
7 5 3 0 
5 6 2 9 
1 2 7 0