Chapter 6: Arrays and pointers

C23ARA1, Page number:482

In [1]:
s_name="Tri Star University"
scores = [88.7,90.4,76.0,97.0,100.0,86.7]   #integer array
average=0.0
for ctr in range(0,6,1):                        #computes total of scores
    average+=scores[ctr]
average/=float(6)                               #computes the average
print "At ",s_name,", your class average is ",'%.2f' %average,"\n"
At  Tri Star University , your class average is  89.80 

C23ARA2, Page number:483

In [2]:
def pr_scores(scores):
    print "Here are your scores:\n"
    for ctr in range(0,6,1):
        print '%.2f' %scores[ctr],"\n"        
s_name="Tri Star University"
scores = [88.7,90.4,76.0,97.0,100.0,86.7]   #integer array
average=0.0
pr_scores(scores)           #Function call to print scores
for ctr in range(0,6,1):                        #computes total of scores
    average+=scores[ctr]
average/=float(6)                               #computes the average
print "At ",s_name,", your class average is ",'%.2f' %average,"\n"
Here are your scores:

88.70 

90.40 

76.00 

97.00 

100.00 

86.70 

At  Tri Star University , your class average is  89.80 

C23ARA3, Page number:485

In [3]:
CLASS_NUM=6
def pr_scores(scores):                              #Function definition 
    print "Here are your scores:\n"
    for ctr in range(0,CLASS_NUM,1):
        print '%.2f' %scores[ctr],"\n"        
s_name="Tri Star University"
scores = [88.7,90.4,76.0,97.0,100.0,86.7]           #Integer array
average=0.0
pr_scores(scores)                                   #Function call to print scores
for ctr in range(0,CLASS_NUM,1):                    #Computes total of scores
    average+=scores[ctr]
average/=float(6)                                   #Computes the average
print "At ",s_name,", your class average is ",'%.2f' %average,"\n"
Here are your scores:

88.70 

90.40 

76.00 

97.00 

100.00 

86.70 

At  Tri Star University , your class average is  89.80 

C23ARA4, Page number:487

In [4]:
NUM_TEMPS=10
temps=[]
#Adding values into temps variable
temps.append(78.6)
temps.append(82.1)
temps.append(79.5)
temps.append(75.0)
temps.append(75.4)
temps.append(71.8)
temps.append(73.3)
temps.append(69.5)
temps.append(74.1)
temps.append(75.7)
#Print the temperatures
print "Daily temperatures for the last ",NUM_TEMPS,"days:\n"
for ctr in range(0,NUM_TEMPS,1):
    print '%.2f' %temps[ctr],"\n"
Daily temperatures for the last  10 days:

78.60 

82.10 

79.50 

75.00 

75.40 

71.80 

73.30 

69.50 

74.10 

75.70 

C23TOT, Page number:488

In [2]:
NUM=8
nums=[]
total=0
for ctr in range(0,NUM,1):
    print "Please enter the next number...",
    nums.append(input())
    total+=nums[ctr]
#Prints the sum of eight values 
print "The total of the numbers is ",total,"\n"
 Please enter the next number...1
 Please enter the next number...2
 Please enter the next number...3
 Please enter the next number...4
 Please enter the next number...5
 Please enter the next number...6
 Please enter the next number...7
 Please enter the next number...8
 The total of the numbers is  36 

C23SAL, Page number:489

In [ ]:
NUM=12
sales=[] 
print "Please enter the twelve monthly sales values\n"
#Fill the array with input values entered by the user
for ctr in range(0,NUM,1):
    print "What are sales for month number ",ctr+1,"?\n"
    sales.append(input())
#for ctr in range(0,25,1):
#    print "\n"                      #Clears the screen
print "\n\n*** Sales Printing Program ***\n"
print "Prints any sales from the last ",NUM," months\n"
ans='Y'
while ans=='Y':
    print "For what month (1-",NUM,") do you want to see a sales value? " 
    req_month=input()
    print "\nMonth ",req_month,"'s sales are",'%.2f' %sales[req_month-1]
    print "\nDo you want to see another (Y/N)? "
    ans=raw_input().upper()
Please enter the twelve monthly sales values

What are sales for month number  1 ?

363.25
What are sales for month number  2 ?

433.22
What are sales for month number  3 ?

652.36
What are sales for month number  4 ?

445.52
What are sales for month number  5 ?

123.45
What are sales for month number  6 ?

780.2
What are sales for month number  7 ?

125.36
What are sales for month number  8 ?

425.15
What are sales for month number  9 ?

325.96
What are sales for month number  10 ?

109.75
What are sales for month number  11 ?

123.65
What are sales for month number  12 ?

253.84


*** Sales Printing Program ***

Prints any sales from the last  12  months

For what month (1- 12 ) do you want to see a sales value? 
2

Month  2 's sales are 433.22

Do you want to see another (Y/N)? 
y
For what month (1- 12 ) do you want to see a sales value? 
5

Month  5 's sales are 123.45

Do you want to see another (Y/N)? 
n

