วันศุกร์ที่ 23 ตุลาคม พ.ศ. 2558

Lab 5 - Array

def setup():
    n = [ 16,48,64,-15,64,64,-69,30,-13,29]
    showIndex(n)
    print("\nMaximum =",find_maxNum(n))
    print("First Index Maximum =",find_firstMaxNum(n))
    print("Last Index Maximum =",find_lastMaxNum(n))
    print("Sum of Value =",sumValue(n))
    print("Sum of Positive Value =",sumPositiveValue(n))
    print("Count Number of Positive Value =",countPositiveNumber(n))
    print("Average of Value =",averageValue(n),"\n\n")
    showValue_afterChange(n)

   
def showIndex(n):
    i = 0
    while(i < len(n)):
        print("Index",i,"=",n[i])
        i += 1
   
def find_maxNum(n):
    i = 0
    maxNum = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
        i += 1
    return maxNum

def find_firstMaxNum(n):
    i = 0
    maxNum = 0
    indexMax = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
            indexMax = i
        i += 1
    return indexMax

def find_lastMaxNum(n):
    i = 0
    maxNum = 0
    indexMax = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
        elif(n[i]== maxNum):
            indexMax = i
        i += 1
    return indexMax

def sumValue(n):
    i = 0
    sum_value = 0
    while(i < len(n)):
        sum_value += n[i]
        i += 1
    return sum_value

def sumPositiveValue(n):
    i = 0
    sum_positiveValue = 0
    while(i < len(n)):
        if(n[i] >= 0):
            sum_positiveValue += n[i]
        i += 1
    return sum_positiveValue

def countPositiveNumber(n):
    i = 0
    count_positive = 0
    while(i < len(n)):
        if(n[i] >= 0):
            count_positive += 1
        i += 1
    return count_positive

def averageValue(n):
    i = 0
    sum_value = 0
    while(i < len(n)):
        sum_value += n[i]
        i += 1
    average = sum_value/len(n)
    return average

def showValue_afterChange(n):
    change = -50
    i = 0
    if(change >= 0):
        print("Increase =",change)
    else:
        print("Decrease =",change)
    while(i < len(n)):
        n[i] += change
        print("Index",i,"=",n[i])
        i += 1

   
setup()

ไม่มีความคิดเห็น:

แสดงความคิดเห็น