Rabu, 18 Januari 2017

MEmbangun Array Python



import random

def loop(start, stop, step):
    i = start
    while i < stop:
        yield i
        i += step

def cetakm(nama,M):
    print("Array ",nama)
    b=len(M)
    c=len(M[0])
    for i in  loop(0,b,1):
        gab=""
        for j in  loop(0,c,1):
            gab+=str(M[i][j])+"\t"
        print(gab)
    print("++++++++++++++++++++++++++++")  

def penjumlahan(M1,M2,ops):
    print ("Operasi Penjumlahan")
    b=len(M)
    c=len(M[0])
    H=[]
    for i in  loop(0,b,1):
        T=[]
        for j in  loop(0,c,1):
            tmp=M1[i][j]+M2[i][j]
            T.append(tmp)
        H.append(T)
       
    cetakm("HasilM",H)


##################################################  
print("+++++++++++++++++++++++++++++++")  
print("+++ BELAJAR MATRIK LP2MARAY +++")  
print("+++++++++++++++++++++++++++++++")  

   
M=[]
N=[]



b=6
c=5

for i in loop(0,b,1):
    T1=[]
    T2=[]
    for j in loop(0,c,1):
        T1.append(random.randint(0,9))
        T2.append(random.randint(0,9))
    M.append(T1)
    N.append(T2)


b=len(M)
c=len(M[0])
print("b:",b)
print("c:",c)
   
cetakm("Matrix 1 ",M)
cetakm("Matrix 2 ",N)

H=M+N

penjumlahan(M,N,"Penjumlahan")

Tidak ada komentar:

Posting Komentar