{ "metadata": { "name": "", "signature": "sha256:d11a5d1aed8c464716c568d4d8be1f78c4ea2b8f130a5adc4fa9ba5c3703fc04" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6: 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", "for ctr in range(0,6,1): #computes total of scores\n", " average+=scores[ctr]\n", "average/=float(6) #computes the average\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", "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", "pr_scores(scores) #Function call to print scores\n", "for ctr in range(0,6,1): #computes total of scores\n", " average+=scores[ctr]\n", "average/=float(6) #computes the average\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", "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", "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", "pr_scores(scores) #Function call to print scores\n", "for ctr in range(0,CLASS_NUM,1): #Computes total of scores\n", " average+=scores[ctr]\n", "average/=float(6) #Computes the average\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", "temps=[]\n", "#Adding values into temps variable\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", "#Print the temperatures\n", "print \"Daily temperatures for the last \",NUM_TEMPS,\"days:\\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", "for ctr in range(0,NUM,1):\n", " print \"Please enter the next number...\",\n", " nums.append(input())\n", " total+=nums[ctr]\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", "sales=[] \n", "print \"Please enter the twelve monthly sales values\\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", "#for ctr in range(0,25,1):\n", "# print \"\\n\" #Clears the screen\n", "print \"\\n\\n*** Sales Printing Program ***\\n\"\n", "print \"Prints any sales from the last \",NUM,\" months\\n\"\n", "ans='Y'\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": [ "363.25\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": [ "652.36\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 4 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "445.52\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": [ "780.2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 7 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "125.36\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 8 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "425.15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 9 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "325.96\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 10 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "109.75\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 11 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "123.65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "What are sales for month number 12 ?\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "253.84\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\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" ] } ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "C24HIGH, Page number:496" ] }, { "cell_type": "code", "collapsed": false, "input": [ "SIZE=15 #Maximum size of array\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", "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", "print \"The highest number in the list is \",high_val,\".\\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", "#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", "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", "print \"\\n\\n\"\n", "#Initialize first element to both high_val and low_val\n", "high_val=ara[0] \n", "low_val=ara[0]\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]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", "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", "#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", "#Prints the array ara[]\n", "def print_array(ara):\n", " for ctr in range(0,MAX,1):\n", " print ara[ctr],\"\\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]