C24HIGH, Page number:496

In [4]:
SIZE=15     #Maximum size of array
ara=[None]*SIZE         #Empty Array declaration with maximum size
ara=[5,2,7,8,36,4,2,86,11,43,22,12,45,6,85]
high_val=ara[0]             #initialize wit first array element
for ctr in range(1,SIZE,1):
    if ara[ctr]>high_val:       #Compares with rest of the elements in the array 
        high_val=ara[ctr]       #Stores higher value in high_val
print "The highest number in the list is ",high_val,".\n"
The highest number in the list is  86 .

C24HILO, Page number:498

In [5]:
#Maximum size of array
SIZE=15                     
#Initialize empty array
ara=[None]*SIZE         
#Fill array with random numbers from 0 to 99
import random
for ctr in range(0,SIZE,1):
    ara[ctr]=random.randint(0,99) %100     
print "Here are the ",SIZE,"random numbers:\n" 
for ctr in range(0,SIZE,1):
    print ara[ctr],"\n"             #Prints the array    
print "\n\n"
#Initialize first element to both high_val and low_val
high_val=ara[0]         
low_val=ara[0]
for ctr in range(1,SIZE,1):
    if ara[ctr]>high_val:                           #Compares with rest of the elements in the array
        high_val=ara[ctr]                           #Stores higher valure in high_val
    if ara[ctr]<low_val:
        low_val=ara[ctr]                            #Stores lower valure in low_val
print "The highest number in the list is ",high_val,"\n"
print "The lowest number in the list is ",low_val,"\n"
Here are the  15 random numbers:

81 

96 

31 

1 

34 

53 

70 

9 

23 

89 

51 

73 

53 

85 

79 




The highest number in the list is  96 

The lowest number in the list is  1 

C24SERCH, Page number:499

In [1]:
MAX=100
def fill_parts(parts):        #Function assigns first five parts to array for testing
    parts[0]=12345
    parts[1]=24724
    parts[2]=54154
    parts[3]=73496
    parts[4]=83925
    return parts
parts=[None]*MAX
num_parts=5               #Beginning inventory count
fill_parts(parts)           #Fills the first five elements
search_part=0
while search_part != -9999:
    print "\n\nPlease type a part number...(-9999 ends program)",
    search_part=input()
    if search_part==-9999:
        break                                                     #Exits the loop if user wants    
    for ctr in range(0,num_parts,1):                   #Scans array to see whether part is in inventory
        if search_part==parts[ctr]:
            print "\nPart ",search_part,"is already in inventory"
            break
        else:
            if ctr==num_parts-1:
                parts[num_parts]=search_part        #If not then it is added to end of the array
                num_parts+=1
                print search_part,"was added to inventory\n"
                break 

Please type a part number...(-9999 ends program)34234
 34234 was added to inventory



Please type a part number...(-9999 ends program)83925
 
Part  83925 is already in inventory


Please type a part number...(-9999 ends program)52786
 52786 was added to inventory



Please type a part number...(-9999 ends program)-9999

C24SORT1, Page number:504

In [1]:
MAX=10
#Fill array with random numbers from 0 to 99
def fill_array(ara):
    import random
    for ctr in range(0,MAX,1):
        ara[ctr]=random.randint(0,99) %100
#Prints the array ara[]
def print_array(ara):
    for ctr in range(0,MAX,1):
        print ara[ctr],"\n"
#Sorts the array
def sort_array(ara):
    for ctr1 in range(0,MAX-1,1):
        for ctr2 in range(ctr1+1,MAX,1):
            if ara[ctr1]>ara[ctr2]:                 #Swap if this part is not in order
                temp=ara[ctr1]                  #Temporary variable to swap
                ara[ctr1]=ara[ctr2]
                ara[ctr2]=temp
ara=[None]*MAX
fill_array(ara)
print "Here are the unsorted numbers:\n"
print_array(ara)                                        #Prints the unsorted array
sort_array(ara)                                         #Sorts the array in ascending order
print "\n\nHere are the sorted numbers:\n"
print_array(ara)                                        #Prints the sorted array
Here are the unsorted numbers:

68 

41 

53 

40 

69 

65 

64 

48 

87 

18 



Here are the sorted numbers:

18 

40 

41 

48 

53 

64 

65 

68 

69 

87 

C24SORT2, Page number:506

In [2]:
MAX=10
#Fill array with random numbers from 0 to 99
def fill_array(ara):
    import random
    for ctr in range(0,MAX,1):
        ara[ctr]=random.randint(0,99) %100
#Prints the array ara[]
def print_array(ara):
    for ctr in range(0,MAX,1):
        print ara[ctr],"\n"
#Sorts the array
def sort_array(ara):
    for ctr1 in range(0,MAX-1,1):
        for ctr2 in range(ctr1+1,MAX,1):
            if ara[ctr1]<ara[ctr2]:                 #Swap if this part is not in order
                temp=ara[ctr1]                  #Temporary variable to swap
                ara[ctr1]=ara[ctr2]
                ara[ctr2]=temp

