matrice.py

Created by hicham-choukour

Created on January 31, 2025

1.12 KB


def print_matrice(matrice):
    n, p = dim_matrice(matrice)
    for i in range(n):
        ligne = "(" + "  ".join(f"{matrice[i][j]:5}" for j in range(p)) + ")"
        print(ligne)

def crea_matrice(n, p):
    return [[0] * p for _ in range(n)]

def matrice(n, p):
    return [[int(input(f"coefficient ligne {i+1} colonne {j+1} : ")) for j in range(p)] for i in range(n)]

def dim_matrice(matrice):
    return [len(matrice), len(matrice[0])]

def add_matrice(A, B):
    if dim_matrice(A) == dim_matrice(B):
        n, p = dim_matrice(A)
        return [[A[i][j] + B[i][j] for j in range(p)] for i in range(n)]
    else:
        print("Erreur : Les matrices doivent avoir la même dimension pour être additionnées.")
        return None

print("Bonjour, bienvenue dans le programme pour additionner deux matrices :")
n = int(input("Nombre de lignes des matrices : "))
p = int(input("Nombre de colonnes des matrices : "))

print("Saisie de la première matrice :")
A = matrice(n, p)
print("Saisie de la deuxième matrice :")
B = matrice(n, p)

print("Résultat de l'addition :")
C = add_matrice(A, B)
if C:
    print_matrice(C)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.