Chapter 7: Arrays and Subscripted Variables

Example 1, Page Number : ASV-4

In [10]:
import sys

#Variable Initialization
n = int(raw_input("\nHow many integers ? "))
x = [0 for i in range(0,n)]

for i in range(0,n):
    x[i] = int(raw_input("\nEnter the %dth value : "%(i+1)))
    
#Processing
#sum is a predefined function in python. so sum1 is used
sum1 = 0
for i in range(0,n):
    sum1 = sum1 + x[i]
    
#Output
sys.stdout.write("\nSum of all integers = %d"%(sum1))
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 : ASV-5

In [11]:
import sys

#Variable initialization
n = int(raw_input("\nHow many numbers ? "))
sys.stdout.write("\nEnter all those numbers ")
x = [0 for i in range(0,n)]

for i in range(0,n):
    x[i] = int(raw_input(""))
    
#Processing
big = x[0]
for i in range(0,n):
    if x[i] > big:
        big = x[i]
        
#Output
sys.stdout.write("\n%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 : ASV-6

In [13]:
import sys
import math

#Variable Initialization
n = int(raw_input("\nHow many values ? "))
x = [0.0 for i in range(0,n)]

sys.stdout.write("\nEnter all values in the list\n")
for i in range(0,n):
    x[i] = float(raw_input(""))
    
#Processing
sum1 = 0
#Since sum is a predefined function in python, sum1 is used
for i in range(0,n):
    sum1 = sum1 + x[i]
xbar = sum1 / n
vsum = 0

for i in range(0,n):
    vsum = vsum + (x[i] - xbar) * (x[i] - xbar)
sigmax = vsum / n
sd = math.sqrt(sigmax)

#Output
sys.stdout.write("\nArithmetic mean = %0.3f"%(xbar))
sys.stdout.write("\nVariance = %0.3f"%(sigmax))
sys.stdout.write("\nStandard 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 : ASV-8

In [15]:
import sys

#Variable Initialization
n = int(raw_input("\nHow many students ? "))

sys.stdout.write("\nEnter all the marks \n")
for i in range(0,n):
    x[i] = int(raw_input(""))
    
#Processing
sum1 = 0.0
#Since sum is a predefined function in python, sum1 is used
for i in range(0,n):
    sum1 = sum1 + x[i]
    
mean = float(sum1/n)

#Output
sys.stdout.write("\nMean = %6.2f"%(mean))
sys.stdout.write("\n\nMarks greater than mean : ")

for i in range(0,n):
    if x[i] > mean:
        sys.stdout.write("%5d"%(x[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 : ASV-9

In [16]:
import sys
import math

#Variable Initialization
n = int(raw_input("\nHow many values ? "))

sys.stdout.write("\nEnter all values in the list \n")
for i in range(0,n):
    x[i] = float(raw_input(""))
    
psum = 0
nsum = 0

#Processing
for i in range(0,n):
    if x[i] > 0:
        psum = psum + x[i]
    else:
        nsum = nsum + x[i]
        
#Output
sys.stdout.write("\nSum of positive values = %0.2f"%(psum))
sys.stdout.write("\nSum of negative values = %0.2f"%(nsum))
if psum > math.fabs(nsum):
    sys.stdout.write("\nPositive sum is greater in magnitude")
else:
    sys.stdout.write("\nNegative sum is greater in magnitude")
    
diff = math.fabs(psum - math.fabs(nsum))
sys.stdout.write("\nDifference in magnitude = %0.2f"%(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 : ASV-11

In [18]:
import sys

#Variable Initialization
n = int(raw_input("How many numbers ? "))

sys.stdout.write("\nEnter the list of %d numbers\n"%(n))
for i in range(0,n):
    x[i] = int(raw_input(""))
    
#Processing
for i in range(0,n):
    for j in range(i+1,n):
        if x[i] > x[j]:
            temp = x[i]
            x[i] = x[j]
            x[j] = temp
            
#Output
sys.stdout.write("\nNumbers in ascending order\n")
for i in range(0,n):
    sys.stdout.write("%5d"%(x[i]))
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 : ASV-13

In [2]:
import sys

x = [0 for i in range(0,50)]

#Variable Initialization
n = int(raw_input("\nHow many values in the list ? "))

sys.stdout.write("\nEnter all values in the list\n")
for i in range(0,n):
    x[i] = int(raw_input(""))
s = int(raw_input("\nEnter the key value to be searched : "))

#Processing
for i in range(0,n):
    if s == x[i]:
        sys.stdout.write("\n%d is available in %dth location"%(s,i+1))
        
if i == n+1:
    sys.stdout.write("\nThe key value %d is not present in the list"%(s))
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 4th location

Example 8, Page Number : ASV-15

In [22]:
import sys

#Variable Initialization
exchng = 0
n = int(raw_input("\nHow many numbers ? "))
sys.stdout.write("\nEnter all numbers in the list\n")
for i in range(0,n):
    x[i] = int(raw_input(""))

#Processing
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
            
#Output
sys.stdout.write("\nThe sorted list is\n")
for i in range(0,n):
    sys.stdout.write("%5d"%(x[i]))
    
sys.stdout.write("\nSorted in %d passes and %d exchanges"%(n-1,exchng))
How many numbers ? 4

Enter all 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 : ASV-17

In [33]:
import sys

#Variable declaration and initialization

m = int(raw_input("\nHow many rows and columns ? "))
n = int(raw_input(""))

a = [[0 for i in range(0,m)] for j in range(0,n)]
b = [[0 for i in range(0,m)] for j in range(0,n)]
c = [[0 for i in range(0,m)] for j in range(0,n)]

sys.stdout.write("\nEnter A matrix\n")
for i in range(0,m):
    for j in range(0,n):
        a[i][j] = int(raw_input(""))
        
sys.stdout.write("\nEnter B matrix\n")
for i in range(0,m):
    for j in range(0,n):
        b[i][j] = int(raw_input(""))
        
#Processing
for i in range(0,m):
    for j in range(0,n):
        c[i][j] = a[i][j] + b[i][j]
        
#Output
sys.stdout.write("\nResultant matrix is \n")
for i in range(0,m):
    for j in range(0,n):
        sys.stdout.write("%6d"%(c[i][j]))
    sys.stdout.write("\n")
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 : ASV-18

In [34]:
import sys

#Variable declaration and initialization

m = int(raw_input("\nEnter order of A matrix : "))
n = int(raw_input(""))

a = [[0 for i in range(0,m)] for j in range(0,n)]
b = [[0 for i in range(0,m)] for j in range(0,n)]
c = [[0 for i in range(0,m)] for j in range(0,n)]

sys.stdout.write("\nEnter A matrix\n")
for i in range(0,m):
    for j in range(0,n):
        a[i][j] = int(raw_input(""))

n = int(raw_input("\nEnter order of B matrix : "))
l = int(raw_input(""))

sys.stdout.write("\nEnter B matrix\n")
for i in range(0,n):
    for j in range(0,l):
        b[i][j] = int(raw_input(""))
        
#Processing
for i in range(0,n):
    for j in range(0,l):
        c[i][j] = 0
        for k in range(0,n):
            c[i][j] = c[i][j] + a[i][k] * b[k][j]
            
#Output
sys.stdout.write("\nResultant matrix is \n")
for i in range(0,m):
    for j in range(0,l):
        sys.stdout.write("%6d"%(c[i][j]))
    sys.stdout.write("\n")
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 : ASV-20

In [35]:
import sys

#Variable declaration and initialization

m = int(raw_input("\nEnter order of the matrix : "))
n = int(raw_input(""))

a = [[0 for i in range(0,10)] for j in range(0,10)]
at = [[0 for i in range(0,10)] for j in range(0,10)]

sys.stdout.write("\nEnter the matrix values\n")
for i in range(0,m):
    for j in range(0,n):
        a[i][j] = int(raw_input(""))

#Processing
for i in range(0,m):
    for j in range(0,n):
        at[j][i] = a[i][j]
        
#Output
sys.stdout.write("\nThe transposed matrix is\n")
for i in range(0,n):
    for j in range(0,m):
        sys.stdout.write("%6d"%(at[i][j]))
    sys.stdout.write("\n")
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 : ASV-21

In [38]:
import sys

#Variable declaration and initialization

m = int(raw_input("\nEnter order of the square matrix : "))

a = [[0 for i in range(0,10)] for j in range(0,10)]

sys.stdout.write("\nEnter the matrix\n")
for i in range(0,m):
    for j in range(0,m):
        a[i][j] = int(raw_input(""))

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

#Output
if flag == 0:
    sys.stdout.write("\nThe given matrix is a symmetric matrix")
else:
    sys.stdout.write("\nThe given matrix is not a symmetric matrix")
Enter order of the square matrix : 3

Enter the matrix
5
3
8
3
1
-7
8
-7
4

The given matrix is a symmetric matrix

Example 13, Page Number : ASV-23

In [39]:
import sys

#Variable declaration and initialization

m = int(raw_input("\nEnter order of the square matrix : "))

a = [[0 for i in range(0,10)] for j in range(0,10)]

sys.stdout.write("\nEnter the matrix\n")
for i in range(0,m):
    for j in range(0,m):
        a[i][j] = int(raw_input(""))

#Processing 
sum1 = 0
#since sum is a predefined function in python, sum1 is used
for i in range(0,m):
    sum1 = sum1 + a[i][i]
    
#Output
sys.stdout.write("\nTrace of the matrix = %d"%(sum1))
Enter order of the square matrix : 3

Enter the matrix
3
2
-1
4
1
8
6
4
2

Trace of the matrix = 6
In [ ]: