Chapter 06: Arrays and Subscribed Variables

Example 1 , Page number: CP-122

In [1]:
# Program to find the sum of n numbers using array
# Variable declaration

n = 5
x = [36,45,52,44,62]

print "How many integers ?",n
print "Enter the 1th value :",x[0]
print "Enter the 2th value :",x[1]
print "Enter the 3th value :",x[2]
print "Enter the 4th value :",x[3]
print "Enter the 5th value :",x[4]

summ = 0 

# Calculation of sum of numbers
for i in x:
    summ = summ + i
    

print "Sum of all integers =",summ
    
How many integers ? 5
Enter the 1th value : 36
Enter the 2th value : 45
Enter the 3th value : 52
Enter the 4th value : 44
Enter the 5th value : 62
Sum of all integers = 239

Example 2 , Page number: CP-123

In [2]:
# Program to find the biggest of n numbers
# Variable declaration

n = 5
x = [25,-228,0,185,36]

print "How many numbers ?",n
print "Enter all those numbers"
for a in x:
    print a,
print     

big = x[0]


for i in x:
    if (i > big):
        big = i


print "%d is the biggest number" % big        
How many numbers ? 5
Enter all those numbers
25 -228 0 185 36
185 is the biggest number

Example 3 , Page number: Cp-124

In [3]:
# Program to find the arithmetic mean, variance and standard deviation
# Variable declaration

n = 6
x = [3.1,3.8,3.6,4.0,3.4,3.8]
summ = 0
vsum = 0

print "How many values ?",n
print "Enter all values in the list "
for a in x:
    print a,
print     

# Loop to find sum of all values

for i in x:
    summ = summ + i

xbar = summ / n

# Loop to find the numerator vsum to find variance

for i in x:
    vsum = vsum + (i - xbar) * (i - xbar)


sigmax = vsum / n
sd = sigmax ** 0.5

print "Arithmetic mean = %0.3f" % xbar
print "Variance = %0.3f " % sigmax
print "Standard deviation = %0.3f" % sd
How many values ? 6
Enter all values in the list 
3.1 3.8 3.6 4.0 3.4 3.8
Arithmetic mean = 3.617
Variance = 0.088 
Standard deviation = 0.297

Example 4 , Page number: CP-126

In [4]:
# Program to calculate mean of marks and print list of marks greater than mean
# Variable declaration

n = 5
x = [58,63,68,54,48]
summ = 0 # Used summ instead of sum since it was a inbuilt function
i = 0

print "How many students ?",n
print "Enter all the marks "
for a in x:
    print a,
print     

for i in x:
    summ = summ + i

mean = float(summ) / n

print "Mean = %0.2f" % mean
print "Marks greater than mean :",

i = 0
for i in x:
    if (i > mean):
        print i,

        
How many students ? 5
Enter all the marks 
58 63 68 54 48
Mean = 58.20
Marks greater than mean : 63 68

Example 5 , Page number: CP-127

In [5]:
# Program to find sum of all positive and negative numbers and to find out which is larger in magnitude
# Variable declaration
import math

n = 6
x = [8,-12,-16,12,-9,5]
psum = 0
nsum = 0

print "How many values ?",n
print "Enter all values in the list"
for i in x:
    print i,
print     

# Loop to calculate sum of positive and negative values

for i in x:
    if i > 0:
        psum = psum + i
    else:
        nsum = nsum + i

print "Sum of positive values = %0.2f" % psum
print "Sum of negative values = %0.2f" % nsum

if (psum > abs(nsum)):
    print "Positive sum is greater in magnitude"
else:
    print "Negative sum is greater in magnitude"

diff = abs(psum) - abs(nsum)
print "Difference in magnitude = %0.2f" % abs(diff)
How many values ? 6
Enter all values in the list
8 -12 -16 12 -9 5
Sum of positive values = 25.00
Sum of negative values = -37.00
Negative sum is greater in magnitude
Difference in magnitude = 12.00

Example 6 , Page number: CP-129

In [6]:
# Program to sort n numbers in ascending order
# Variable declaration

n = 4
x = [32,-10,20,5]
i = 0


print "How many numbers ?",n
print "Enter the list of 4 numbers"
for a in x:
    print a,
print

# Loop to arrange the numbers in ascending order

while i < n-1:
    j = i + 1
    while j < n:
        if x[i] > x[j]:
            temp = x[i]
            x[i] = x[j]
            x[j] = temp
        j = j + 1
    i = i + 1

print "Numbers in ascending order "

for a in x:
    print a,
How many numbers ? 4
Enter the list of 4 numbers
32 -10 20 5
Numbers in ascending order 
-10 5 20 32

Example 7 , Page number: CP-131

In [7]:
# Program to search the key value and to print it if the search is successful
# Variable declaration

n = 6
x = [6,-2,8,3,13,10]
s = 3

print "How many values in the list ?",n
print "Enter all values in the list"
for i in x:
    print i,
print     
print "Enter the key value to be searched :",s

# loop to search key value in the list

for i in range(n):
    if s == x[i]:
        print s," is available in",i+1,"th location"
