วันอาทิตย์ที่ 20 กันยายน พ.ศ. 2558

LAB 5 find Sum of value in array and show value any array

def findSumAndValueIndex():
   A = [1,2,3,4,5,6,7,8,9,10]
   sumA = 0
   index = 0
 
   while(index<len(A)):
      sumA = sumA + A[index]
      print("A[",index,"] is",A[index])
      index = index + 1
   print ("sum values in array",sumA)
   print ("How many index is ",len(A))
 
findSumAndValueIndex()

ผลลัพธ์
A[ 0 ] is 1
A[ 1 ] is 2
A[ 2 ] is 3
A[ 3 ] is 4
A[ 4 ] is 5
A[ 5 ] is 6
A[ 6 ] is 7
A[ 7 ] is 8
A[ 8 ] is 9
A[ 9 ] is 10
sum values in array 55
How many index is  10

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

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