diff options
Diffstat (limited to 'Practical_C_Programming/Chapter_13_1.ipynb')
-rw-r--r-- | Practical_C_Programming/Chapter_13_1.ipynb | 348 |
1 files changed, 315 insertions, 33 deletions
diff --git a/Practical_C_Programming/Chapter_13_1.ipynb b/Practical_C_Programming/Chapter_13_1.ipynb index 95603acb..e3f18fc3 100644 --- a/Practical_C_Programming/Chapter_13_1.ipynb +++ b/Practical_C_Programming/Chapter_13_1.ipynb @@ -1,6 +1,7 @@ { "metadata": { - "name": "Chapter 13" + "name": "", + "signature": "sha256:e550d25cd0a5a3cbfd731b0cd1d4a1ad11e5182b8955b4cee863068b4cfdaf40" }, "nbformat": 3, "nbformat_minor": 0, @@ -11,25 +12,43 @@ "cell_type": "heading", "level": 1, "metadata": {}, - "source": "Chapter 13: Simple pointers" + "source": [ + "Chapter 13: Simple pointers" + ] }, { "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.1, Page number: 225" + "source": [ + "Example 13.1, Page number: 225" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.1.py\n# To print the value of 'thing_var' and 'thing_ptr'\n\n\n# Variable declaration\nthing_var = 2\n\n# Calculation and result\nprint ('Thing %d' % thing_var)\n\nthing_ptr = thing_var\nthing_ptr = 3\nprint ('Thing %d' % thing_ptr)", + "input": [ + "\n", + "# Variable declaration\n", + "thing_var = 2\n", + "\n", + "# Calculation and result\n", + "print ('Thing %d' % thing_var)\n", + "\n", + "thing_ptr = thing_var\n", + "thing_ptr = 3\n", + "print ('Thing %d' % thing_ptr)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Thing 2\nThing 3\n" + "text": [ + "Thing 2\n", + "Thing 3\n" + ] } ], "prompt_number": 1 @@ -38,19 +57,56 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.2, Page number: 227" + "source": [ + "Example 13.2, Page number: 227" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.2.py\n# To loop through the value of 'count' from 1 to 10\n\n\n# Function declaration\ndef inc_count (count_ptr) :\n count_ptr += 1\n return count_ptr\n\n# Calculation and result\ncount = 0\n\nwhile (count < 10) :\n print ('Count %d\\n' % inc_count (count))\n count += 1", + "input": [ + "\n", + "\n", + "# Function declaration\n", + "def inc_count (count_ptr) :\n", + " count_ptr += 1\n", + " return count_ptr\n", + "\n", + "# Calculation and result\n", + "count = 0\n", + "\n", + "while (count < 10) :\n", + " print ('Count %d\\n' % inc_count (count))\n", + " count += 1" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Count 1\n\nCount 2\n\nCount 3\n\nCount 4\n\nCount 5\n\nCount 6\n\nCount 7\n\nCount 8\n\nCount 9\n\nCount 10\n\n" + "text": [ + "Count 1\n", + "\n", + "Count 2\n", + "\n", + "Count 3\n", + "\n", + "Count 4\n", + "\n", + "Count 5\n", + "\n", + "Count 6\n", + "\n", + "Count 7\n", + "\n", + "Count 8\n", + "\n", + "Count 9\n", + "\n", + "Count 10\n", + "\n" + ] } ], "prompt_number": 2 @@ -59,19 +115,54 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.3, Page number: 230" + "source": [ + "Example 13.3, Page number: 230" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.3.py\n# To print the addresses and elements of a character array\n\n\n# Variable declaration\narray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n\n# Calculation and result\nprint ('&array[index] (array+index) array[index]\\n')\n\nfor index in range (0, 10) :\n print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))", + "input": [ + "\n", + "# Variable declaration\n", + "array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n", + "\n", + "# Calculation and result\n", + "print ('&array[index] (array+index) array[index]\\n')\n", + "\n", + "for index in range (0, 10) :\n", + " print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "&array[index] (array+index) array[index]\n\n0x20916008 0x20916008 0x0\n\n0x20270280 0x20270280 0x1\n\n0x20733728 0x20733728 0x2\n\n0x20916464 0x20916464 0x3\n\n0x20270232 0x20270232 0x4\n\n0x20733560 0x20733560 0x5\n\n0x20270256 0x20270256 0x6\n\n0x20733752 0x20733752 0x7\n\n0x20916512 0x20916512 0x8\n\n0x20916032 0x20916032 0x9\n\n" + "text": [ + "&array[index] (array+index) array[index]\n", + "\n", + "0x20916008 0x20916008 0x0\n", + "\n", + "0x20270280 0x20270280 0x1\n", + "\n", + "0x20733728 0x20733728 0x2\n", + "\n", + "0x20916464 0x20916464 0x3\n", + "\n", + "0x20270232 0x20270232 0x4\n", + "\n", + "0x20733560 0x20733560 0x5\n", + "\n", + "0x20270256 0x20270256 0x6\n", + "\n", + "0x20733752 0x20733752 0x7\n", + "\n", + "0x20916512 0x20916512 0x8\n", + "\n", + "0x20916032 0x20916032 0x9\n", + "\n" + ] } ], "prompt_number": 3 @@ -80,19 +171,35 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.4, Page number: 232" + "source": [ + "Example 13.4, Page number: 232" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.4.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)", + "input": [ + "\n", + "# Variable declaration\n", + "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n", + "index = 0\n", + "\n", + "# Calculation and result\n", + "while (array[index] != 0) :\n", + " index += 1\n", + "\n", + "print ('Number of elements before zero %d\\n' % index)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Number of elements before zero 6\n\n" + "text": [ + "Number of elements before zero 6\n", + "\n" + ] } ], "prompt_number": 4 @@ -101,19 +208,35 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.5, Page number: 232" + "source": [ + "Example 13.5, Page number: 232" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.5.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)", + "input": [ + "\n", + "# Variable declaration\n", + "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n", + "index = 0\n", + "\n", + "# Calculation and result\n", + "while (array[index] != 0) :\n", + " index += 1\n", + "\n", + "print ('Number of elements before zero %d\\n' % index)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Number of elements before zero 6\n\n" + "text": [ + "Number of elements before zero 6\n", + "\n" + ] } ], "prompt_number": 5 @@ -122,19 +245,32 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.6, Page number: 233" + "source": [ + "Example 13.6, Page number: 233" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.6.py\n# To initialize the elements of an array to 0\n\n\n# Variable declaration\ndata = []\n\n# Calculation and result\nfor index in range (0, 10) :\n data.append(0)\nprint (data)", + "input": [ + "\n", + "# Variable declaration\n", + "data = []\n", + "\n", + "# Calculation and result\n", + "for index in range (0, 10) :\n", + " data.append(0)\n", + "print (data)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" + "text": [ + "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" + ] } ], "prompt_number": 6 @@ -143,19 +279,33 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.10, Page number: 238" + "source": [ + "Example 13.10, Page number: 238" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.10.py\n# To split a string of the form 'First/Last' into two strings\n\n\n# Variable declaration\nline = 'Steve/Oualline'\n\n# Calculation and result\nfirst_ptr, last_ptr = line.split('/')\nprint ('First: %s Last: %s\\n' % (first_ptr, last_ptr))", + "input": [ + "\n", + "\n", + "# Variable declaration\n", + "line = 'Steve/Oualline'\n", + "\n", + "# Calculation and result\n", + "first_ptr, last_ptr = line.split('/')\n", + "print ('First: %s Last: %s\\n' % (first_ptr, last_ptr))" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "First: Steve Last: Oualline\n\n" + "text": [ + "First: Steve Last: Oualline\n", + "\n" + ] } ], "prompt_number": 8 @@ -164,19 +314,37 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.11, Page number: 240" + "source": [ + "Example 13.11, Page number: 240" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.11.py\n# To return a temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nprint ('Name: %s\\n' % tmp_name())", + "input": [ + "\n", + "# Function declaration\n", + "def tmp_name() :\n", + " if not hasattr (tmp_name, 'sequence') :\n", + " tmp_name.sequence = 0\n", + " tmp_name.sequence += 1\n", + " name = 'tmp'\n", + " name += str (tmp_name.sequence)\n", + " return name\n", + "\n", + "# Calculation and result\n", + "print ('Name: %s\\n' % tmp_name())" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Name: tmp1\n\n" + "text": [ + "Name: tmp1\n", + "\n" + ] } ], "prompt_number": 9 @@ -185,12 +353,87 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.12, Page number: 245" + "source": [ + "Example 13.12, Page number: 245" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.12.py\n# To format files for printing\n\n\n# Variable and function declaration\nimport sys\nv_count = sys.argv\ncounter = len(sys.argv)\n\n# Produces verbose messages\nverbose = 0\n\n# Sends output to a file\nout_file = open ('print.out', 'w')\n\n# Sets the number of lines per page\nline_max = 66\n\ndef do_file (name) :\n print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n\ndef usage() :\n print ('Usage is %s [options] [file-list]\\n' % program_name)\n print ('Options\\n')\n print (' -v verbose\\n')\n print (' -l<number> Number of lines\\n')\n print (' -o<name> Set output filename\\n')\n sys.exit(1)\n \n# Calculation and result\nprogram_name = str (sys.argv[0])\n\nwhile ((counter > 1) and (sys.argv[1][0] == '-')) :\n if (sys.argv[1][1] == 'v') :\n verbose = 1\n break\n\n elif (sys.argv[1][1] == 'o') :\n temp = str (sys.argv[1])\n out_file.write (temp)\n break\n\n elif (sys.argv[1][1] == 'l') :\n line_max = int (sys.argv[1][2])\n break\n\n else :\n print ('Bad option %s\\n' % sys.argv[1])\n usage()\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n\n counter -= 1\n\nif (counter == 1) :\n do_file ('print.in')\n\nelse :\n while (counter > 1) :\n do_file (sys.argv[1])\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n \n counter -= 1\n\nout_file.close()", + "input": [ + "\n", + "\n", + "# Variable and function declaration\n", + "import sys\n", + "v_count = sys.argv\n", + "counter = len(sys.argv)\n", + "\n", + "# Produces verbose messages\n", + "verbose = 0\n", + "\n", + "# Sends output to a file\n", + "out_file = open ('print.out', 'w')\n", + "\n", + "# Sets the number of lines per page\n", + "line_max = 66\n", + "\n", + "def do_file (name) :\n", + " print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n", + "\n", + "def usage() :\n", + " print ('Usage is %s [options] [file-list]\\n' % program_name)\n", + " print ('Options\\n')\n", + " print (' -v verbose\\n')\n", + " print (' -l<number> Number of lines\\n')\n", + " print (' -o<name> Set output filename\\n')\n", + " sys.exit(1)\n", + " \n", + "# Calculation and result\n", + "program_name = str (sys.argv[0])\n", + "\n", + "while ((counter > 1) and (sys.argv[1][0] == '-')) :\n", + " if (sys.argv[1][1] == 'v') :\n", + " verbose = 1\n", + " break\n", + "\n", + " elif (sys.argv[1][1] == 'o') :\n", + " temp = str (sys.argv[1])\n", + " out_file.write (temp)\n", + " break\n", + "\n", + " elif (sys.argv[1][1] == 'l') :\n", + " line_max = int (sys.argv[1][2])\n", + " break\n", + "\n", + " else :\n", + " print ('Bad option %s\\n' % sys.argv[1])\n", + " usage()\n", + "\n", + " for index in range (0, counter) :\n", + " if (index == counter - 1) :\n", + " break\n", + " else :\n", + " v_count[index] = v_count[index + 1]\n", + "\n", + " counter -= 1\n", + "\n", + "if (counter == 1) :\n", + " do_file ('print.in')\n", + "\n", + "else :\n", + " while (counter > 1) :\n", + " do_file (sys.argv[1])\n", + "\n", + " for index in range (0, counter) :\n", + " if (index == counter - 1) :\n", + " break\n", + " else :\n", + " v_count[index] = v_count[index + 1]\n", + " \n", + " counter -= 1\n", + "\n", + "out_file.close()" + ], "language": "python", "metadata": {}, "outputs": [ @@ -206,12 +449,27 @@ { "output_type": "stream", "stream": "stdout", - "text": "Bad option -f\n\nUsage is -c [options] [file-list]\n\nOptions\n\n -v verbose\n\n -l<number> Number of lines\n\n -o<name> Set output filename\n\n" + "text": [ + "Bad option -f\n", + "\n", + "Usage is -c [options] [file-list]\n", + "\n", + "Options\n", + "\n", + " -v verbose\n", + "\n", + " -l<number> Number of lines\n", + "\n", + " -o<name> Set output filename\n", + "\n" + ] }, { "output_type": "stream", "stream": "stderr", - "text": "To exit: use 'exit', 'quit', or Ctrl-D." + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D." + ] } ], "prompt_number": 10 @@ -220,19 +478,43 @@ "cell_type": "heading", "level": 3, "metadata": {}, - "source": "Example 13.13, Page number: 248" + "source": [ + "Example 13.13, Page number: 248" + ] }, { "cell_type": "code", "collapsed": false, - "input": "# Example 13.13.py\n# To return a new temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nname1 = tmp_name()\nname2 = tmp_name()\nprint ('Name1: %s\\n' % name1)\nprint ('Name2: %s\\n' % name2)", + "input": [ + "\n", + "\n", + "# Function declaration\n", + "def tmp_name() :\n", + " if not hasattr (tmp_name, 'sequence') :\n", + " tmp_name.sequence = 0\n", + " tmp_name.sequence += 1\n", + " name = 'tmp'\n", + " name += str (tmp_name.sequence)\n", + " return name\n", + "\n", + "# Calculation and result\n", + "name1 = tmp_name()\n", + "name2 = tmp_name()\n", + "print ('Name1: %s\\n' % name1)\n", + "print ('Name2: %s\\n' % name2)" + ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "Name1: tmp1\n\nName2: tmp2\n\n" + "text": [ + "Name1: tmp1\n", + "\n", + "Name2: tmp2\n", + "\n" + ] } ], "prompt_number": 11 |