ara=[None]*MAX
fill_array(ara)
print "Here are the unsorted numbers:\n"
print_array(ara)                                        #Prints the unsorted array
sort_array(ara)                                         #Sorts the array in descending order
print "\n\nHere are the sorted numbers:\n"
print_array(ara)                                        #Prints the newly sorted array
Here are the unsorted numbers:

40 

22 

4 

78 

20 

45 

60 

93 

85 

22 



Here are the sorted numbers:

93 

85 

78 

60 

45 

40 

22 

22 

20 

4 

C25DISK1, Page number:533

In [9]:
#Two dimensional array declaration
disks= [[[] for ni in range(4)] for mi in range(2)]
#Assign values to the 2Darray
disks[0][0]=2.39
disks[0][1]=2.75
disks[0][2]=3.29
disks[0][3]=3.59
disks[1][0]=1.75
disks[1][1]=2.19
disks[1][2]=2.69
disks[1][3]=2.95
#Print the values in the array
for row in range(0,2,1):
    for col in range(0,4,1):
        print "$",'%.2f'%disks[row][col],"\n",
$ 2.39 
$ 2.75 
$ 3.29 
$ 3.59 
$ 1.75 
$ 2.19 
$ 2.69 
$ 2.95 

C25DISK2, Page number:534

In [8]:
#Two dimensional array declaration
disks= [[[] for ni in range(4)] for mi in range(2)]
#Assign values to the 2Darray
disks[0][0]=2.39
disks[0][1]=2.75
disks[0][2]=3.29
disks[0][3]=3.59
disks[1][0]=1.75
disks[1][1]=2.19
disks[1][2]=2.69
disks[1][3]=2.95
#Print the values in the array
for row in range(0,2,1):
    for col in range(0,4,1):
        print "$",'%.2f'%disks[row][col],"\t",
    print "\n"
$ 2.39 	$ 2.75 	$ 3.29 	$ 3.59 	

$ 1.75 	$ 2.19 	$ 2.69 	$ 2.95 	

C25DISK3, Page number:535

In [10]:
#Two dimensional array declaration
disks= [[[] for ni in range(4)] for mi in range(2)]
#Assign values to the 2Darray
disks[0][0]=2.39
disks[0][1]=2.75
disks[0][2]=3.29
disks[0][3]=3.59
disks[1][0]=1.75
disks[1][1]=2.19
disks[1][2]=2.69
disks[1][3]=2.95
#Print the column titles.
print "\tSingle-sided\tDouble-sided\tSingle-sided\tDouble-sided"
print "\tDouble-density\tDouble-density\tHigh-density\tHigh-density"
#Print the prices
for row in range(0,2,1):
    if row==0:
        print "3-1/2\"\t",
    else:
        print "5-1/2\"\t",
    for col in range(0,4,1):
        print "$",'%.2f'%disks[row][col],"\t\t",
    print "\n"
	Single-sided	Double-sided	Single-sided	Double-sided
	Double-density	Double-density	High-density	High-density
3-1/2"	$ 2.39 		$ 2.75 		$ 3.29 		$ 3.59 		

5-1/2"	$ 1.75 		$ 2.19 		$ 2.69 		$ 2.95 		

C26SWAP, Page number:551

In [ ]:
#Function definition
def swap_them(num1,num2):
    num1,num2=num2,num1
    return num1,num2
#Variable declaration
i=10
j=20
print "\nBefore swap, i is ",i,"and j is ",j,"\n"
i,j=swap_them(i,j)          # Function call
print "\nAfter swap, i is ",i,"and j is ",j,"\n"
Before swap, i is  10 and j is  20 


After swap, i is  20 and j is  10 

C27CP1, Page number:565

In [12]:
#Variable Declaration
c="Bettye Lou Horn"
#Result
print "My sister's name is ",c,"\n"
My sister's name is  Bettye Lou Horn 

C27CP2, Page number:566

In [13]:
#Variable Declaration
c="Bettye Lou Horn"
print "My sister's maiden was ",c,"\n"
#Assigns new string to c
c="Bettye Lou Henderson"  
#Result
print "My sister's married name is ",c,"\n"
My sister's maiden was  Bettye Lou Horn 

My sister's married name is  Bettye Lou Henderson 

C27CP3, Page number:566

In [14]:
#Variable Declaration
c="Bettye Lou Horn"
print "My sister's maiden was ",c,"\n"
c+=str(11)               #makes c points to the last name
c="Henderson"       #Assigns new string to c
#Result
print "My sister's married name is ",c,"\n"
My sister's maiden was  Bettye Lou Horn 

My sister's married name is  Henderson 

C27PTST1, Page number:576

In [15]:
#Array declaration
name=["George","Michelle","Joe","Marcus","Stephanie"]
#Result
for ctr in range(0,5,1):
    print "String #",(ctr+1),"is",name[ctr],"\n"
String # 1 is George 

String # 2 is Michelle 

String # 3 is Joe 

String # 4 is Marcus 

String # 5 is Stephanie