Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
3.8 kB
7
Indexable
Never
def p_matriks1():
    baris = int(input('masukkan jumlah baris: '))
    kolom = int(input('masukkan jumlah kolom: '))

    matrix1 = []
    for i in range (kolom) :
        bar= []
        for j in range (baris):
            bar.append (int(input(f'masukkan elemen matriks [{i+1} {j+1}]: ')))
        matrix1.append(bar)
    for i in range(baris):
        for j in range(kolom):
            print("matriks pertama anda adalah", 
                matrix1  [j][i], end='\t\n')

    return matrix1

def p_matriks2():
    baris = int(input('masukkan jumlah baris: '))
    kolom = int(input('masukkan jumlah kolom: '))

    matrix2 = []
    for i in range (kolom) :
        bar= []
        for j in range (baris):
            bar.append (int(input(f'masukkan elemen matriks [{i+1} {j+1}]: ')))
        matrix2.append(bar)
    for i in range(baris):
        for j in range(kolom):
            print( ("Matriks kedua anda adalah"), matrix2  [j][i], end='\t\n')

    return matrix2

def penjumlahan(matriks1) :
    print("Silahkan masukkan matriks kedua")
    print("Perhatian : ukuran matriks kedua harus 2x2")
    
    matriks2 = p_matriks2()
   
    hasil_jumlah = []
    for i in range(len(matriks1)) :
        jumlah = []
        for j in range(len(matriks2)) :
            nilai = matriks1[i][j] + matriks2[i][j]
            jumlah.append(nilai)
        hasil_jumlah.append(jumlah)

    for baris in hasil_jumlah :
        print("\t\n", end='') # error syntax - to =
        for kolom in baris :
            print(kolom,"\t", end='') # error syntax - to =
    print("\n")

    return hasil_jumlah

def pengurangan(matriks1) :
    print("Silahkan masukkan matriks kedua")
    print("Perhatian : ukuran matriks kedua harus 2x2")
    
    matriks2 = p_matriks2()
  
    hasil_jumlah = []
    for i in range(len(matriks1)) :
        jumlah = []
        for j in range(len(matriks2)) :
            nilai = matriks1[i][j] - matriks2[i][j]
            jumlah.append(nilai)
        hasil_jumlah.append(jumlah)

    for baris in hasil_jumlah :
        print("\t\n", end='') # typo = to -
        for kolom in baris :
            print(kolom,"\t", end='') # typo = to -
    print("\n")

    return hasil_jumlah

def perkalian(baris, kolom) :

    hasil_kali = []
    for i in range (len(baris)):
        kali = []
        for j in range (len(kolom)) :
            total = 0
            for i in range (len(baris)) :
                total += (baris[i][k]*kolom[k][j])
                print(baris[i][k],'*',kolom[k][j])
            kali.append(total)
        hasil_kali.append(kali)

    for baris in hasil_kali :
        print("\t", end-'')
        for kolom in baris :
            print(kolom,"\t", end-'')
    print("\n")

    hasil_kali


print("Program Operasi Matriks")
print("Silahkan masukkan matriks pertama")
matriks1 = p_matriks1()
print("type: ", matriks1)

# exit()

print("Silahkan Pilih Operasi Matriksnya")
print("1. Penjumlahan Matriks")
print("2. Pengurangan Matriks")
print("3. Perkalian Matriks")

opsi = int(input("Pilih 1/2/3 :"))

if opsi == 1:

    print("adalah", penjumlahan(matriks1) )

elif opsi == 2:

    # print("Silahkan masukkan matriks kedua")
    # print("Perhatian : ukuran matriks kedua harus 2x2")

    # matriks2 = p_matriks2()

    # print("hasil pengurangan antara")
    # print(matriks1)
    # print("dengan")
    # print(matriks2)

    print("adalah", pengurangan(matriks1) )

elif opsi == 3:

    # print("Silahkan masukkan matriks kedua")
    # print("Perhatian : matriks kedua harus memiliki 2 baris")
    
    matriks2 = p_matriks2() # from p_matriks
   
    print("hasil perkalian antara")
    print(matriks1)
    print("dengan")
    print(matriks2)

    print("adalah", perkalian() )
else :
    print ('Pilihan Tidak Tersedia')