Chapter 05 : Defining and Processing of Arrays

Example: 1, Page Number: 5.5

In [2]:
#Program to read 5 data items from input and store them in an array and display their sum

#Local definitions
a = []
sum = 0

for i in range(0,5):
    a.append(0)
print "Enter 5 integer numbers"    
for i in range(0,5):
    a[i] = eval(raw_input())
    
for i in range(0,5):    
    sum = sum + a[i] 
    
print "The sum of given numbers is : %d\n" %sum
Enter 5 integer numbers
120
45
69
390
45
The sum of given numbers is : 669

Example: 2 Page No. 5.7

In [3]:
#Program to accept 5 numbers and print whether the number is even or not

#Local Variable
a=[]

for i in range(5):
    a.append(0)
for i in range(0,5):
    i+1
    a[i]=eval(raw_input("Enter the %d number: "%(i+1))) 
    print "\n"
for i in range (0,5):
    if a[i]%2==0:
        print "%d Number is Even"%a[i]
    else:
        print "%d Number is Odd"%a[i]
Enter the 1 number: 17


Enter the 2 number: 38


Enter the 3 number: 79


Enter the 4 number: 80


Enter the 5 number: 76


17 Number is Odd
38 Number is Even
79 Number is Odd
80 Number is Even
76 Number is Even

Example: 3 Page No. 5.8

In [1]:
#Program to illustrate how a character array can be used.

#Local definition
word=['F', 'R', 'I', 'E', 'N', 'D', 'S']

for i in range(0,7):
    word.append(0)
for i in range(0,7):
    print word[i],
F R I E N D S

Example: 4, Page Number: 5.8

In [5]:
#Program to sort the given strings alphabetically

b = raw_input("Enter the text to sort : ")
print "Sorted Text are : ",''.join(sorted(b, key=lambda v: (v.upper(), v[0].islower())))
Enter the text to sort : VRBPublishers
Sorted Text are :  BbehilPRrssuV

Example: 5, Page Number: 5.10

In [6]:
#Program using two dimension array

#Local definition 
stud = [[0 for i in xrange(0,4)] for i in xrange(0,4)] #stud is a array name with 4 rows and 2 columns

for i in range(0,4):
    print "Enter the %d Student roll no and Mark:" %i
    stud[i][1], stud[i][2] = input()
    
for i in range(0,4):
    print "%d Student roll no %d mark %d" %(i, stud[i][1], stud[i][2])
Enter the 0 Student roll no and Mark:
3977,80
Enter the 1 Student roll no and Mark:
17776,95
Enter the 2 Student roll no and Mark:
6682,82
Enter the 3 Student roll no and Mark:
6683,85
0 Student roll no 3977 mark 80
1 Student roll no 17776 mark 95
2 Student roll no 6682 mark 82
3 Student roll no 6683 mark 85

Example: 6, Page Number: 5.12

In [7]:
#Program to illustrate Matrix Multiplication

a = [[0 for i in xrange(0,5)] for i in xrange(0,5)]
b = [[0 for i in xrange(0,5)] for i in xrange(0,5)]
c = [[0 for i in xrange(0,5)] for i in xrange(0,5)]

while True:
    r1, c1 = input("Enter the size of the matrix A....")
    r2, c2 = input("Enter the size of the matrix B....")
    if c1 == r2:
        print "Enter matrix A elements..."
        for i in range (0,r1):
            for j in range (0,c1):
                a[i][j] = input()
            
        print "Enter matrix B elements..."
        for i in range (0,r2):
            for j in range (0,c2):
                b[i][j] = input()
            
        for i in range(0,r1):
            for j in range(0,c1):
                c[i][j] = 0
                for k in range(0,c1):
                    c[i][j] += a[i][k] * b[k][j]
                
        print "The resultant matrix is...."
        for i in range(0,r1):
            for j in range(0,c1):
                print "%d\t" %c[i][j],
            print "\n"
        break        
    else:
        print "Multiplication is not possible"
Enter the size of the matrix A....3,3
Enter the size of the matrix B....2,2
Multiplication is not possible
Enter the size of the matrix A....3,3
Enter the size of the matrix B....3,3
Enter matrix A elements...
2
2
2
2
2
2
2
2
2
Enter matrix B elements...
3
3
3
3
3
3
3
3
3
The resultant matrix is....
18	18	18	

18	18	18	

18	18	18	

Example: 7, Page Number: 5.13

In [8]:
#Program to print two dimensional array by row by row

arr = [[0 for i in xrange(0,4)] for i in xrange(0,4)]

for i in range(0,3):
    for j in range(0,4):
        arr[i][j] = (i * 4) + j + 1
        
        
print "Printing array contents :"
for i in range(0,3):
   for j in range(0,4):
        print "%3d" %arr[i][j],
   print "\n"   
Printing array contents :
  1   2   3   4 

  5   6   7   8 

  9  10  11  12 

Example: 8, Page Number: 5.14

In [9]:
#Program to add and display the results of 5 nos

#Function definition  
def add(x,ar):
    sum = 0
    for i in range(0,5):
        sum += ar[i]
    print "Sum is ... %d" %sum   
    
a = []
n = 5
for i in range(0,5):
    a.append(0)
print "Enter 5 values"
for i in range(0,5):
    a[i] = input()
add(n,a) #function call
Enter 5 values
10
20
30
40
50
Sum is ... 150

Example: 9, Page Number: 5.15

In [10]:
#Program for capture and store n values into array and print them

#Function definition 
def arr(a = []):
    for i in range(0,5):
        print "Value in array %d\n" %a[i]
        
a = []
for i in range(0,5):
    a.append(0)
for i in range(0,5):
    a[i] = i
arr(a)  #Function call
Value in array 0

Value in array 1

Value in array 2

Value in array 3

Value in array 4

Example: 10, Page Number: 5.16

In [11]:
#Program to print the character string from the array

name = "LAK"
print "%s" %name
LAK

Example: 11, Page Number: 5.17

In [12]:
#Program for explaining the working of 4 dimensional array

#Local definition
array_4d = [[[[0 for i in xrange(0,3)] for i in xrange(0,3)]for i in xrange(0,3)] for i in xrange(0,3)]

#looping statements 
for a in range(0,3):
    for b in range(0,3):
        for c in range(0,3):
            for d in range(0,3):
                array_4d[a][b][c][d] = a + b + c + d
#Result                
for a in range(0,3)                :
    print "\n"
    for b in range(0,3):
        for c in range(0,3):
            for d in range(0,3):
                print "%3d" %array_4d[a][b][c][d],
            print "\n"    

  0   1   2 

  1   2   3 

  2   3   4 

  1   2   3 

  2   3   4 

  3   4   5 

  2   3   4 

  3   4   5 

  4   5   6 



  1   2   3 

  2   3   4 

  3   4   5 

  2   3   4 

  3   4   5 

  4   5   6 

  3   4   5 

  4   5   6 

  5   6   7 



  2   3   4 

  3   4   5 

  4   5   6 

  3   4   5 

  4   5   6 

  5   6   7 

  4   5   6 

  5   6   7 

  6   7   8