summaryrefslogtreecommitdiff
path: root/C++_By_Example/Chapter6.ipynb
diff options
context:
space:
mode:
authorhardythe12015-04-07 15:58:05 +0530
committerhardythe12015-04-07 15:58:05 +0530
commitc7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131 (patch)
tree725a7d43dc1687edf95bc36d39bebc3000f1de8f /C++_By_Example/Chapter6.ipynb
parent62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 (diff)
downloadPython-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.gz
Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.bz2
Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.zip
added books
Diffstat (limited to 'C++_By_Example/Chapter6.ipynb')
-rwxr-xr-xC++_By_Example/Chapter6.ipynb1505
1 files changed, 1505 insertions, 0 deletions
diff --git a/C++_By_Example/Chapter6.ipynb b/C++_By_Example/Chapter6.ipynb
new file mode 100755
index 00000000..5ecafc20
--- /dev/null
+++ b/C++_By_Example/Chapter6.ipynb
@@ -0,0 +1,1505 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d5f3c8482f9d7c2571575eb27fc10c6b549235c3c17830e788a211d649641ac0"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter6, Arrays and pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23ARA1, Page number:482"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "s_name=\"Tri Star University\"\n",
+ "scores = [88.7,90.4,76.0,97.0,100.0,86.7] #integer array\n",
+ "average=0.0\n",
+ "\n",
+ "for ctr in range(0,6,1): #computes total of scores\n",
+ " average+=scores[ctr]\n",
+ "\n",
+ "average/=float(6) #computes the average\n",
+ "\n",
+ "print \"At \",s_name,\", your class average is \",'%.2f' %average,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "At Tri Star University , your class average is 89.80 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23ARA2, Page number:483"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def pr_scores(scores):\n",
+ " print \"Here are your scores:\\n\"\n",
+ " for ctr in range(0,6,1):\n",
+ " print '%.2f' %scores[ctr],\"\\n\"\n",
+ " \n",
+ "s_name=\"Tri Star University\"\n",
+ "scores = [88.7,90.4,76.0,97.0,100.0,86.7] #integer array\n",
+ "average=0.0\n",
+ "\n",
+ "pr_scores(scores) #Function call to print scores\n",
+ "\n",
+ "for ctr in range(0,6,1): #computes total of scores\n",
+ " average+=scores[ctr]\n",
+ "\n",
+ "average/=float(6) #computes the average\n",
+ "\n",
+ "print \"At \",s_name,\", your class average is \",'%.2f' %average,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are your scores:\n",
+ "\n",
+ "88.70 \n",
+ "\n",
+ "90.40 \n",
+ "\n",
+ "76.00 \n",
+ "\n",
+ "97.00 \n",
+ "\n",
+ "100.00 \n",
+ "\n",
+ "86.70 \n",
+ "\n",
+ "At Tri Star University , your class average is 89.80 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23ARA3, Page number:485"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "CLASS_NUM=6\n",
+ "\n",
+ "def pr_scores(scores): #Function definition \n",
+ " print \"Here are your scores:\\n\"\n",
+ " for ctr in range(0,CLASS_NUM,1):\n",
+ " print '%.2f' %scores[ctr],\"\\n\"\n",
+ " \n",
+ "s_name=\"Tri Star University\"\n",
+ "scores = [88.7,90.4,76.0,97.0,100.0,86.7] #Integer array\n",
+ "average=0.0\n",
+ "\n",
+ "pr_scores(scores) #Function call to print scores\n",
+ "\n",
+ "for ctr in range(0,CLASS_NUM,1): #Computes total of scores\n",
+ " average+=scores[ctr]\n",
+ "\n",
+ "average/=float(6) #Computes the average\n",
+ "\n",
+ "print \"At \",s_name,\", your class average is \",'%.2f' %average,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are your scores:\n",
+ "\n",
+ "88.70 \n",
+ "\n",
+ "90.40 \n",
+ "\n",
+ "76.00 \n",
+ "\n",
+ "97.00 \n",
+ "\n",
+ "100.00 \n",
+ "\n",
+ "86.70 \n",
+ "\n",
+ "At Tri Star University , your class average is 89.80 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23ARA4, Page number:487"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NUM_TEMPS=10\n",
+ "\n",
+ "temps=[]\n",
+ "\n",
+ "#Adding values into temps variable\n",
+ "\n",
+ "temps.append(78.6)\n",
+ "temps.append(82.1)\n",
+ "temps.append(79.5)\n",
+ "temps.append(75.0)\n",
+ "temps.append(75.4)\n",
+ "temps.append(71.8)\n",
+ "temps.append(73.3)\n",
+ "temps.append(69.5)\n",
+ "temps.append(74.1)\n",
+ "temps.append(75.7)\n",
+ "\n",
+ "#Print the temperatures\n",
+ "\n",
+ "print \"Daily temperatures for the last \",NUM_TEMPS,\"days:\\n\"\n",
+ "\n",
+ "for ctr in range(0,NUM_TEMPS,1):\n",
+ " print '%.2f' %temps[ctr],\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Daily temperatures for the last 10 days:\n",
+ "\n",
+ "78.60 \n",
+ "\n",
+ "82.10 \n",
+ "\n",
+ "79.50 \n",
+ "\n",
+ "75.00 \n",
+ "\n",
+ "75.40 \n",
+ "\n",
+ "71.80 \n",
+ "\n",
+ "73.30 \n",
+ "\n",
+ "69.50 \n",
+ "\n",
+ "74.10 \n",
+ "\n",
+ "75.70 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23TOT, Page number:488"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NUM=8\n",
+ "nums=[]\n",
+ "total=0\n",
+ "\n",
+ "for ctr in range(0,NUM,1):\n",
+ " print \"Please enter the next number...\",\n",
+ " nums.append(input())\n",
+ " total+=nums[ctr]\n",
+ "\n",
+ "#Prints the sum of eight values \n",
+ "print \"The total of the numbers is \",total,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Please enter the next number..."
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "8\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The total of the numbers is 36 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C23SAL, Page number:489"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NUM=12\n",
+ "\n",
+ "sales=[] \n",
+ "\n",
+ "print \"Please enter the twelve monthly sales values\\n\"\n",
+ "\n",
+ "#Fill the array with input values entered by the user\n",
+ "for ctr in range(0,NUM,1):\n",
+ " print \"What are sales for month number \",ctr+1,\"?\\n\"\n",
+ " sales.append(input())\n",
+ "\n",
+ "for ctr in range(0,25,1):\n",
+ " print \"\\n\" #Clears the screen\n",
+ "\n",
+ "print \"*** Sales Printing Program ***\\n\"\n",
+ "print \"Prints any sales from the last \",NUM,\" months\\n\"\n",
+ "ans='Y'\n",
+ "\n",
+ "while ans=='Y':\n",
+ " print \"For what month (1-\",NUM,\") do you want to see a sales value? \" \n",
+ " req_month=input()\n",
+ " print \"\\nMonth \",req_month,\"'s sales are\",'%.2f' %sales[req_month-1]\n",
+ " print \"\\nDo you want to see another (Y/N)? \"\n",
+ " ans=raw_input().upper()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please enter the twelve monthly sales values\n",
+ "\n",
+ "What are sales for month number 1 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "450.33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 2 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "433.22\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 3 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "500.11\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 4 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "465.33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 5 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "123.45\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 6 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "698.33\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 7 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "756.65\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 8 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "121.45\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 9 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "147.36\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 10 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "789.36\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 11 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "986.25\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What are sales for month number 12 ?\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "108.36\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "*** Sales Printing Program ***\n",
+ "\n",
+ "Prints any sales from the last 12 months\n",
+ "\n",
+ "For what month (1- 12 ) do you want to see a sales value? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Month 2 's sales are 433.22\n",
+ "\n",
+ "Do you want to see another (Y/N)? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "For what month (1- 12 ) do you want to see a sales value? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Month 5 's sales are 123.45\n",
+ "\n",
+ "Do you want to see another (Y/N)? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C24HIGH, Page number:496"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "SIZE=15 #Maximum size of array\n",
+ "\n",
+ "ara=[None]*SIZE #Empty Array declaration with maximum size\n",
+ "ara=[5,2,7,8,36,4,2,86,11,43,22,12,45,6,85]\n",
+ "high_val=ara[0] #initialize wit first array element\n",
+ "\n",
+ "for ctr in range(1,SIZE,1):\n",
+ " if ara[ctr]>high_val: #Compares with rest of the elements in the array \n",
+ " high_val=ara[ctr] #Stores higher value in high_val\n",
+ "\n",
+ "print \"The highest number in the list is \",high_val,\".\\n\"\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The highest number in the list is 86 .\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C24HILO, Page number:498"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Maximum size of array\n",
+ "SIZE=15 \n",
+ "#Initialize empty array\n",
+ "ara=[None]*SIZE \n",
+ "\n",
+ "#Fill array with random numbers from 0 to 99\n",
+ "import random\n",
+ "for ctr in range(0,SIZE,1):\n",
+ " ara[ctr]=random.randint(0,99) %100 \n",
+ "\n",
+ "print \"Here are the \",SIZE,\"random numbers:\\n\" \n",
+ "for ctr in range(0,SIZE,1):\n",
+ " print ara[ctr],\"\\n\" #Prints the array\n",
+ " \n",
+ "print \"\\n\\n\"\n",
+ "\n",
+ "#Initialize first element to both high_val and low_val\n",
+ "high_val=ara[0] \n",
+ "low_val=ara[0]\n",
+ "\n",
+ "for ctr in range(1,SIZE,1):\n",
+ " if ara[ctr]>high_val: #Compares with rest of the elements in the array\n",
+ " high_val=ara[ctr] #Stores higher valure in high_val\n",
+ " if ara[ctr]<low_val:\n",
+ " low_val=ara[ctr] #Stores lower valure in low_val\n",
+ "\n",
+ "print \"The highest number in the list is \",high_val,\"\\n\"\n",
+ "print \"The lowest number in the list is \",low_val,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are the 15 random numbers:\n",
+ "\n",
+ "81 \n",
+ "\n",
+ "96 \n",
+ "\n",
+ "31 \n",
+ "\n",
+ "1 \n",
+ "\n",
+ "34 \n",
+ "\n",
+ "53 \n",
+ "\n",
+ "70 \n",
+ "\n",
+ "9 \n",
+ "\n",
+ "23 \n",
+ "\n",
+ "89 \n",
+ "\n",
+ "51 \n",
+ "\n",
+ "73 \n",
+ "\n",
+ "53 \n",
+ "\n",
+ "85 \n",
+ "\n",
+ "79 \n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "The highest number in the list is 96 \n",
+ "\n",
+ "The lowest number in the list is 1 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C24SERCH, Page number:499"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX=100\n",
+ "\n",
+ "def fill_parts(parts): #Function assigns first five parts to array for testing\n",
+ " parts[0]=12345\n",
+ " parts[1]=24724\n",
+ " parts[2]=54154\n",
+ " parts[3]=73496\n",
+ " parts[4]=83925\n",
+ " return parts\n",
+ "\n",
+ "parts=[None]*MAX\n",
+ "num_parts=5 #Beginning inventory count\n",
+ "\n",
+ "fill_parts(parts) #Fills the first five elements\n",
+ "\n",
+ "search_part=0\n",
+ "while search_part != -9999:\n",
+ " print \"\\n\\nPlease type a part number...(-9999 ends program)\",\n",
+ " search_part=input()\n",
+ " if search_part==-9999:\n",
+ " break #Exits the loop if user wants\n",
+ " \n",
+ " for ctr in range(0,num_parts,1): #Scans array to see whether part is in inventory\n",
+ " if search_part==parts[ctr]:\n",
+ " print \"\\nPart \",search_part,\"is already in inventory\"\n",
+ " break\n",
+ " else:\n",
+ " if ctr==num_parts-1:\n",
+ " parts[num_parts]=search_part #If not then it is added to end of the array\n",
+ " num_parts+=1\n",
+ " print search_part,\"was added to inventory\\n\"\n",
+ " break\n",
+ " \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C24SORT1, Page number:504"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX=10\n",
+ "\n",
+ "#Fill array with random numbers from 0 to 99\n",
+ "def fill_array(ara):\n",
+ " import random\n",
+ " for ctr in range(0,MAX,1):\n",
+ " ara[ctr]=random.randint(0,99) %100\n",
+ "\n",
+ "\n",
+ "#Prints the array ara[]\n",
+ "def print_array(ara):\n",
+ " for ctr in range(0,MAX,1):\n",
+ " print ara[ctr],\"\\n\"\n",
+ "\n",
+ "#Sorts the array\n",
+ "def sort_array(ara):\n",
+ " for ctr1 in range(0,MAX-1,1):\n",
+ " for ctr2 in range(ctr1+1,MAX,1):\n",
+ " if ara[ctr1]>ara[ctr2]: #Swap if this part is not in order\n",
+ " temp=ara[ctr1] #Temporary variable to swap\n",
+ " ara[ctr1]=ara[ctr2]\n",
+ " ara[ctr2]=temp\n",
+ "\n",
+ "\n",
+ "ara=[None]*MAX\n",
+ "fill_array(ara)\n",
+ "print \"Here are the unsorted numbers:\\n\"\n",
+ "print_array(ara) #Prints the unsorted array\n",
+ "sort_array(ara) #Sorts the array in ascending order\n",
+ "print \"\\n\\nHere are the sorted numbers:\\n\"\n",
+ "print_array(ara) #Prints the sorted array"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are the unsorted numbers:\n",
+ "\n",
+ "68 \n",
+ "\n",
+ "41 \n",
+ "\n",
+ "53 \n",
+ "\n",
+ "40 \n",
+ "\n",
+ "69 \n",
+ "\n",
+ "65 \n",
+ "\n",
+ "64 \n",
+ "\n",
+ "48 \n",
+ "\n",
+ "87 \n",
+ "\n",
+ "18 \n",
+ "\n",
+ "\n",
+ "\n",
+ "Here are the sorted numbers:\n",
+ "\n",
+ "18 \n",
+ "\n",
+ "40 \n",
+ "\n",
+ "41 \n",
+ "\n",
+ "48 \n",
+ "\n",
+ "53 \n",
+ "\n",
+ "64 \n",
+ "\n",
+ "65 \n",
+ "\n",
+ "68 \n",
+ "\n",
+ "69 \n",
+ "\n",
+ "87 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C24SORT2, Page number:506"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX=10\n",
+ "\n",
+ "#Fill array with random numbers from 0 to 99\n",
+ "def fill_array(ara):\n",
+ " import random\n",
+ " for ctr in range(0,MAX,1):\n",
+ " ara[ctr]=random.randint(0,99) %100\n",
+ "\n",
+ "\n",
+ "#Prints the array ara[]\n",
+ "def print_array(ara):\n",
+ " for ctr in range(0,MAX,1):\n",
+ " print ara[ctr],\"\\n\"\n",
+ "\n",
+ "#Sorts the array\n",
+ "def sort_array(ara):\n",
+ " for ctr1 in range(0,MAX-1,1):\n",
+ " for ctr2 in range(ctr1+1,MAX,1):\n",
+ " if ara[ctr1]<ara[ctr2]: #Swap if this part is not in order\n",
+ " temp=ara[ctr1] #Temporary variable to swap\n",
+ " ara[ctr1]=ara[ctr2]\n",
+ " ara[ctr2]=temp\n",
+ "\n",
+ "\n",
+ "ara=[None]*MAX\n",
+ "fill_array(ara)\n",
+ "print \"Here are the unsorted numbers:\\n\"\n",
+ "print_array(ara) #Prints the unsorted array\n",
+ "sort_array(ara) #Sorts the array in descending order\n",
+ "print \"\\n\\nHere are the sorted numbers:\\n\"\n",
+ "print_array(ara) #Prints the newly sorted array"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are the unsorted numbers:\n",
+ "\n",
+ "40 \n",
+ "\n",
+ "22 \n",
+ "\n",
+ "4 \n",
+ "\n",
+ "78 \n",
+ "\n",
+ "20 \n",
+ "\n",
+ "45 \n",
+ "\n",
+ "60 \n",
+ "\n",
+ "93 \n",
+ "\n",
+ "85 \n",
+ "\n",
+ "22 \n",
+ "\n",
+ "\n",
+ "\n",
+ "Here are the sorted numbers:\n",
+ "\n",
+ "93 \n",
+ "\n",
+ "85 \n",
+ "\n",
+ "78 \n",
+ "\n",
+ "60 \n",
+ "\n",
+ "45 \n",
+ "\n",
+ "40 \n",
+ "\n",
+ "22 \n",
+ "\n",
+ "22 \n",
+ "\n",
+ "20 \n",
+ "\n",
+ "4 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C25DISK1, Page number:533"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Two dimensional array declaration\n",
+ "disks= [[[] for ni in range(4)] for mi in range(2)]\n",
+ "\n",
+ "#Assign values to the 2Darray\n",
+ "disks[0][0]=2.39\n",
+ "disks[0][1]=2.75\n",
+ "disks[0][2]=3.29\n",
+ "disks[0][3]=3.59\n",
+ "\n",
+ "disks[1][0]=1.75\n",
+ "disks[1][1]=2.19\n",
+ "disks[1][2]=2.69\n",
+ "disks[1][3]=2.95\n",
+ "\n",
+ "#Print the values in the array\n",
+ "for row in range(0,2,1):\n",
+ " for col in range(0,4,1):\n",
+ " print \"$\",'%.2f'%disks[row][col],\"\\n\","
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "$ 2.39 \n",
+ "$ 2.75 \n",
+ "$ 3.29 \n",
+ "$ 3.59 \n",
+ "$ 1.75 \n",
+ "$ 2.19 \n",
+ "$ 2.69 \n",
+ "$ 2.95 \n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C25DISK2, Page number:534"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Two dimensional array declaration\n",
+ "disks= [[[] for ni in range(4)] for mi in range(2)]\n",
+ "\n",
+ "#Assign values to the 2Darray\n",
+ "disks[0][0]=2.39\n",
+ "disks[0][1]=2.75\n",
+ "disks[0][2]=3.29\n",
+ "disks[0][3]=3.59\n",
+ "\n",
+ "disks[1][0]=1.75\n",
+ "disks[1][1]=2.19\n",
+ "disks[1][2]=2.69\n",
+ "disks[1][3]=2.95\n",
+ "\n",
+ "#Print the values in the array\n",
+ "for row in range(0,2,1):\n",
+ " for col in range(0,4,1):\n",
+ " print \"$\",'%.2f'%disks[row][col],\"\\t\",\n",
+ " print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "$ 2.39 \t$ 2.75 \t$ 3.29 \t$ 3.59 \t\n",
+ "\n",
+ "$ 1.75 \t$ 2.19 \t$ 2.69 \t$ 2.95 \t\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C25DISK3, Page number:535"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Two dimensional array declaration\n",
+ "disks= [[[] for ni in range(4)] for mi in range(2)]\n",
+ "\n",
+ "#Assign values to the 2Darray\n",
+ "disks[0][0]=2.39\n",
+ "disks[0][1]=2.75\n",
+ "disks[0][2]=3.29\n",
+ "disks[0][3]=3.59\n",
+ "\n",
+ "disks[1][0]=1.75\n",
+ "disks[1][1]=2.19\n",
+ "disks[1][2]=2.69\n",
+ "disks[1][3]=2.95\n",
+ "\n",
+ "#Print the column titles.\n",
+ "print \"\\tSingle-sided\\tDouble-sided\\tSingle-sided\\tDouble-sided\"\n",
+ "print \"\\tDouble-density\\tDouble-density\\tHigh-density\\tHigh-density\"\n",
+ "\n",
+ "#Print the prices\n",
+ "for row in range(0,2,1):\n",
+ " if row==0:\n",
+ " print \"3-1/2\\\"\\t\",\n",
+ " else:\n",
+ " print \"5-1/2\\\"\\t\",\n",
+ " for col in range(0,4,1):\n",
+ " print \"$\",'%.2f'%disks[row][col],\"\\t\\t\",\n",
+ " print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\tSingle-sided\tDouble-sided\tSingle-sided\tDouble-sided\n",
+ "\tDouble-density\tDouble-density\tHigh-density\tHigh-density\n",
+ "3-1/2\"\t$ 2.39 \t\t$ 2.75 \t\t$ 3.29 \t\t$ 3.59 \t\t\n",
+ "\n",
+ "5-1/2\"\t$ 1.75 \t\t$ 2.19 \t\t$ 2.69 \t\t$ 2.95 \t\t\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C26SWAP, Page number:551"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Function definition\n",
+ "def swap_them(num1,num2):\n",
+ " num1,num2=num2,num1\n",
+ " return num1,num2\n",
+ "\n",
+ "#Variable declaration\n",
+ "i=10\n",
+ "j=20\n",
+ "\n",
+ "print \"\\n\\nBefore swap, i is \",i,\"and j is \",j,\"\\n\\n\"\n",
+ "\n",
+ "i,j=swap_them(i,j) # Function call\n",
+ "\n",
+ "print \"\\n\\nAfter swap, i is \",i,\"and j is \",j,\"\\n\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "Before swap, i is 10 and j is 20 \n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "After swap, i is 20 and j is 10 \n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C27CP1, Page number:565"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Variable Declaration\n",
+ "c=\"Bettye Lou Horn\"\n",
+ "\n",
+ "#Result\n",
+ "print \"My sister's name is \",c,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "My sister's name is Bettye Lou Horn \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C27CP2, Page number:566"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Variable Declaration\n",
+ "c=\"Bettye Lou Horn\"\n",
+ "\n",
+ "print \"My sister's maiden was \",c,\"\\n\"\n",
+ "\n",
+ "#Assigns new string to c\n",
+ "c=\"Bettye Lou Henderson\" \n",
+ "\n",
+ "#Result\n",
+ "print \"My sister's married name is \",c,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "My sister's maiden was Bettye Lou Horn \n",
+ "\n",
+ "My sister's married name is Bettye Lou Henderson \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C27CP3, Page number:566"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Variable Declaration\n",
+ "c=\"Bettye Lou Horn\"\n",
+ "\n",
+ "print \"My sister's maiden was \",c,\"\\n\"\n",
+ "c+=str(11) #makes c points to the last name\n",
+ "\n",
+ "c=\"Henderson\" #Assigns new string to c\n",
+ "\n",
+ "#Result\n",
+ "print \"My sister's married name is \",c,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "My sister's maiden was Bettye Lou Horn \n",
+ "\n",
+ "My sister's married name is Henderson \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C27PTST1, Page number:576"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Array declaration\n",
+ "name=[\"George\",\"Michelle\",\"Joe\",\"Marcus\",\"Stephanie\"]\n",
+ "\n",
+ "#Result\n",
+ "for ctr in range(0,5,1):\n",
+ " print \"String #\",(ctr+1),\"is\",name[ctr],\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "String # 1 is George \n",
+ "\n",
+ "String # 2 is Michelle \n",
+ "\n",
+ "String # 3 is Joe \n",
+ "\n",
+ "String # 4 is Marcus \n",
+ "\n",
+ "String # 5 is Stephanie \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file