summaryrefslogtreecommitdiff
path: root/C++_from_the_Ground/Chapter_5(1).ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C++_from_the_Ground/Chapter_5(1).ipynb')
-rw-r--r--C++_from_the_Ground/Chapter_5(1).ipynb668
1 files changed, 668 insertions, 0 deletions
diff --git a/C++_from_the_Ground/Chapter_5(1).ipynb b/C++_from_the_Ground/Chapter_5(1).ipynb
new file mode 100644
index 00000000..4ae8f6f6
--- /dev/null
+++ b/C++_from_the_Ground/Chapter_5(1).ipynb
@@ -0,0 +1,668 @@
+{
+ "metadata": {
+ "name": "Chapter 5"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h1>Chapter 5: Arrays ans Strings<h1>"
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.1, Page Number: 82<h3>\n "
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Loads sample with the numbers 0 to 9'''\n\n#Variable declaration\nsample = range(10) #Declares an array [0,1,2,3,4,5,6,7,8,9]\n\n#Displays the array\nfor t in range(10):\n print sample[t],\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 1 2 3 4 5 6 7 8 9\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.2, Page Number: 83<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Maximum and Minimum value from a list'''\n\nimport random\n\n#Variable declaration\nlist = [] #List of integers\n\nfor i in range(10):\n list.append(random.randint(0,1000)) #randomely assigning integers\n \n#Finding the minimum value\nmin_value = list[0]\n\nfor i in range(10):\n if min_value>list[i]:\n min_value=list[i]\nprint \"minimum value: \",min_value #Result:Minimum value\n\n#Finding maximum value\nmax_value = list[0]\n\nfor i in range(10):\n if max_value<list[i]:\n max_value=list[i]\nprint \"maximum value: \",max_value #Result:Maximum value\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "minimum value: 192\nmaximum value: 684\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.3, Page Number: 85<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Bubble Sort'''\n\nimport random\n\n#Variable declaration\nnums = []\nsize = 10\n\n#Initializing list with random numbers\nfor t in range(size):\n nums.append(random.randint(0,1000))\n\n#Displays original array\nprint \"Original array is: \"\nprint nums\n\n#Bubble Sort\nfor a in range(size):\n for b in xrange(size-1,a,-1):\n if nums[b-1]>nums[b]:\n t=nums[b-1]\n nums[b-1]=nums[b]\n nums[b] = t\n\n#Display sorted array\nprint \"Sorted array is: \"\nprint nums\n\n \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Original array is: \n[388, 661, 218, 595, 167, 46, 704, 140, 559, 428]\nSorted array is: \n[46, 140, 167, 218, 388, 428, 559, 595, 661, 704]\n"
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.4, Page Number: 87<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Example for a string; Implementing cin and cout'''\n\n#Variable decleration\nstr = \"Hello\"\n\n#Result\nprint \"Here is your string:\",\nprint str\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Here is your string: Hello\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.5, Page Number: 88<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Example for a string; Implementing gets for input'''\n\nprint \"Enter a string: \"\n\n#user input\nstr = \"Hello\"\n\n#Result\nprint \"Here is your string:\",\nprint str\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a string: \nHere is your string: Hello\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.6, Page Number: 89<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Implementing strcpy'''\n\n#Variable decleration\ns=str\nstr = s #copying s into str\n\n#Result\nprint str\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Hello\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.7, Page Number: 89<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Example for concatenation'''\n\n#Variable decleration\ns1 = \"Hello\"\ns2 = \" There\"\n\n#Concatenation\ns1+=s2\n\n#Result\nprint s1\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Hello There\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.8, Page Number: 90<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n#Example for string compare: Password verification\n\n#Function for password verification\ndef password():\n print \"Enter password: \" #User input for password\n s= \"pass\"\n if s==\"password\":\n return True\n else:\n print \"Invalid password.\"\n return False\n\n#Result\nif password() :\n print \"Logged On.\"\nelse:\n print \"Access denied\"\n \n \n \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter password: \nInvalid password.\nAccess denied\n"
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.9, Page Number: 91<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Takes user input till strings match'''\n\nwhile True:\n s=raw_input(\"Enter a string: \") #User input of string;\n if s==\"quit\": #Sting comparison\n break\n \n \n\n \n \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a string: ddc\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a string: hello\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter a string: quit\n"
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.10, Page Number: 91<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''String length; Implementing strlen'''\n\n#Variable declaration\nstr = \"Hello\"\n\n#Result\nprint \"Length is: \",len(str)\n \n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Length is: 5\n"
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.11, Page Number: 92<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Print a string backwards.'''\n\n#Taking a user input string\nstr = \"Hello World\"\n\n#Reversing a String\nrev = str[::-1]\n\n#Result\nprint rev\n\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "dlroW olleH\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.12, Page Number: 92<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "\n'''Illustrating string functions'''\n\n#Variable declaration\ns1 = \"Hello\"\ns2 = \"there\"\n\n#Printing lengths\nprint \"lengths: \",len(s1),' ',len(s2)\n\n#Comparing\nif(s1==s2):\n print \"The strings are equal\"\nelse:\n print \"not equal\"\n\n#Concatenation\ns1+=s2\nprint s1\n\n\n#Copying\ns1=s2\n\n#Result\nprint s1,\"and\",s2,\" are now the same\"\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "lengths: 5 5\nnot equal\nHellothere\nthere and there are now the same\n"
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.13, Page Number: 93<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Convert a string to upper case'''\n\nimport string\n\n#Variable Initialization\nstr= \"this is a test\"\n\nstr=string.upper(str)\n\n#Result\nprint str\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "THIS IS A TEST\n"
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.14, Page Number: 94<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Load a two-dimensional array with values 1-12'''\n\n#Variable Initialization\narr=[] #The 2-D list\nnew=[] #The nested list\n\n#Initializing the 2-D array\nfor i in range(3):\n new=[]\n for j in range(4):\n new.append(i*4+j+1)\n arr.append(new)\n\n#Result\nfor i in range(3):\n for j in range(4):\n print arr[i][j],\n print",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1 2 3 4\n5 6 7 8\n9 10 11 12\n"
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.15, Page Number: 98<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Looks up a no. in an array n prints the corresponding square'''\n\n#Variabe Initialzation\nsqrs= [[1,1],[2,4],[3,9],[4,16],[5,25],\n [6,36],[7,49],[8,64],[9,81],[10,100]] #Array storing the squares\ni=6 #User input of number whose square is to be looked up\n\n#Search for the number\nfor j in range(10):\n if sqrs[j][0]==i:\n break\n \n#Result\nprint \"The square of \",i,\" is \", sqrs[j][1]",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "The square of 6 is 36\n"
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.16, Page Number: 99<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Demonstating how local strings are initialized each time they are called'''\n\ndef f1():\n s=\"this is a test\" #initial s\n print s\n s=\"CHANGED\" #s is now changed \n print s\n\n#Calling the function twice\nf1()\nf1()",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "this is a test\nCHANGED\nthis is a test\nCHANGED\n"
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.17, Page Number: 101<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''Enter and display strings'''\n\n#Variable decleration\ntext=[]\nstr=['eat','play','work'] #user input of strings\np=len(str)\n\nfor t in range(p):\n print t,\":\"\n text.append(str[t]) #Here, user input taken from the list\n \n#Result; redisplay the strings\nfor i in range(p):\n print text[i]\n ",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0 :\n1 :\n2 :\neat\nplay\nwork\n"
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": "<h3>Example 5.18, Page Number: 103<h3>"
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "'''A simple employee database program'''\nimport random \n\n#Variable decleration\nname=[] #this list holds employee names\nwage=[] #their phone numbers\nphone=[] #hours worked per week\nhours=[] #wage\nnum=0 #User choice\n\n#Menu \ndef menu():\n global num #All options are chosen one by one\n print \"0.Quit.\"\n print \"1.Enter information\"\n print \"2.Report information\"\n print \"Choose one: \"\n num=int(input())\n return num #Return users selction\n\n#Enter information\ndef enter():\n for i in range(10):\n n=raw_input(\"Enter your name: \")\n name.append(n)\n phone.append(int(input(\"Enter your phone number\")))\n hours.append(int(input(\"Enter number of hours worked: \")))\n wage.append(int(input(\"Enter wage: \")))\n\n#Display report\ndef report():\n p=len(name)\n for i in range(p):\n print name[i],' ',phone[i]\n print \"Pay for the week: \",wage[i]*hours[i]\n\n\nwhile True:\n ch=menu() #get selection\n if ch==0:\n break\n elif ch==1:\n enter()\n elif ch==2:\n report()\n else:\n print \"Try again.\"\n if ch==0:\n break\n",
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "1\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Anny\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number987654321\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 50\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Billy\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9456783652\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 50\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Catherene\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9476836578\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 50\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Dolly\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9831356748\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 15\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 50\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Emily\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9576843721\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 15\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 40\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Jack\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9485738567\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 45\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Kevin\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9345678923\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 45\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Lily\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9345672831\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 45\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Monica\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number9475867483\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 50\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your name: Irene\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter your phone number5674356776\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter number of hours worked: 10\n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Enter wage: 20\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "2\n"
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "Anny 987654321\nPay for the week: 500\nBilly 9456783652\nPay for the week: 500\nCatherene 9476836578\nPay for the week: 500\nDolly 9831356748\nPay for the week: 750\nEmily 9576843721\nPay for the week: 600\nJack 9485738567\nPay for the week: 450\nKevin 9345678923\nPay for the week: 450\nLily 9345672831\nPay for the week: 450\nMonica 9475867483\nPay for the week: 500\nIrene 5674356776\nPay for the week: 200\n0.Quit.\n1.Enter information\n2.Report information\nChoose one: \n"
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": "0\n"
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": "",
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file