How to display an upper triangular matrix?

Asked

Viewed 912 times

1

Write a program that prints a 10x10 matrix that is triangular higher, that is, the elements below the main diagonal are null (or zero).

Until then I managed to do this:

for linha in range(10):
    for coluna in range(10):
      if(linha == coluna):
        print("1", and = "/t")
      else:
        if(coluna > linha):
          print("2", end = "\t")
        else:
          print("0", end = "\t")
    print ("\n")

But he’s making a mistake, someone can help me?

  • 3

    What is the mistake that is happening? Could you edit your question and format the code so that we can better understand your problem?

  • 6

    Revise line 4 of your code. It is in it that you are giving the error that you did not put in the question. Compare what you did on this line with the others and you’ll see the problem.

2 answers

3

On line 4:

print("1", and = "/t")

Should be replaced by:

print("1", end="/t")

Error messages are important both for you to understand what is happening in your code and to enrich your question. Next to the next, try posting the messages :)

0

The subtype matriz triangular is only applicable to square matrices. Thus, any triangular matrix - upper or lower - will necessarily always be a square matrix.

According to this definition a matrix is triangular superior, if and only if, the square matrix has elements Aij = 0 for all i > j.

As my intention is to show how to assemble and display an upper triangular matrix of any order, then we can use the following algorithm below:

import numpy as np

while True:
    try:
        m = int(input('Digite a ordem da matriz: '))
        if m < 1:
            print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
        else:
            break
    except ValueError:
        print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')

lista = list()
for i in range(1, m + 1):
    linha = list()
    for j in range(1, m + 1):
        if i > j:
            linha.append(0)
        else:
            while True:
                try:
                    v = int(input(f'Digite o {j}º valor da {i}ª linha: '))
                    break
                except ValueError:
                    print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
            linha.append(v)
    lista.append(linha)

matriz = np.array(lista)
print(f'\033[32mA matriz Triangular superior é:\n{matriz}\033[m')

Note that to mount the algorithm I imported the library numpay. This, in turn, had previously been installed.

Note that the first block While True, captures and treats the typed value that represents the matrix order.

Later, the algorithm performs block nesting for. It is, through this nesting of blocks for, which will be mounted a list of lists containing all the entered values. Then the method array library numpy organizes the spatial presentation of the list, producing a matrix in tabular form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.