diff options
author | Thomas Stephen Lee | 2015-08-28 16:53:23 +0530 |
---|---|---|
committer | Thomas Stephen Lee | 2015-08-28 16:53:23 +0530 |
commit | 4a1f703f1c1808d390ebf80e80659fe161f69fab (patch) | |
tree | 31b43ae8895599f2d13cf19395d84164463615d9 /C++_By_Example_by_Greg__M._Perry | |
parent | 9d260e6fae7328d816a514130b691fbd0e9ef81d (diff) | |
download | Python-Textbook-Companions-4a1f703f1c1808d390ebf80e80659fe161f69fab.tar.gz Python-Textbook-Companions-4a1f703f1c1808d390ebf80e80659fe161f69fab.tar.bz2 Python-Textbook-Companions-4a1f703f1c1808d390ebf80e80659fe161f69fab.zip |
add books
Diffstat (limited to 'C++_By_Example_by_Greg__M._Perry')
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter1_3.ipynb | 733 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter2_3.ipynb | 756 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter3_3.ipynb | 1969 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter4_3.ipynb | 1073 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter5_3.ipynb | 184 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter6_3.ipynb | 1457 | ||||
-rwxr-xr-x | C++_By_Example_by_Greg__M._Perry/Chapter7_3.ipynb | 1046 |
7 files changed, 7218 insertions, 0 deletions
diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter1_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter1_3.ipynb new file mode 100755 index 00000000..98d9cbdf --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter1_3.ipynb @@ -0,0 +1,733 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:846e4f1aaa1db87dad144e6583289406d6871f19fbfd727f22b3926e50573c01" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1: Introduction to C++" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C3FIRST, Page number:52" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Variable Declaration\n", + "i=4\n", + "j=i+7\n", + "c='A'\n", + "x=9.087\n", + "x=x*4.5\n", + "#Result\n", + "print i,c,j,x" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "4 A 11 40.8915\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C4ST1, Page number:85" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Result\n", + "print \"C++ programming is fun!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++ programming is fun!\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C4ST2, Page number:86" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "tax_rate=0.08\n", + "sale=22.54\n", + "#Calculation\n", + "tax=sale*tax_rate\n", + "#Result\n", + "print \"The sales tax is :\" ,tax" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sales tax is : 1.8032\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C4AREAC,Page number:95" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "PI=3.14159\n", + "radius=5\n", + "#Calculation\n", + "area=radius*radius*PI\n", + "#Result\n", + "print \"The area is \",area\n", + "radius=20\n", + "#Calculation\n", + "area=radius*radius*PI\n", + "#Result\n", + "print \"The area is \",area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The area is 78.53975\n", + "The area is 1256.636\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C5INIT,Page number:108" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#first=raw_input(\"Enter your first name:\")\n", + "#last=raw_input(\"Enter your last name\")\n", + "first=\"perry\"\n", + "last=\"greg\"\n", + "#Print the Initials\n", + "print \"Your initials are \",first[0],last[0] " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your initials are p g\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C6PRE,Page number:114" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "AGE=28\n", + "MESSAGE=\"Hello, world\"\n", + "i=10\n", + "age=5\n", + "# 'AGE' is different from 'age'\n", + "i=i*AGE \n", + "#Result\n", + "print i,\" \",age,\" \",AGE,\"\\n\"\n", + "print MESSAGE " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "280 5 28 \n", + "\n", + "Hello, world\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C6INCL1,Page number:119" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Result\n", + "print \"Kelly Jane Peterson\\n\"\n", + "print \"Apartment #217\\n\"\n", + "print \"4323 East Skelly Drive\\n\"\n", + "print \"New York, New York\\n\"\n", + "print \" 10012\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kelly Jane Peterson\n", + "\n", + "Apartment #217\n", + "\n", + "4323 East Skelly Drive\n", + "\n", + "New York, New York\n", + "\n", + " 10012\n", + "\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C6INCL3,Page number:120" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "source = \"This is fun!\"\n", + "#source is copied to message\n", + "import copy\n", + "message=copy.copy(source)\n", + "#Result\n", + "print message" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "This is fun!\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C6DEF1,Page number:121" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MYNAME=\"Phil Ward\"\n", + "name=MYNAME\n", + "#Result\n", + "print \"My name is \",name,\"\\n\"\n", + "print \"My name is \",MYNAME,\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "My name is Phil Ward \n", + "\n", + "My name is Phil Ward \n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C6DEF2,Page number:122" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#function definition\n", + "def X4(b,c,d):\n", + " return 2*b+c+3*b+c+b+c+4*b+c+b+c*c+b+c-d\n", + "b=2\n", + "c=3\n", + "d=4\n", + "e= X4 (b,c,d)\n", + "#Result\n", + "print e,\",\",b+c,\",\",b+c+b+c,\",\",b+c+b+c*c+b+c-d,\",\",X4(b,c,d)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "44 , 5 , 10 , 17 , 44\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7PRNT1,Page number:136" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "first='E'\n", + "middle='W'\n", + "last='C'\n", + "age=32\n", + "dependents=2\n", + "salary=25000.00\n", + "bonus=575.25\n", + "#Result\n", + "print \"Here are the initials: \"\n", + "print first,middle,last\n", + "print \"The age and number of dependents are \"\n", + "print age,\" \",dependents\n", + "print \"The salary and bonus are \"\n", + "print salary,\" \",bonus" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Here are the initials: \n", + "E W C\n", + "The age and number of dependents are \n", + "32 2\n", + "The salary and bonus are \n", + "25000.0 575.25\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7TEAM, Page number:138" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#table of team names and hits for three weeks\n", + "print \"Parrots\\tRams\\tKings\\tTitans\\tChargers\"\n", + "print \"3\\t5\\t3\\t1\\t0\"\n", + "print \"2\\t5\\t1\\t0\\t1\"\n", + "print \"2\\t6\\t4\\t3\\t0\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Parrots\tRams\tKings\tTitans\tChargers\n", + "3\t5\t3\t1\t0\n", + "2\t5\t1\t0\t1\n", + "2\t6\t4\t3\t0\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7PAY1,Page number:141" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Computes and prints payroll data properly in dollars and cents.\n", + "emp_name=\"Larry Payton\"\n", + "pay_date=\"03/09/92\"\n", + "hours_worked=43\n", + "rate=7.75 #pay per hour\n", + "tax_rate=.32 #Tax percentage rate\n", + "#Compute the pay amount\n", + "gross_pay=hours_worked*rate\n", + "taxes=tax_rate*gross_pay\n", + "net_pay=gross_pay-taxes\n", + "#Results\n", + "print \"As of: \",pay_date\n", + "print emp_name,\" worked \",hours_worked,\"hours\"\n", + "print \"and got paid\",round(gross_pay,2)\n", + "print \"After taxes of: \",round(taxes,2)\n", + "print \"his take-home pay was $\",round(net_pay,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "As of: 03/09/92\n", + "Larry Payton worked 43 hours\n", + "and got paid 333.25\n", + "After taxes of: 106.64\n", + "his take-home pay was $ 226.61\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7SLTX1, Page number:146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#getting total sale as float number.\n", + "#print \"What is the total amount of the sale?\"\n", + "#total_sale=float(raw_input()) \n", + "total_sale=50\n", + "#Compute sales tax\n", + "stax=total_sale*0.07 \n", + "#Results\n", + "print \"The sales tax for\",float(round(total_sale,2)),\"is\",round(stax,2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sales tax for 50.0 is 3.5\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7PHON1, Page number:147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#request user's name and print it as it would appeat in a phone book\n", + "#get name\n", + "#first=raw_input(\"What is your first name?\\n\")\n", + "#last=raw_input(\"What is your last name?\\n\")\n", + "first=\"perry\"\n", + "last=\"greg\"\n", + "print \"\\n\\n\"\n", + "print \"In a phone book,your name would look like this :\\n\"\n", + "print last,\",\",first " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "\n", + "In a phone book,your name would look like this :\n", + "\n", + "greg , perry\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7MATH, Page number:148" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Simple Addition\n", + "print \"*** Math Practice ***\\n\"\n", + "#num1=input(\"What is the first number:\")\n", + "#num2=input(\"What is the second number:\")\n", + "num1=10\n", + "num2=20\n", + "ans=num1+num2\n", + "#get user answer\n", + "#her_ans=input(\"\\nWhat do you think is the answer?\")\n", + "her_ans=30\n", + "#Result\n", + "print \"\\n\",num1,\"plus\",num2,\"is\",ans,\"\\n\\nHope you got it right!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "*** Math Practice ***\n", + "\n", + "\n", + "10 plus 20 is 30 \n", + "\n", + "Hope you got it right!\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7PS2, Page number:150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "message=\"Please turn on your printer.\"\n", + "#Result\n", + "print message" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please turn on your printer.\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7PRNTF, Page number:153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "first='E'\n", + "middle='W'\n", + "last='C'\n", + "age=32\n", + "dependents=2\n", + "salary=25000.00\n", + "bonus=575.25\n", + "#Result\n", + "print \"Here are the initials: \"\n", + "print first,\" \",middle,\" \",last,\"\\n\"\n", + "print \"The age and number of dependents are: \"\n", + "print age,\" \",dependents,\"\\n\"\n", + "print \"The salary and bonus are: \"\n", + "print \"%.6f\" %salary,\" \",\"%.6f\" %bonus" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Here are the initials: \n", + "E W C \n", + "\n", + "The age and number of dependents are: \n", + "32 2 \n", + "\n", + "The salary and bonus are: \n", + "25000.000000 575.250000\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C7SLTXS, Page number:156" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#prompt for a sales amount and print sales tax\n", + "#getting total sale as float number\n", + "#print \"What is the total amount of the sale?\"\n", + "#total_sale=float(raw_input()) \n", + "total_sale=10\n", + "#compute sales tax\n", + "stax=total_sale*0.07 \n", + "#Result\n", + "print \"The sales tax for\",float(round(total_sale,3)),\"is\",round(stax,3)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sales tax for 10.0 is 0.7\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter2_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter2_3.ipynb new file mode 100755 index 00000000..16b4b2bc --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter2_3.ipynb @@ -0,0 +1,756 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:5641a94ed238f5f3bc00f944b1535145b962a2fb8c9b581876b500af4b0fc420" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2: Using C++ Operators" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C8NEG :Page 166" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "temp=-12\n", + "#Result\n", + "print -temp" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "12\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C8DIV :Page 167" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To compute weekly pay\n", + "#Get input\n", + "#yearly=input(\"What is your annual pay?\")\n", + "yearly=38000.00\n", + "weekly=yearly/52\n", + "#Result\n", + "print \"\\nYour weekly pay is \",float(weekly)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Your weekly pay is 730.769230769\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C8AVG1 :Page 172" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Compute the average of three grades\n", + "grade1=87.5\n", + "grade2=92.4\n", + "grade3=79.6\n", + "#Average calculation\n", + "avg=grade1+grade2+grade3/3.0\n", + "#Result\n", + "print \"The average is: \",avg" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The average is: 206.433333333\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C8DATA :Page 179" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Declaration\n", + "bonus=50\n", + "salary=1400.50\n", + "#Calculation\n", + "total=salary+bonus\n", + "#Result\n", + "print \"The total is \",\"%.2f\" %total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The total is 1450.50\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C8INT1 :Page 181" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate interest\n", + "days=45\n", + "principle=3500.00\n", + "interest_rate=0.155\n", + "#daily interest rate\n", + "daily_interest=interest_rate/365 \n", + "daily_interest=principle*daily_interest*days\n", + "#Update principle\n", + "principle+=daily_interest \n", + "#Result\n", + "print \"The balance you owe is:\",\"%.2f\" %principle" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The balance you owe is: 3566.88\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9PAY1 :Page 193" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Calculate salesperson's pay based on his or her sales\n", + "print \"Payroll Calculation\\n\"\n", + "print \"------------------------\\n\"\n", + "#Get input\n", + "sal_name=raw_input(\"What is salesperson's last name? \")\n", + "hours=input(\"How many hours did the salesperson work? \")\n", + "total_sales=input(\"What were the total sales? \")\n", + "bonus=0\n", + "#Compute base pay\n", + "pay=4.10*float(hours) \n", + "if total_sales>8500.00:\n", + " bonus=500.00\n", + "#Result\n", + "print sal_name,\"made $\",\"%.2f\" %pay, \"\\n and got a bonus of $\",\"%.2f\" %bonus" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Payroll Calculation\n", + "\n", + "------------------------\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is salesperson's last name? Harrison\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many hours did the salesperson work? 40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What were the total sales? 6050.64\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Harrison made $ 164.00 \n", + " and got a bonus of $ 0.00\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9AGE :Page 195" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "age=input(\"What is the student's age?\")\n", + "if age<10:\n", + " print \"\\n*** The age cannot be less than 10 ***\\n\"\n", + " print \"Try again...\\n\"\n", + " age=input(\"What is the student's age?\")\n", + "#Result\n", + "print \"\\nThank you. You entered a valid age.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is the student's age?3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "*** The age cannot be less than 10 ***\n", + "\n", + "Try again...\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is the student's age?21\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Thank you. You entered a valid age.\n" + ] + } + ], + "prompt_number": 26 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9SQR1 :Page 197" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "##Print square of the input value if it is lessthan 180\n", + "#Get input\n", + "num=input(\"What number do you want to see the square of?\")\n", + "if num<=180:\n", + " square=num*num\n", + " print \"The square of \",num,\"is \",square,\"\\n\"\n", + " print \"\\nThank you for requesting square roots.\\n\" \n", + "num=input(\"What number do you want to see the square of?\")\n", + "if num>180:\n", + " import os\n", + " os.system('\\a')\n", + " print \"\\n* Square is not allowed for numbers over 180 *\"\n", + " print \"\\nRun this program again trying a smaller value.\"\n", + "print \"\\nThank you for requesting square roots.\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What number do you want to see the square of?45\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The square of 45 is 2025 \n", + "\n", + "\n", + "Thank you for requesting square roots.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What number do you want to see the square of?212\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "* Square is not allowed for numbers over 180 *\n", + "\n", + "Run this program again trying a smaller value.\n", + "\n", + "Thank you for requesting square roots.\n", + "\n" + ] + } + ], + "prompt_number": 32 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9IFEL1 :Page 200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#num=input(\"What is your answer?\\n\")\n", + "num=1\n", + "if num>0:\n", + " print \"\\nMore than 0\\n\"\n", + "else:\n", + " print \"\\nLess or equal to 0\\n\"\n", + "\n", + "print \"\\nThanks for your time.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "More than 0\n", + "\n", + "\n", + "Thanks for your time.\n" + ] + } + ], + "prompt_number": 33 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9IFEL2 :Page 200" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Test user's first initial and prints a message.\n", + "#Get input\n", + "#last=raw_input(\"\\nWhat is your last name?\\n\")\n", + "last=\"Praveen\"\n", + "#test the initial\n", + "if last[0] <= 'P':\n", + " print \"Your name is early in the alphabet.\\n\"\n", + "else:\n", + " print \"You have to wait a while for Your name to be called\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your name is early in the alphabet.\n", + "\n" + ] + } + ], + "prompt_number": 34 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9PAY2 :Page 201" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#hours=input(\"\\nHow many hours were worked?\")\n", + "#rate=input(\"\\nWhat is the regular hourly pay?\")\n", + "hours=44\n", + "rate=0.20\n", + "#Compute pay\n", + "if hours>50:\n", + " dt=2.0*rate*float(hours-50)\n", + " ht=1.5*rate*10.0\n", + "else:\n", + " dt=0.0\n", + "#Time and a half\n", + "if hours>40:\n", + " ht=1.5*rate*float(hours-40)\n", + "#Regular pay\n", + "if hours>=40:\n", + " rp=40*rate\n", + "else:\n", + " rp=float(hours)*rate\n", + "#Payroll\n", + "pay=dt+ht+rp \n", + "#Result\n", + "print \"\\nThe pay is \",\"%.2f\" %pay" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "The pay is 9.20\n" + ] + } + ], + "prompt_number": 36 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C9SERV :Page 202" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#if...else...if\n", + "#yrs=input(\"\\nHow many years of service?\")\n", + "yrs=25\n", + "if yrs>20:\n", + " print \"\\nGive a gold watch\"\n", + "else:\n", + " if yrs>10:\n", + " print \"\\nGive a paper weight\"\n", + " else:\n", + " print \"\\nGive a pat on the back\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Give a gold watch\n" + ] + } + ], + "prompt_number": 37 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C10YEAR :Page 212" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#To determine if it is Summer Olympics year\n", + "#year=input(\"\\nWhat is a year for the test?\")\n", + "year=2004\n", + "#Test the Year\n", + "if year%4==0 and year%10==0:\n", + " print \"\\nBoth Olympics and U.S. Census!\"\n", + " exit(0)\n", + "if year%4==0:\n", + " print \"\\nSummer Olympics only\"\n", + "else:\n", + " if year%10==0:\n", + " print \"\\nU.S. Census only\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Summer Olympics only\n" + ] + } + ], + "prompt_number": 41 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C10AGE :Page 213" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#age=input(\"\\nWhat is your age?\\n\")\n", + "age=20\n", + "if age<10 or age>100:\n", + " print \"*** The age must be between 10 and 100 ***\\n\"\n", + "else:\n", + " print \"You entered a valid age.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You entered a valid age.\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C10VIDEO :Page 214" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# -*- coding: cp1252 -*-\n", + "#Program that computes video rental amounts and gives\n", + "# appropriate discounts based on the day or customer status.\n", + "print \"\\n *** Video Rental Computation ***\\n\"\n", + "print \"--------------------------------------\\n\"\n", + "tape_charge=2.00 #Before-discount tape fee-per tape.\n", + "first_name=raw_input(\"\\nWhat is customer's first name? \")\n", + "last_name=raw_input(\"\\nWhat is customer's last name? \")\n", + "num_tapes=input(\"\\nHow many tapes are being rented? \")\n", + "val_day=raw_input(\"Is this a Value day (Y/N)?\")\n", + "sp_stat=raw_input(\"Is this a Special Status customer (Y/N)?\")\n", + "\n", + "# Calculate rental amount.\n", + "discount=0.0\n", + "if val_day=='Y' or sp_stat=='Y':\n", + " discount=0.5\n", + " x=num_tapes*tape_charge\n", + " y=discount*num_tapes\n", + " rental_amt=x-y\n", + "print \"\\n** Rental club **\\n\"\n", + "print first_name,last_name,\"rented \",num_tapes,\" tapes \"\n", + "print \"The total was \",\"%.2f\" %rental_amt\n", + "print \"The discount was \",\"%.2f\" %discount,\"per tape\\n\"\n", + "if sp_stat=='Y':\n", + " print \"\\nThank them for being a special Status customer\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + " *** Video Rental Computation ***\n", + "\n", + "--------------------------------------\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "What is customer's first name? Jerry\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "What is customer's last name? Parker\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "How many tapes are being rented? 3\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Is this a Value day (Y/N)?Y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Is this a Special Status customer (Y/N)?Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "** Rental club **\n", + "\n", + "Jerry Parker rented 3 tapes \n", + "The total was 4.50\n", + "The discount was 0.50 per tape\n", + "\n", + "\n", + "Thank them for being a special Status customer\n", + "\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter3_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter3_3.ipynb new file mode 100755 index 00000000..7bf3f7ef --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter3_3.ipynb @@ -0,0 +1,1969 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:2f60b7cc87bacc3ca0350839f24c16d5959454ed43a60a8db422fa8483e17f84" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3: 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", + "import sys\n", + "x=0.0\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", + "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": [ + "ans=raw_input(\"Do you want to continue (Y/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", + "name=raw_input(\"What is your first name?\")\n", + "count=len(name)\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": [ + "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": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C12CNT1, Page number:261" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ctr=0\n", + "while ctr<10:\n", + " print \"Computers are fun!\\n\"\n", + " ctr+=1" + ], + "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", + "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)" + ], + "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", + "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)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", + "#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 loop example\n", + "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", + "total=0\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", + " 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", + "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", + " for num in range(1,6,1):\n", + " print num,\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 loop\n", + "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": 6 + }, + { + "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", + "for fact in range(1,num+1,1):\n", + " total=total*fact \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\") #Get age\n", + "while age<=0:\n", + " print \"*** Your age cannot be that small ! ***\"\n", + " for outer in range(1,3,1): #outer loop\n", + " for inner in range(1,50,1): #inner loop\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 #break statement\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", + "print \"Here are the numbers from 1 to 20\\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", + "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", + "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", + "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", + "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", + "avg=0.0\n", + "total=0.0\n", + "month=1.0\n", + "count=0\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", + "num=input(\"Please enter a number:\")\n", + "#Use multiple if statements to beep.\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": 4 + }, + { + "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", + "daily=2343.34\n", + "weekly=13432.65\n", + "monthly=43468.97\n", + "ans=raw_input(\"Is this the end of the month? (Y/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 " + ], + "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", + "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", + "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 diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter4_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter4_3.ipynb new file mode 100755 index 00000000..af8938df --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter4_3.ipynb @@ -0,0 +1,1073 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:4df8567d0a6ab5b81136865656edfa550f51aea32b240bc480ecbc37fd6ab9bf" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4: Variable Scope and Modular Programming" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C16FUN1, Page number:338" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Function Definition\n", + "def next_fun(): \n", + " print \"Inside next_fun()\" \n", + "def third_fun(): \n", + " print \"Inside third_fun()\"\n", + "def main(): \n", + " print \"First function called main()\"\n", + " #Function Call\n", + " next_fun() \n", + " third_fun() \n", + " print \"main() is completed\"\n", + "#Function Call\n", + "main() " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "First function called main()\n", + "Inside next_fun()\n", + "Inside third_fun()\n", + "main() is completed\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C16FUN2, Page number:347" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Function Calls\n", + "def name_print():\n", + " print \"C++ is Fun!\\tC++ is Fun!\\tC++ is Fun!\"\n", + " print \" C++ i s F u n ! \\t C++ i s F u n ! \\t C++ i s F u n ! \"\n", + " reverse_print() \n", + "def reverse_print():\n", + " print \"!nuF si ++C\\t!nuF si ++C\\t!nuF si ++C\"\n", + "def one_per_line():\n", + " print \"C++\\n i\\n s\\n F\\n u\\n n\\n !\"\n", + "def main():\n", + " for ctr in range(1,6,1):\n", + " name_print() #Calls function five times.\n", + " one_per_line() #Calls the program's last function once \n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n", + " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n", + "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n", + "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n", + " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n", + "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n", + "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n", + " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n", + "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n", + "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n", + " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n", + "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n", + "C++ is Fun!\tC++ is Fun!\tC++ is Fun!\n", + " C++ i s F u n ! \t C++ i s F u n ! \t C++ i s F u n ! \n", + "!nuF si ++C\t!nuF si ++C\t!nuF si ++C\n", + "C++\n", + " i\n", + " s\n", + " F\n", + " u\n", + " n\n", + " !\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17GLO, Page number:356" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def do_fun():\n", + " global sales #global variable\n", + " global profit #global variable \n", + " sales = 20000.00\n", + " profit=5000.00\n", + " print \"The sales in the second function are: \",sales\n", + " print \"The profit in the second function is: \",profit\n", + " third_fun() #Call third function to show that globals are visible\n", + "def third_fun():\n", + " print \"\\nIn the third function:\"\n", + " print \"The sales in the third function are\",sales\n", + " print \"The profit in the third function is \",profit\n", + "def main():\n", + " print \"No variable defined in main()\\n\"\n", + " do_fun()\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "No variable defined in main()\n", + "\n", + "The sales in the second function are: 20000.0\n", + "The profit in the second function is: 5000.0\n", + "\n", + "In the third function:\n", + "The sales in the third function are 20000.0\n", + "The profit in the third function is 5000.0\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17GLLO, page number:358" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def pr_again():\n", + " j=5 #Local to only pr_again().\n", + " print j,\",\",z,\",\",i\n", + "global i\n", + "i=0\n", + "def main():\n", + " p=9.0 #Local to main() only\n", + " print i,\",\",p\n", + " pr_again() #Calls next function.\n", + "\n", + "global z\n", + "z=9.0\n", + "main() " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 , 9.0\n", + "5 , 9.0 , 0\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17LOC1, Page number:359" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " age=input(\"What is your age? \") #Variable age is local to main()\n", + " get_age()\n", + " print \"main()'s age is still\",age \n", + "def get_age():\n", + " age=input(\"What is your age again? \") #A different age. This one is local to get_age().\n", + "main() " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your age? 28\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your age again? 56\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "main()'s age is still 28\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17LOC2, Page number:360" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " for ctr in range(0,11,1): #Loop counter\n", + " print \"main()'s ctr is \",ctr,\"\\n\"\n", + " do_fun() #Call second function\n", + "\n", + "def do_fun():\n", + " for ctr in range(10,0,-1):\n", + " print \"do_fun()'s ctr is \",ctr,\"\\n\"\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "main()'s ctr is 0 \n", + "\n", + "main()'s ctr is 1 \n", + "\n", + "main()'s ctr is 2 \n", + "\n", + "main()'s ctr is 3 \n", + "\n", + "main()'s ctr is 4 \n", + "\n", + "main()'s ctr is 5 \n", + "\n", + "main()'s ctr is 6 \n", + "\n", + "main()'s ctr is 7 \n", + "\n", + "main()'s ctr is 8 \n", + "\n", + "main()'s ctr is 9 \n", + "\n", + "main()'s ctr is 10 \n", + "\n", + "do_fun()'s ctr is 10 \n", + "\n", + "do_fun()'s ctr is 9 \n", + "\n", + "do_fun()'s ctr is 8 \n", + "\n", + "do_fun()'s ctr is 7 \n", + "\n", + "do_fun()'s ctr is 6 \n", + "\n", + "do_fun()'s ctr is 5 \n", + "\n", + "do_fun()'s ctr is 4 \n", + "\n", + "do_fun()'s ctr is 3 \n", + "\n", + "do_fun()'s ctr is 2 \n", + "\n", + "do_fun()'s ctr is 1 \n", + "\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17MULI, Page number:362" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " i=10\n", + " i=20\n", + " print i,\" \",i,\"\\n\"\n", + " i=30\n", + " print i,\" \",i,\" \",i,\"\\n\" \n", + " i=10\n", + " print i,\" \",i,\" \",i \n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "20 20 \n", + "\n", + "30 30 30 \n", + "\n", + "10 10 10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17LOC3, Page number:367" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def pr_init(initial):\n", + " print \"Your initial is \",initial\n", + "def pr_other(age,salary):\n", + " print \"You look young for\",age,\"and \",'%.2f' %salary,\"is a lot of money\"\n", + "initial=raw_input(\"What is your initial?\")\n", + "age=input(\"What is your age?\")\n", + "salary=input(\"What is your salary?\")\n", + "pr_init(initial) #call pr_init() and pass it initial\n", + "pr_other(age,salary) #call pr_other and pass age and salary" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your initial?Jerry\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your age?30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your salary?50000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your initial is Jerry\n", + "You look young for 30 and 50000.00 is a lot of money\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17LOC4, Page number:368" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def compute_sale(gallons):\n", + " #local variable\n", + " price_per=12.45 \n", + " x=price_per*float(gallons) #type casting gallons because it was integer\n", + " print \"The total is \",'%.2f' %x\n", + "def main():\n", + " print \"Richard's Paint Service\"\n", + " gallons=input(\"How many gallons of paint did you buy?\")\n", + " compute_sale(gallons) #Function Call\n", + "main()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Richard's Paint Service\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many gallons of paint did you buy?20\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The total is 249.00\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C17STA2, Page number:372" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " for ctr in range(1,26,1):\n", + " triple_it(ctr)\n", + "def triple_it(ctr):\n", + " total=0\n", + " ans=ctr*3\n", + " total+=ans\n", + " print \"The number \",ctr,\"multiplied by 3 is \",ans\n", + " if total>300:\n", + " print \"The total of triple numbers is over 300\"\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The number 1 multiplied by 3 is 3\n", + "The number 2 multiplied by 3 is 6\n", + "The number 3 multiplied by 3 is 9\n", + "The number 4 multiplied by 3 is 12\n", + "The number 5 multiplied by 3 is 15\n", + "The number 6 multiplied by 3 is 18\n", + "The number 7 multiplied by 3 is 21\n", + "The number 8 multiplied by 3 is 24\n", + "The number 9 multiplied by 3 is 27\n", + "The number 10 multiplied by 3 is 30\n", + "The number 11 multiplied by 3 is 33\n", + "The number 12 multiplied by 3 is 36\n", + "The number 13 multiplied by 3 is 39\n", + "The number 14 multiplied by 3 is 42\n", + "The number 15 multiplied by 3 is 45\n", + "The number 16 multiplied by 3 is 48\n", + "The number 17 multiplied by 3 is 51\n", + "The number 18 multiplied by 3 is 54\n", + "The number 19 multiplied by 3 is 57\n", + "The number 20 multiplied by 3 is 60\n", + "The number 21 multiplied by 3 is 63\n", + "The number 22 multiplied by 3 is 66\n", + "The number 23 multiplied by 3 is 69\n", + "The number 24 multiplied by 3 is 72\n", + "The number 25 multiplied by 3 is 75\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C18PASS1, Page number:381" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def moon(weight): \n", + " weight/=6 \n", + " print \"You weigh only \",weight,\"pounds on the moon !\"\n", + "def main(): \n", + " weight=input(\"How many pounds do you weigh? \")\n", + " moon(weight) #call the moon() function and pass weight\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many pounds do you weigh? 120\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You weigh only 20 pounds on the moon !\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C18PASS3, Page number:383" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def main():\n", + " Igrade=raw_input(\"What letter grade do you want?\")\n", + " average=input(\"What is your current test average\")\n", + " tests=input(\"How many tests do you have left?\")\n", + " check_grade(Igrade,average,tests) #// Calls function and passes three variables by value\n", + "def check_grade(Igrade,average,tests):\n", + " if tests==0:\n", + " print \"You will get your current grade of \",Igrade\n", + " else:\n", + " if tests==1:\n", + " print \"You still have time to bring up your average of\",'%.1f' %average,\"up . Study hard !\"\n", + " else :\n", + " print \"Relax. You still have plenty of time.\" \n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What letter grade do you want?A\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your current test average1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many tests do you have left?3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Relax. You still have plenty of time.\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19AVG, Page number:398" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def calc_av(num1,num2,num3):\n", + " local_avg=(num1+num2+num3) / 3 #Holds the average for these numbers\n", + " return local_avg\n", + "print \"please type three numbers (such as 23 54 85) \"\n", + "num1=input()\n", + "num2=input()\n", + "num3=input()\n", + "avg=calc_av(num1,num2,num3) #call function and pass the numbers\n", + "print \"\\n\\nThe average is \",avg #Print the return value" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "please type three numbers (such as 23 54 85) \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "30\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "40\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "The average is 40\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19DOUB, Page number:401" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def doub(number):\n", + " d_num=number*2 #Doubles the number.\n", + " return d_num #Returns the result.\n", + "number=input(\"What number do you want doubled? \")\n", + "d_number= doub(number) #Assigns return value.\n", + "print number,\" doubled is \",d_number" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What number do you want doubled? 5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "5 doubled is 10\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19SUMD, Page number:403" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def sums(num):\n", + " sumd=0\n", + " if num<=0:\n", + " sumd=num\n", + " else:\n", + " for ctr in range(1,num+1,1):\n", + " sumd=sumd+ctr\n", + " return sumd\n", + "num=input(\"Please type a number: \")\n", + "sumd= sums(num)\n", + "print \"The sum of the digits is \" , sumd" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please type a number: 6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The sum of the digits is 21\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19MINMX, Page number:404" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def maximum(num1,num2): \n", + " if num1>num2:\n", + " maxi=num1\n", + " else:\n", + " maxi=num2\n", + " return maxi\n", + "def minimum(num1,num2):\n", + " if num1>num2:\n", + " mini=num2\n", + " else:\n", + " mini=num1\n", + " return mini\n", + "print \"Please type two numbers ( such as 46 75 ) \"\n", + "num1 = input()\n", + "num2 = input()\n", + "maxi=maximum(num1,num2) #Assign the return value of each function to variables\n", + "mini=minimum(num1,num2) \n", + "print \"The minimum number is \",mini\n", + "print \"The maximum number is \", maxi" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Please type two numbers ( such as 46 75 ) \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "72\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "55\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The minimum number is 55\n", + "The maximum number is 72\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19PRO1, Page number:409" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "tax_rate=0.07 #Assume seven percent tax rate\n", + "total_sale=input(\"What is the sale amount? \")\n", + "total_sale+=tax_rate*total_sale\n", + "print \"The total sale is \",'%.2f' %total_sale" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is the sale amount? 4000\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The total sale is 4280.00\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19ASC, Page number:410" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def ascii(num):\n", + " asc_char=chr(num) #Type cast to a character\n", + " return asc_char\n", + "num=input(\"Enter an ASCII number? \")\n", + "asc_char=ascii(num) #Number is passed to the function ascii()\n", + "print \"The ASCII character for \",num,\"is \",asc_char" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an ASCII number? 67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The ASCII character for 67 is C\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C19NPAY, Page number:411" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def netpayfun(hours,rate,taxrate):\n", + " gross_pay=hours*rate\n", + " taxes=taxrate*gross_pay\n", + " net_pay=gross_pay-taxes\n", + " return net_pay\n", + "net_pay=netpayfun(40.0,3.50,0.20)\n", + "print \"The pay for 40 hours at $3.50/hr., and a 20% tax rate is $ \",net_pay\n", + "net_pay=netpayfun(50.0,10.00,0.30)\n", + "print \"The pay for 40 hours at $10.00/hr., and a 30% tax rate is $ \",net_pay\n", + "net_pay=netpayfun(10.0,5.00,0.10)\n", + "print \"The pay for 40 hours at $5.00/hr., and a 10% tax rate is $ \",net_pay" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The pay for 40 hours at $3.50/hr., and a 20% tax rate is $ 112.0\n", + "The pay for 40 hours at $10.00/hr., and a 30% tax rate is $ 350.0\n", + "The pay for 40 hours at $5.00/hr., and a 10% tax rate is $ 45.0\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C20OVF1, Page number:423" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "i=-15\n", + "x=-64.53\n", + "ians=abs(i) #abs() function is a built in function that returns a positive value \n", + "print \"Integer absolute value of -15 is \",ians\n", + "fans=abs(x)\n", + "print \"Float absolute value of -64.53 is \",'%.2f' %fans" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Integer absolute value of -15 is 15\n", + "Float absolute value of -64.53 is 64.53\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C20OVF2, Page number:424" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#function definition\n", + "def output(x):\n", + " if isinstance(x,int):\n", + " print x\n", + " else:\n", + " if isinstance(x,float):\n", + " print '%.2f' %x\n", + " else:\n", + " print x\n", + "#Variable Decleration\n", + "name=\"C++ By Example makes C++ easy!\"\n", + "Ivalue=2543\n", + "fvalue=39.4321\n", + "#calling function\n", + "output(name)\n", + "output(Ivalue)\n", + "output(fvalue)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++ By Example makes C++ easy!\n", + "2543\n", + "39.43\n" + ] + } + ], + "prompt_number": 19 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter5_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter5_3.ipynb new file mode 100755 index 00000000..92b07a7b --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter5_3.ipynb @@ -0,0 +1,184 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:1663500e8fbe5ad2cc16ca34f15e5b3a438d37780443ea138d094544e9484c86" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 5: Character Input/Output and String Functions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C22INI, Page number:452" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "print \"What is your first initial?\"\n", + "initial=raw_input()\n", + "\n", + "#Check if it is an alphabet\n", + "while not initial.isalpha():\n", + " print \"That was not a valid initial!\"\n", + " print \"What is your first initial?\"\n", + " initial=raw_input()\n", + "print \"Thanks\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is your first initial?\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "p\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Thanks\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C22GB, Page number:454" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#ans=raw_input(\"Are you a girl or a boy (G/B)? \") \n", + "ans=\"b\"\n", + "#convert answer to uppercase\n", + "ans=ans.upper() \n", + "if ans=='G':\n", + " print \"You look pretty today!\\n\"\n", + "else:\n", + " if ans=='B':\n", + " print \"You look handsome today!\\n\"\n", + " else:\n", + " print \"Your answer makes no sense!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "You look handsome today!\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C22GPS1, Page number:459" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#get book title\n", + "#book=raw_input(\"What is the book title? \") \n", + "book=\"Mary and Her Lambs\"\n", + "#print book title\n", + "print book \n", + "print \"Thanks for the book!\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Mary and Her Lambs\n", + "Thanks for the book!\n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C22ABS, Page number:463" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Get input\n", + "#age1=input(\"\\nWhat is the first child's age? \")\n", + "#age2=input(\"\\nWhat is the second child's age? \")\n", + "age1=10\n", + "age2=12\n", + "diff=age1-age2 \n", + "# abs() function determines absolute value\n", + "diff=abs(diff) \n", + "#Result\n", + "print \"\\nThey are \",diff,\" years apart.\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "They are 2 years apart.\n" + ] + } + ], + "prompt_number": 4 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter6_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter6_3.ipynb new file mode 100755 index 00000000..0905d8e9 --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter6_3.ipynb @@ -0,0 +1,1457 @@ +{ + "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]<low_val:\n", + " low_val=ara[ctr] #Stores lower valure in low_val\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", + "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", + "parts=[None]*MAX\n", + "num_parts=5 #Beginning inventory count\n", + "fill_parts(parts) #Fills the first five elements\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", + " 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 " + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\n", + "Please type a part number...(-9999 ends program)" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34234\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 34234 was added to inventory\n", + "\n", + "\n", + "\n", + "Please type a part number...(-9999 ends program)" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "83925\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "Part 83925 is already in inventory\n", + "\n", + "\n", + "Please type a part number...(-9999 ends program)" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "52786\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 52786 was added to inventory\n", + "\n", + "\n", + "\n", + "Please type a part number...(-9999 ends program)" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-9999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C24SORT1, Page number:504" + ] + }, + { + "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]>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]<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", + "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", + "#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", + "disks[1][0]=1.75\n", + "disks[1][1]=2.19\n", + "disks[1][2]=2.69\n", + "disks[1][3]=2.95\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", + "#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", + "disks[1][0]=1.75\n", + "disks[1][1]=2.19\n", + "disks[1][2]=2.69\n", + "disks[1][3]=2.95\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", + "#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", + "disks[1][0]=1.75\n", + "disks[1][1]=2.19\n", + "disks[1][2]=2.69\n", + "disks[1][3]=2.95\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", + "#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", + "#Variable declaration\n", + "i=10\n", + "j=20\n", + "print \"\\nBefore swap, i is \",i,\"and j is \",j,\"\\n\"\n", + "i,j=swap_them(i,j) # Function call\n", + "print \"\\nAfter swap, i is \",i,\"and j is \",j,\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Before swap, i is 10 and j is 20 \n", + "\n", + "\n", + "After swap, i is 20 and j is 10 \n", + "\n" + ] + } + ] + }, + { + "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", + "#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", + "print \"My sister's maiden was \",c,\"\\n\"\n", + "#Assigns new string to c\n", + "c=\"Bettye Lou Henderson\" \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", + "print \"My sister's maiden was \",c,\"\\n\"\n", + "c+=str(11) #makes c points to the last name\n", + "c=\"Henderson\" #Assigns new string to c\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", + "#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 diff --git a/C++_By_Example_by_Greg__M._Perry/Chapter7_3.ipynb b/C++_By_Example_by_Greg__M._Perry/Chapter7_3.ipynb new file mode 100755 index 00000000..4f861159 --- /dev/null +++ b/C++_By_Example_by_Greg__M._Perry/Chapter7_3.ipynb @@ -0,0 +1,1046 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:75d9b3e202f1b623d68374a7f029f2643cd152423fdceca49ae347a494dcf861" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7: Structures and File Input/Output" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C28ST1 :Page 592" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from collections import namedtuple\n", + "#Structure declaration and definition\n", + "struct_cd_collection = namedtuple(\"struct_cd_collection\", \"title artist num_songs price date_purch\")\n", + "#Structure for Cd Collection\n", + "cd1 = struct_cd_collection(\"Red Moon Men\", \"Sam and the Sneeds\", 12,11.95,\"02/13/92\")\n", + "\n", + "#Result\n", + "print \"Here is the CD information :\\n\\n\"\n", + "print \"Title:\",cd1.title,\"\\n\"\n", + "print \"Artist:\",cd1.artist,\"\\n\"\n", + "print \"Songs:\",cd1.num_songs,\"\\n\"\n", + "print \"Price:\",cd1.price,\"\\n\"\n", + "print \"Date purchased:\",cd1.date_purch,\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Here is the CD information :\n", + "\n", + "\n", + "Title: Red Moon Men \n", + "\n", + "Artist: Sam and the Sneeds \n", + "\n", + "Songs: 12 \n", + "\n", + "Price: 11.95 \n", + "\n", + "Date purchased: 02/13/92 \n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C28ST2 :Page 593" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from collections import namedtuple\n", + "\n", + "#Structure initial declaration\n", + "students = namedtuple(\"students\", \"name age average\")\n", + "#Get data\n", + "print \"What is first student's name? \",\n", + "x=raw_input()\n", + "print \"What is the first student's age? \",\n", + "y=input()\n", + "print \"What is the first student's average ? \",\n", + "z=input()\n", + "#Assign data to the structure variable\n", + "student1=students(x,y,z)\n", + "print \"\\n\"\n", + "#Get data\n", + "print \"What is second student's name? \",\n", + "x=raw_input()\n", + "print \"What is the second student's age? \",\n", + "y=input()\n", + "print \"What is the second student's average ? \",\n", + "z=input()\n", + "#Assign data to the structure variable\n", + "student2=students(x,y,z)\n", + "#Result\n", + "print \"\\n\\nHere is the student information you entered:\\n\\n\"\n", + "print \"Student #1:\\n\"\n", + "print \"Name: \",student1.name,\"\\n\"\n", + "print \"Age: \",student1.age,\"\\n\"\n", + "print \"Average: \",'%.2f'%student1.average,\"\\n\"\n", + "print \"Student #2:\\n\"\n", + "print \"Name: \",student2.name,\"\\n\"\n", + "print \"Age: \",student2.age,\"\\n\"\n", + "print \"Average: \",'%.2f'%student2.average,\"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is first student's name? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Larry\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the first student's age? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the first student's average ? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87.67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "What is second student's name? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Judy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the second student's age? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the second student's average ? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "95.38\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Here is the student information you entered:\n", + "\n", + "\n", + "Student #1:\n", + "\n", + "Name: Larry \n", + "\n", + "Age: 14 \n", + "\n", + "Average: 87.67 \n", + "\n", + "Student #2:\n", + "\n", + "Name: Judy \n", + "\n", + "Age: 15 \n", + "\n", + "Average: 95.38 \n", + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C28ST3 :Page 596" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Function definitions\n", + "def fill_structs(student_var):\n", + " #Get student data\n", + " print \"What is student's name? \",\n", + " x=raw_input()\n", + " print \"What is the student's age? \",\n", + " y=input()\n", + " print \"What is the student's average ? \",\n", + " z=input()\n", + " student_var=students(x,y,z)\n", + " return student_var\n", + "def pr_students(student_var):\n", + " print \"Name: \",student_var.name,\"\\n\",\n", + " print \"Age: \",student_var.age,\"\\n\",\n", + " print \"Average: \",student_var.average,\"\\n\",\n", + "#Structure declaration\n", + "from collections import namedtuple\n", + "students=namedtuple(\"students\",\"name age average\")\n", + "#Structure variable declaration\n", + "student1=students(\" \",0,0.0)\n", + "student2=students(\" \",0,0.0)\n", + "#Stucture variable is passed as copy to the function\n", + "student1=fill_structs(student1)\n", + "student2=fill_structs(student2)\n", + "#Result\n", + "print \"\\n\\nHere is the student information you entered:\\n\\n\"\n", + "pr_students(student1)\n", + "pr_students(student2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is student's name? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Larry\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the student's age? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "14\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the student's average ? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "87.67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is student's name? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Judy\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the student's age? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "15\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " What is the student's average ? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "97.38\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " \n", + "\n", + "Here is the student information you entered:\n", + "\n", + "\n", + "Name: Larry \n", + "Age: 14 \n", + "Average: 87.67 \n", + "Name: Judy \n", + "Age: 15 \n", + "Average: 97.38 \n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C28CUST :Page 598" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Structure definition\n", + "from collections import namedtuple\n", + "customer_rec=namedtuple(\"customer_rec\",\"cust_name balance dist_rate\")\n", + "customer=customer_rec(\"Steve Thompson\",431.23,.25)\n", + "print \"Before the update\",customer.cust_name,\" has a balance of $\",'%.2f' %customer.balance,\"\\n\",\n", + "#Update the balance\n", + "customer_rec.balance=customer.balance*(1.0-customer.dist_rate)\n", + "#Result\n", + "print \"After the update\",customer.cust_name,\" has a balance of $\",'%.2f' %customer.balance,\"\\n\"," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before the update Steve Thompson has a balance of $ 431.23 \n", + "After the update Steve Thompson has a balance of $ 323.42 \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C28STCPY :Page 599" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Structure declaration\n", + "from collections import namedtuple\n", + "students=namedtuple(\"students\",\"st_name grade age average\")\n", + "#Structure variable declaration\n", + "std1=students(\"Joe Brown\",'A',13,91.4)\n", + "std2=students(\" \",\" \",0,0.0)\n", + "std3=students(\" \",\" \",0,0.0)\n", + "#Assigning one structure variable to another.\n", + "std2=std1\n", + "std3=std1\n", + "#Result\n", + "print \"The contents of std2:\\n\",\n", + "print std2.st_name,\", \",std2.grade,\", \",std2.age,\", \",'%.1f' %std2.average,\"\\n\\n\"\n", + "print \"The contents of std3:\\n\",\n", + "print std3.st_name,\", \",std3.grade,\", \",std3.age,\", \",'%.1f' %std3.average,\"\\n\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The contents of std2:\n", + "Joe Brown , A , 13 , 91.4 \n", + "\n", + "\n", + "The contents of std3:\n", + "Joe Brown , A , 13 , 91.4 \n", + "\n", + "\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C30WR1 :Page 635" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#open a new file in write mode\n", + "fp = open (\"NAMES.txt\", \"w\" ) \n", + "fp.writelines(\"Michel Langston\\n\")\n", + "fp.writelines(\"Sally Redding\\n\")\n", + "fp.writelines(\"Jane Kirk\\n\")\n", + "fp.writelines(\"Stacy Wikert\\n\")\n", + "fp.writelines(\"Joe Hiquet\\n\")\n", + "#Close file\n", + "fp.close()\n", + "#Result\n", + "print \"names were written into the file\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "names were written into the file\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C30WR2 :Page 636" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#Creates a file in write mode\n", + "fp = open ( \"NUMS.1.txt\", \"w\" ) \n", + "if not fp:\n", + " print \"Error opening file.\\n\"\n", + "else:\n", + " for ctr in range(1,101,1):\n", + " fp.write('%d' %ctr) #Writes number from 1-100 into the file\n", + "fp.close()\n", + "#Result\n", + "print \"ctr value is written into the file\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "ctr value is written into the file\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C30AP1 :Page 638" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#opens existing file\n", + "fp = open ( \"NAMES.txt\", \"a\" ) \n", + "#Adds to file\n", + "fp.writelines(\"Johnny Smith\\n\")\n", + "fp.writelines(\"Laura Hull \\n\")\n", + "fp.writelines(\"Mark Brown\\n\")\n", + "#Close file\n", + "fp.close()\n", + "#Result\n", + "print \"Names are added to the existing file\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Names are added to the existing file\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C30RE2 :Page 641" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#File1 to take backup\n", + "print \"What is the name of the file you want to back up? \"\n", + "in_filename=raw_input()\n", + "#File2 to copy contents\n", + "print \"What is the name of the file you want to copy \",in_filename,\"to? \"\n", + "out_filename=raw_input()\n", + "in_fp=open(in_filename,\"r\")\n", + "if not in_fp:\n", + " print \"\\n\\n*** \",in_filename,\"does not exist***\\n\"\n", + " exit(0)\n", + "else:\n", + " out_fp=open(out_filename,\"w\")\n", + " if not out_fp:\n", + " print \"\\n\\n*** Error opening \",out_filename,\"***\\n\"\n", + " exit(0)\n", + " else:\n", + " print \"\\nCopying...\\n\" #Reads from old file \n", + " for in_char in in_fp.readlines(): #and copies it to new file\n", + " out_fp.writelines(in_char)\n", + " print \"\\nThe file is copied.\\n\"\n", + "#Close all files\n", + "in_fp.close()\n", + "out_fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is the name of the file you want to back up? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "NAMES.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "What is the name of the file you want to copy NAMES.txt to? \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "NEW_NAMES.txt\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Copying...\n", + "\n", + "\n", + "The file is copied.\n", + "\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32CON :Page 664" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "class Sphere: #Class declaration\n", + " def __init__(self, xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " \n", + " def volume(self):\n", + " return r*r*r*4*PI/3\n", + "\n", + " def surface_area(self):\n", + " return r*r*4*PI\n", + "def main():\n", + " s = Sphere(1.0,2.0,3.0,4.0) #Class object \n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r #Result\n", + "\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32DES :Page 665" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class object\n", + " s = Sphere(1.0,2.0,3.0,4.0) \n", + " #Result\n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n", + "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32MEM :Page 667" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class object\n", + " s = Sphere(1.0,2.0,3.0,4.0) \n", + " #Result\n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + " print \"The volume is \",s.volume(),\"\\n\",\n", + " print \"The surface area is \",s.surface_area(),\"\\n\",\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n", + "The volume is 268.082346667 \n", + "The surface area is 201.06176 \n", + "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32MEM1 :Page 668" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class object\n", + " s = Sphere(1.0,2.0,3.0,4.0) \n", + " #Result\n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + " print \"The volume is \",s.volume(),\"\\n\", \n", + " print \"The surface area is \",s.surface_area(),\"\\n\",\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n", + "The volume is 268.082346667 \n", + "The surface area is 201.06176 \n", + "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32MEM1A :Page 669" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class object\n", + " s = Sphere(1.0,2.0,3.0,4.0) \n", + " #Result\n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + " print \"The volume is \",(s.r*s.r*s.r*4*PI/3),\"\\n\", #volume() function is expanded here\n", + " print \"The surface area is \",s.surface_area(),\"\\n\",\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n", + "The volume is 268.082346667 \n", + "The surface area is 201.06176 \n", + "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32DEF :Page 671" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord=2.0,zcoord=2.5,radius=1.0):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class objects\n", + " s = Sphere(1.0) \n", + " t = Sphere(1.0,1.1)\n", + " u = Sphere(1.0,1.1,1.2)\n", + " v = Sphere(1.0,1.1,1.2,1.3)\n", + " #Result\n", + " print \"s: X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + " print \"The volume is \",s.volume(),\"\\n\", \n", + " print \"The surface area is \",s.surface_area(),\"\\n\",\n", + " print \"t: X=\",t.x,\", Y=\",t.y,\", Z=\",t.z,\", R=\",t.r \n", + " print \"The volume is \",t.volume(),\"\\n\", \n", + " print \"The surface area is \",t.surface_area(),\"\\n\",\n", + " print \"u: X=\",u.x,\", Y=\",u.y,\", Z=\",u.z,\", R=\",u.r \n", + " print \"The volume is \",u.volume(),\"\\n\", \n", + " print \"The surface area is \",u.surface_area(),\"\\n\",\n", + " print \"v: X=\",v.x,\", Y=\",v.y,\", Z=\",v.z,\", R=\",v.r \n", + " print \"The volume is \",v.volume(),\"\\n\", \n", + " print \"The surface area is \",v.surface_area(),\"\\n\",\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "s: X= 1.0 , Y= 2.0 , Z= 2.5 , R= 1.0\n", + "The volume is 4.18878666667 \n", + "The surface area is 12.56636 \n", + "t: X= 1.0 , Y= 1.1 , Z= 2.5 , R= 1.0\n", + "The volume is 4.18878666667 \n", + "The surface area is 12.56636 \n", + "u: X= 1.0 , Y= 1.1 , Z= 1.2 , R= 1.0\n", + "The volume is 4.18878666667 \n", + "The surface area is 12.56636 \n", + "v: X= 1.0 , Y= 1.1 , Z= 1.2 , R= 1.3\n", + "The volume is 9.20276430667 \n", + "The surface area is 21.2371484 \n", + "Sphere( 1.0 , 2.0 , 2.5 , 1.0 ) destroyed\n", + "Sphere( 1.0 , 1.1 , 2.5 , 1.0 ) destroyed\n", + "Sphere( 1.0 , 1.1 , 1.2 , 1.0 ) destroyed\n", + "Sphere( 1.0 , 1.1 , 1.2 , 1.3 ) destroyed\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "C32OVCON :Page 673" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "PI = 3.14159\n", + "#Class declaration\n", + "class Sphere: \n", + " def __init__( self,xcoord,ycoord,zcoord,radius):\n", + " self.r = radius\n", + " self.x = xcoord\n", + " self.y = ycoord\n", + " self.z = zcoord\n", + " def __del__(self):\n", + " print \"Sphere(\",self.x,\",\" ,self.y,\",\",self.z,\",\",self.r,\") destroyed\"\n", + " def volume(self):\n", + " return self.r*self.r*self.r*4*PI/3\n", + " def surface_area(self):\n", + " return self.r*self.r*4*PI\n", + "def main():\n", + " #Class object\n", + " s = Sphere(1.0,2.0,3.0,4.0) \n", + " #Result\n", + " print \"X=\",s.x,\", Y=\",s.y,\", Z=\",s.z,\", R=\",s.r \n", + " print \"The volume is \",(s.r*s.r*s.r*4*PI/3),\"\\n\", #volume() function is expanded here\n", + " print \"The surface area is \",s.surface_area(),\"\\n\",\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X= 1.0 , Y= 2.0 , Z= 3.0 , R= 4.0\n", + "The volume is 268.082346667 \n", + "The surface area is 201.06176 \n", + "Sphere( 1.0 , 2.0 , 3.0 , 4.0 ) destroyed\n" + ] + } + ], + "prompt_number": 7 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |