summaryrefslogtreecommitdiff
path: root/C++_By_Example/Chapter3.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_By_Example/Chapter3.ipynb')
-rwxr-xr-xC++_By_Example/Chapter3.ipynb2029
1 files changed, 2029 insertions, 0 deletions
diff --git a/C++_By_Example/Chapter3.ipynb b/C++_By_Example/Chapter3.ipynb
new file mode 100755
index 00000000..b16d7174
--- /dev/null
+++ b/C++_By_Example/Chapter3.ipynb
@@ -0,0 +1,2029 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:8646d541edd5eae08f7eb4ee8123b4c24dfa7a544da673c822f5f9c2cfa537ea"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter3, C++ Constructs"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C11SIZE1, Page number:232"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Size of floating-point values\n",
+ "\n",
+ "import sys\n",
+ "x=0.0\n",
+ "\n",
+ "print \"The size of floating-point variables on this computer is \"\n",
+ "print sys.getsizeof(x) #depends on the compiler "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of floating-point variables on this computer is \n",
+ "24\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C11COM1, Page number:233"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Illustrates the sequence point.\n",
+ "num=5\n",
+ "sq,cube=num*num,num*num*num\n",
+ "#Result\n",
+ "print \"The square of \",num,\"is\",sq\n",
+ "print \"and the cube is \",cube"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The square of 5 is 25\n",
+ "and the cube is 125\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C11ODEV, Page number:239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Uses a bitwise & to determine whether a number is odd or even.\n",
+ "\n",
+ "input1=input(\"What number do you want me to test?\")\n",
+ "if input1&1:\n",
+ " print \"The number \",input1,\"is odd\"\n",
+ "else:\n",
+ " print \"The number \",input1,\"is even\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What number do you want me to test?5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number 5 is odd\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12WHIL1, Page number:248"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "ans=raw_input(\"Do you want to continue (Y/N)\")\n",
+ "\n",
+ "while ans!='Y' and ans!='N':\n",
+ " print \"\\nYou must type a Y or an N\\n\"\n",
+ " ans=raw_input( \"Do you want to continue(Y/N)?\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to continue (Y/N)k\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "You must type a Y or an N\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to continue(Y/N)?c\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "You must type a Y or an N\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to continue(Y/N)?s\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "You must type a Y or an N\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to continue(Y/N)?5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "You must type a Y or an N\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to continue(Y/N)?Y\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12WHIL3, Page number:251"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Number of letters in the user's name\n",
+ "\n",
+ "name=raw_input(\"What is your first name?\")\n",
+ "count=len(name)\n",
+ "\n",
+ "print \"Your name has \",count,\"characters\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your first name?greg\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your name has 4 characters\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12INV1, Page number:253"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"*** Inventory computation ***\\n\"\n",
+ "while True:\n",
+ " part_no=input(\"What is the next part number(-999 to end)?\\n\")\n",
+ " if part_no!=-999:\n",
+ " quantity=input(\"How many were bought?\\n\")\n",
+ " cost=input(\"What is the unit price of this item?\\n\")\n",
+ " ext_cost=cost*quantity\n",
+ " print \"\\n\",quantity,\"of #\",part_no,\"will cost\",\"%.2f\" %ext_cost,\"\\n\"\n",
+ " else:\n",
+ " break\n",
+ "print \"End of Inventory computation\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "*** Inventory computation ***\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next part number(-999 to end)?\n",
+ "213\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many were bought?\n",
+ "12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the unit price of this item?\n",
+ "5.66\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "12 of # 213 will cost 67.92 \n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next part number(-999 to end)?\n",
+ "92\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many were bought?\n",
+ "53\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the unit price of this item?\n",
+ ".23\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "53 of # 92 will cost 12.19 \n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next part number(-999 to end)?\n",
+ "-999\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "End of Inventory computation\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12EXIT1, Page number:257"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Quits early due to exit() function.\n",
+ "def main():\n",
+ " exit(0)\n",
+ " print \"C++ programming is fun\"\n",
+ " print \"I like learning C++ by example!\\n\"\n",
+ " print \"C++ is a powerful language that is not difficult to learn\"\n",
+ "main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12BRK, Page number:257"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Demonstrates the Break statement\n",
+ "while True:\n",
+ " print \"C++ is fun!\\n\"\n",
+ " break\n",
+ " user_ans=raw_input( \"Do you want to see the message again(Y/N)?\")\n",
+ "print \"That's all for now\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C++ is fun!\n",
+ "\n",
+ "That's all for now\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12CNT1, Page number:261"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ctr=0\n",
+ "\n",
+ "while ctr<10:\n",
+ " print \"Computers are fun!\\n\"\n",
+ " ctr+=1\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n",
+ "Computers are fun!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12PASS1, Page number:263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "stored_pass=11862\n",
+ "num_tries=0\n",
+ "\n",
+ "while num_tries<3:\n",
+ " user_pass=input(\"\\nWhat is the password? (you get 3 tries...)?\")\n",
+ " num_tries+=1\n",
+ " if user_pass==stored_pass:\n",
+ " print \"You entered the correct password.\\n\"\n",
+ " print \"The cash safe is behind the picture of the ship.\"\n",
+ " exit()\n",
+ " else:\n",
+ " print \"You entered the wrong password.\\n\"\n",
+ " if num_tries==3:\n",
+ " print \"Sorry, you get no more chances\"\n",
+ " else:\n",
+ " print \"you get \",3-num_tries,\"more tries...\\n\"\n",
+ "exit(0)\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "What is the password? (you get 3 tries...)?11202\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You entered the wrong password.\n",
+ "\n",
+ "you get 2 more tries...\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "What is the password? (you get 3 tries...)?23265\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You entered the wrong password.\n",
+ "\n",
+ "you get 1 more tries...\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "What is the password? (you get 3 tries...)?36963\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You entered the wrong password.\n",
+ "\n",
+ "Sorry, you get no more chances\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12GRAD1, Page number:266"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Adds grades and determines whether you earned an A.\n",
+ "total_grade=0.0\n",
+ "while 1:\n",
+ " grade=input(\"What is your grade?(-1 to end)\")\n",
+ " if grade>=0.0:\n",
+ " total_grade+=grade\n",
+ " if grade==-1:\n",
+ " break\n",
+ "#Result\n",
+ "print \"\\n\\nYou made a total of \",\"%.1f\" %total_grade,\"points\\n\"\n",
+ "\n",
+ "if total_grade>=450.00:\n",
+ " print \"** You made an A !!\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)87.6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)92.4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)78.7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)-1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "You made a total of 258.7 points\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C12GRAD2, Page number:267"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "total_grade=0.0\n",
+ "grade_avg=0.0\n",
+ "grade_ctr=0\n",
+ "while 1:\n",
+ " grade=input(\"What is your grade?(-1 to end)\")\n",
+ " if grade>=0.0:\n",
+ " total_grade+=grade\n",
+ " grade_ctr+=1\n",
+ " if grade==-1:\n",
+ " break\n",
+ "grade_avg=total_grade/grade_ctr\n",
+ "\n",
+ "#Result\n",
+ "print \"\\n\\nYou made a total of \",'%.1f' %total_grade,\"points\\n\"\n",
+ "print \"Your average was \",'%.1f' %grade_avg,\"\\n\"\n",
+ "if total_grade>=450.00:\n",
+ " print \"** You made an A !!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)67.8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)98.7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)67.8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)92.4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your grade?(-1 to end)-1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "You made a total of 326.7 points\n",
+ "\n",
+ "Your average was 81.7 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13FOR1, Page number:276"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for ctr in range(1,11):\n",
+ " print ctr,\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 \n",
+ "\n",
+ "2 \n",
+ "\n",
+ "3 \n",
+ "\n",
+ "4 \n",
+ "\n",
+ "5 \n",
+ "\n",
+ "6 \n",
+ "\n",
+ "7 \n",
+ "\n",
+ "8 \n",
+ "\n",
+ "9 \n",
+ "\n",
+ "10 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13FOR2, Page number:278"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Demonstrates totaling using a for loop.\n",
+ "\n",
+ "total=0\n",
+ "\n",
+ "for ctr in range(100,201):\n",
+ " total+=ctr\n",
+ "#Result\n",
+ "print \"The total is \",total"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The total is 15150\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13EVOD, Page number:281"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Even numbers below 21\"\n",
+ "#Result\n",
+ "for num in range(2,21,2):\n",
+ " print num,\" \",\n",
+ " \n",
+ " \n",
+ "print \"\\n\\nOdd numbers below 20\"\n",
+ "#Result\n",
+ "for num in range(1,21,2):\n",
+ " print num,\" \","
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Even numbers below 21\n",
+ "2 4 6 8 10 12 14 16 18 20 \n",
+ "\n",
+ "Odd numbers below 20\n",
+ "1 3 5 7 9 11 13 15 17 19 \n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13CNTD1, Page number:282"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for ctr in range(10,0,-1):\n",
+ " print ctr\n",
+ "print \"*** Blast off! ***\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n",
+ "9\n",
+ "8\n",
+ "7\n",
+ "6\n",
+ "5\n",
+ "4\n",
+ "3\n",
+ "2\n",
+ "1\n",
+ "*** Blast off! ***\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13FOR4, Page number:283"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "total=0.0\n",
+ "print \"\\n*** Grade Calculation ***\\n\"\n",
+ "num=input(\"How many students are there?\\n\")\n",
+ "for loopvar in range(0,num,1):\n",
+ " grade=input(\"What is the next student's grade?\\n\")\n",
+ " total=total+grade\n",
+ "\n",
+ "avg=total/num\n",
+ "#Result\n",
+ "print \"\\n the average of this class is\",'%.1f' %avg"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "*** Grade Calculation ***\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students are there?\n",
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade?\n",
+ "8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade?\n",
+ "9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade?\n",
+ "7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " the average of this class is 8.0\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13FOR6, Page number:285"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "num=5\n",
+ "print \"\\nCounting by 5s:\\n\"\n",
+ "for num in range(5,101,5):\n",
+ " print \"\\n\",num"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Counting by 5s:\n",
+ "\n",
+ "\n",
+ "5\n",
+ "\n",
+ "10\n",
+ "\n",
+ "15\n",
+ "\n",
+ "20\n",
+ "\n",
+ "25\n",
+ "\n",
+ "30\n",
+ "\n",
+ "35\n",
+ "\n",
+ "40\n",
+ "\n",
+ "45\n",
+ "\n",
+ "50\n",
+ "\n",
+ "55\n",
+ "\n",
+ "60\n",
+ "\n",
+ "65\n",
+ "\n",
+ "70\n",
+ "\n",
+ "75\n",
+ "\n",
+ "80\n",
+ "\n",
+ "85\n",
+ "\n",
+ "90\n",
+ "\n",
+ "95\n",
+ "\n",
+ "100\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13NEST1, Page number:288"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for times in range(1,4,1):\n",
+ "\n",
+ " for num in range(1,6,1):\n",
+ " print num,\n",
+ "\n",
+ " print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2 3 4 5 \n",
+ "\n",
+ "1 2 3 4 5 \n",
+ "\n",
+ "1 2 3 4 5 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13NEST2, Page number:289"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for outer in range(6,-1,-1):\n",
+ " for inner in range(1,outer,1):\n",
+ " print inner,\n",
+ " print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2 3 4 5 \n",
+ "\n",
+ "1 2 3 4 \n",
+ "\n",
+ "1 2 3 \n",
+ "\n",
+ "1 2 \n",
+ "\n",
+ "1 \n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C13FACT, Page number:291"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#factorial\n",
+ "num=input(\"What factorial do you want to see?\")\n",
+ "total=1\n",
+ "\n",
+ "for fact in range(1,num+1,1):\n",
+ " total=total*fact\n",
+ " \n",
+ "print \"The factorial for \",num,\"is\",total"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What factorial do you want to see?7\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The factorial for 7 is 5040\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14CNTD1, Page number:297"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for cd in range(10,0,-1):\n",
+ " for delay in range(1,10,1): #for delay in range(1,30001,1):\n",
+ " print \" \"\n",
+ " print cd,\"\\n\"\n",
+ "print \"*** Blast off! ***\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "10 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "9 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "8 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "7 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "6 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "5 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "4 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "3 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "2 \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ "1 \n",
+ "\n",
+ "*** Blast off! ***\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14TIM, Page number:298"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "age=input(\"What is your age?\\n\")\n",
+ "while age<=0:\n",
+ " print \"*** Your age cannot be that small ! ***\"\n",
+ " for outer in range(1,3,1):\n",
+ " for inner in range(1,50,1):\n",
+ " print \"\"\n",
+ " print \"\\r\\n\\n\"\n",
+ " age=input(\"What is your age?\\n\")\n",
+ "print \"Thanks, I did not think you would actually tell me your age!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your age?\n",
+ "20\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Thanks, I did not think you would actually tell me your age!\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14BRAK1, Page number:299"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "print \"Here are the numbers from 1 to 20\\n\"\n",
+ "for num in range(1,21,1):\n",
+ " print num,\"\\n\"\n",
+ " break\n",
+ "print \"That's all, folks!\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are the numbers from 1 to 20\n",
+ "\n",
+ "1 \n",
+ "\n",
+ "That's all, folks!\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14BRAK2, Page number:300"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#A for loop running at the user\u2019s request.\n",
+ "\n",
+ "print \"Here are the numbers from 1 to 20\\n\"\n",
+ "\n",
+ "for num in range(1,21,1):\n",
+ " print num\n",
+ " ans=raw_input(\"Do you want to see another (Y/N)?\")\n",
+ " if ans=='N' or ans=='n':\n",
+ " break\n",
+ "\n",
+ "print \"That's all, folks!\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Here are the numbers from 1 to 20\n",
+ "\n",
+ "1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "9\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Do you want to see another (Y/N)?N\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "That's all, folks!\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14BRAK3, Page number:302"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "total=0.0\n",
+ "count=0\n",
+ "print \"\\n*** Grade Calculation ***\\n\"\n",
+ "num=input(\"How many students are there?\")\n",
+ "\n",
+ "for loopvar in range(1,num+1,1):\n",
+ " grade=input(\"What is the next student's grade? (-99 to quit)\")\n",
+ " if grade<0.0:\n",
+ " break\n",
+ " count+=1\n",
+ " total+=grade\n",
+ "\n",
+ "avg=total/count\n",
+ "#Result\n",
+ "print \"The average of this class is \",'%.1f' %avg"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "*** Grade Calculation ***\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many students are there?10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)87\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)97\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)67\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)89\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)94\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next student's grade? (-99 to quit)-99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The average of this class is 86.8\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14CON1, Page number:305"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Demonstrates the use of the continue statement.\n",
+ "\n",
+ "for ctr in range(1,11,1):\n",
+ " print ctr,\n",
+ " continue\n",
+ " print \"C++ programming\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 2 3 4 5 6 7 8 9 10\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C14CON3, Page number:306"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Average salaries over $10,000\n",
+ "\n",
+ "avg=0.0\n",
+ "total=0.0\n",
+ "month=1.0\n",
+ "count=0\n",
+ "\n",
+ "while month>0.0:\n",
+ " month=input(\"What is the next monthly salary (-1) to quit\")\n",
+ " year=month*12.00\n",
+ " if year <= 10000.00: #Do not add low salaries\n",
+ " continue\n",
+ " if month<0.0:\n",
+ " break\n",
+ " count+=1\n",
+ " total+=year #Add yearly salary to total.\n",
+ "avg=total/float(count)\n",
+ "#Result\n",
+ "print \"\\nThe average of high salaries is $\",'%.2f' %avg"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit500\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit2000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit750\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit4000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit5000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit1200\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is the next monthly salary (-1) to quit-1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "The average of high salaries is $ 36600.00\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C15BEEP1, Page number:314"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def beep():\n",
+ " import os\n",
+ " os.system('\\a')\n",
+ "\n",
+ "num=input(\"Please enter a number:\")\n",
+ "\n",
+ "#Use multiple if statements to beep.\n",
+ "\n",
+ "if num==1:\n",
+ " beep()\n",
+ "else:\n",
+ " if num==2:\n",
+ " beep()\n",
+ " beep()\n",
+ " else:\n",
+ " if num==3:\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n",
+ " else:\n",
+ " if num==4:\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n",
+ " else:\n",
+ " if num==5:\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n",
+ " beep()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Please enter a number:2\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C15SALE, Page number:319"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Prints daily, weekly, and monthly sales totals.\n",
+ "\n",
+ "daily=2343.34\n",
+ "weekly=13432.65\n",
+ "monthly=43468.97\n",
+ "\n",
+ "ans=raw_input(\"Is this the end of the month? (Y/N) :\")\n",
+ "\n",
+ "if ans=='Y' or ans=='y':\n",
+ " day=6\n",
+ "else:\n",
+ " day=input(\"What day number , 1 through 5( for mon-fri) :\")\n",
+ "if day==6:\n",
+ " print \"The monthly total is \",'%.2f' %monthly\n",
+ "else:\n",
+ " if day==5:\n",
+ " print \"The weekly total is \",'%.2f' %weekly\n",
+ " else:\n",
+ " print \"The daily total is \",'%.2f' %daily\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Is this the end of the month? (Y/N) :Y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The monthly total is 43468.97\n"
+ ]
+ }
+ ],
+ "prompt_number": 19
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "C15DEPT1, Page number:320"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "choice=\"R\"\n",
+ "\n",
+ "while choice!='S' and choice!='A' and choice!='E' and choice!='P':\n",
+ " print \"\\n choose your department :\"\n",
+ " print \"S - Sales\"\n",
+ " print \"A - Accounting\"\n",
+ " print \"E - Engineering\"\n",
+ " print \"P - Payroll\"\n",
+ " choice=raw_input( \"What is your choice? (upper case)\")\n",
+ "\n",
+ "if choice=='E':\n",
+ " print \"Your meeting is at 2:30\"\n",
+ "else:\n",
+ " if choice=='S':\n",
+ " print \"Your meeting is at 8:30\"\n",
+ " else:\n",
+ " if choice=='A':\n",
+ " print \"Your meeting is at 10:00\"\n",
+ " else:\n",
+ " if choice=='P':\n",
+ " print \"your meeting has been cancelled\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " choose your department :\n",
+ "S - Sales\n",
+ "A - Accounting\n",
+ "E - Engineering\n",
+ "P - Payroll\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "What is your choice? (upper case)E\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your meeting is at 2:30\n"
+ ]
+ }
+ ],
+ "prompt_number": 21
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file