How many values in the list ? 6
Enter all values in the list
6 -2 8 3 13 10
Enter the key value to be searched : 3
3  is available in 4 th location

Example 8 , Page number: CP-133

In [8]:
# Program to sort n numbers using bubble sort and find number of exchanges and passes
# Variable declaration

n = 4
x = [6,-2,8,3]
exchng = 0

print "How many numbers?",n
print "Enter all the numbers in the list"
for i in x:
    print i,

print

for i in range(0,n-1):
    for j in range(0,n-i-1):
        if x[j] > x[j+1]:
            temp = x[j]
            x[j] = x[j+1]
            x[j+1] = temp
            exchng = exchng + 1
            

print "The sorted list is"
for i in x:
    print i,

print     

print "Sorted in",n-1,"passes and",exchng,"exchanges"
How many numbers? 4
Enter all the numbers in the list
6 -2 8 3
The sorted list is
-2 3 6 8
Sorted in 3 passes and 3 exchanges

Example 9 , Page number: CP-135

In [5]:
# Program to add two matrices
# Variable declaration

a = [[2,-2],
     [0,4]]
b = [[6,2],
     [4,-5]]

c = [[0,0],
     [0,0]]

m = 2
n = 2

print "How many rows and columns ?",m,n
print "Enter A matrix"
for i in range(m):
    for j in range(n):
        print a[i][j],
    print 

print "Enter B matrix"
for i in range(m):
    for j in range(n):
        print b[i][j],
    print 

# Loop to add two matrices

for i in range(m):
    for j in range(n):
        c[i][j] = a[i][j] + b[i][j]


print "Resultant matrix is"
for i in range(m):
    for j in range(n):
        print c[i][j],
    print 
How many rows and columns ? 2 2
Enter A matrix
2 -2
0 4
Enter B matrix
6 2
4 -5
Resultant matrix is
8 0
4 -1

Example 10 , Page number: CP-136

In [6]:
# Program to multiply two matrices
# Variable declaration

m = 2
n = 2
l = 2
a = [[2,-2],
     [0,4]]
b = [[6,2],
     [4,-5]]
c = [[0,0],
     [0,0]]

print "Enter order of A matrix :",m,n
print "Enter A matrix"
for i in range(m):
    for j in range(n):
        print a[i][j],
    print 
    
print "Enter order of B matrix :",m,n
print "Enter B matrix"
for i in range(m):
    for j in range(n):
        print b[i][j],
    print 

# Loop to multiply two matrices
# iterate through rowa of A
for i in range(m):
    # iterate through columns of B
    for j in range(l):
        c[i][j] = 0
        # iterate through rows of B
        for k in range(n):
            c[i][j] = c[i][j] + a[i][k] * b[k][j]


print "Resultant matrix is"

for i in range(m):
    for j in range(n):
        print c[i][j],
    print 
    
Enter order of A matrix : 2 2
Enter A matrix
2 -2
0 4
Enter order of B matrix : 2 2
Enter B matrix
6 2
4 -5
Resultant matrix is
4 14
16 -20

Example 11 , Page number: CP-138

In [7]:
# Program to find and print the transpose of the matrix
# Variable declaration

m = 2
n = 3
a = [[-3,6,0],
     [3,2,8]]
at = [[0,0],
      [0,0],
      [0,0]]

print "Enter order of the matrix :",m,n
print "Enter the matrix values"
for i in range(m):
    for j in range(n):
        print a[i][j],
    print 

# Loop to calculate transpose

for i in range(m):
    for j in range(n):
        at[j][i] = a[i][j]

print "The transposed matrix is "
for i in range(n):
    for j in range(m):
        print at[i][j],
    print 
    
Enter order of the matrix : 2 3
Enter the matrix values
-3 6 0
3 2 8
The transposed matrix is 
-3 3
6 2
0 8

Example 12 ,Page number: CP-139

In [8]:
# Program to check whether a given matrix is symmetric or not
# Variable declaration

m = 3
a = [[5,3,8],
     [3,1,-7],
     [8,-7,4]]

print "Enter order of the square matrix :",m
for i in range(m):
    for j in range(n):
        print a[i][j],
    print 

# Loop to check whether symmetric or not

for i in range(m):
    flag = 0
    for j in range(m):
        flag = 0
        if a[i][j] == a[j][i]:
            continue
        else:
            flag = 1

if flag == 0:
    print "The given matrix is a symmetric matrix"
else:
    print "The given matrix is not a symmetric matrix"
Enter order of the square matrix : 3
5 3 8
3 1 -7
8 -7 4
The given matrix is a symmetric matrix

Example 13 , Page number: CP-141

In [4]:
# Program to find the trace of a given square matrix
# Variable dclaration

m = 3
a = [[3,2,-1],
     [4,1,8],
     [6,4,2]]
summ = 0

print "Enter order of the square matrix :",m
print "Enter the matrix"
for i in range(m):
    for j in range(m):
        print a[i][j],    
    print 

# Loop to find trace
for i in range(m):
    summ = summ + a[i][i]

print "Trace of the matrix =",summ    
Enter order of the square matrix : 3
Enter the matrix
3 2 -1
4 1 8
6 4 2
Trace of the matrix = 6