summaryrefslogtreecommitdiff
path: root/The_C_Book/Chapter5.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'The_C_Book/Chapter5.ipynb')
-rwxr-xr-xThe_C_Book/Chapter5.ipynb610
1 files changed, 610 insertions, 0 deletions
diff --git a/The_C_Book/Chapter5.ipynb b/The_C_Book/Chapter5.ipynb
new file mode 100755
index 00000000..e9821e3f
--- /dev/null
+++ b/The_C_Book/Chapter5.ipynb
@@ -0,0 +1,610 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:9c17589f9344a35ddc3dd493523efe0d9a1cf9ad387dc72f15338d1f77f12b62"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5: Arrays and Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.1, page no. 137"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "python does not have pointer functionality.We can get it explicitly.\n",
+ "'''\n",
+ "\n",
+ "class ref:\n",
+ " def __init__(self, obj): self.obj = obj\n",
+ " def get(self): return str(self.obj)\n",
+ " def set(self, obj): self.obj = obj\n",
+ "\n",
+ "\n",
+ "x=ref([0])\n",
+ "p=x # assigning variable value to p\n",
+ "\n",
+ "p.set(0) # set x to zero */\n",
+ "print \"\\nx is \" + x.get()\n",
+ "print \"\\np is \" + p.get()\n",
+ "a = p.get()\n",
+ "p.set(int(a)+1) # increment what p points to */\n",
+ "print \"\\nx is \" +x.get()\n",
+ "a = p.get()\n",
+ "p.set(int(a)+1) # increment what p points to */\n",
+ "print \"\\nx is \" + x.get()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "x is 0\n",
+ "\n",
+ "p is 0\n",
+ "\n",
+ "x is 1\n",
+ "\n",
+ "x is 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.2, page no. 139"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def date(a,b): # /* declare the function */\n",
+ " day_ret =0\n",
+ " month_ret = 0\n",
+ " ''' At this point, calculate the day and month\n",
+ " * values in day_ret and month_ret respectively.\n",
+ " '''\n",
+ " a = day_ret;\n",
+ " b = month_ret;\n",
+ " return a,b\n",
+ "\n",
+ "\n",
+ "\n",
+ "\n",
+ "month = 5\n",
+ "day = 1\n",
+ "d,m = date(day,month);\n",
+ "print \"day is %d, month is %d\\n\" %(d, m)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "day is 0, month is 0\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.3, page no. 142"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "from array import array\n",
+ "\n",
+ "ARSZ=20\n",
+ "ar=[]\n",
+ "\n",
+ "for i in range(ARSZ + 1):\n",
+ " ar.append( i)\n",
+ " print \"ar[%d] now = %d\\n\" %( i, ar[i])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "ar[0] now = 0\n",
+ "\n",
+ "ar[1] now = 1\n",
+ "\n",
+ "ar[2] now = 2\n",
+ "\n",
+ "ar[3] now = 3\n",
+ "\n",
+ "ar[4] now = 4\n",
+ "\n",
+ "ar[5] now = 5\n",
+ "\n",
+ "ar[6] now = 6\n",
+ "\n",
+ "ar[7] now = 7\n",
+ "\n",
+ "ar[8] now = 8\n",
+ "\n",
+ "ar[9] now = 9\n",
+ "\n",
+ "ar[10] now = 10\n",
+ "\n",
+ "ar[11] now = 11\n",
+ "\n",
+ "ar[12] now = 12\n",
+ "\n",
+ "ar[13] now = 13\n",
+ "\n",
+ "ar[14] now = 14\n",
+ "\n",
+ "ar[15] now = 15\n",
+ "\n",
+ "ar[16] now = 16\n",
+ "\n",
+ "ar[17] now = 17\n",
+ "\n",
+ "ar[18] now = 18\n",
+ "\n",
+ "ar[19] now = 19\n",
+ "\n",
+ "ar[20] now = 20\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.4 & 5.5, page no. 146"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "ARSZ = 10\n",
+ "\n",
+ "fp1= ARSZ\n",
+ "fp2=0\n",
+ "\n",
+ "\n",
+ "while(fp2 != fp1):\n",
+ " print \"Difference: %d\\n\" %((ARSZ-fp1))\n",
+ " fp1 -= 1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Difference: 0\n",
+ "\n",
+ "Difference: 1\n",
+ "\n",
+ "Difference: 2\n",
+ "\n",
+ "Difference: 3\n",
+ "\n",
+ "Difference: 4\n",
+ "\n",
+ "Difference: 5\n",
+ "\n",
+ "Difference: 6\n",
+ "\n",
+ "Difference: 7\n",
+ "\n",
+ "Difference: 8\n",
+ "\n",
+ "Difference: 9\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.6, page no. 149"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "s = raw_input(\"Enter String:\")\n",
+ "print len(s)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter String:python \n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.7, page no. 150"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def str_eq(s1,s2):\n",
+ " if s1==s2:\n",
+ " return True\n",
+ " return False\n",
+ "\n",
+ "print str_eq(\"this\",\"hello\")\n",
+ "print str_eq(\"this\",\"this\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "False\n",
+ "True\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.8, page no. 151"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "cp = \"a string\";\n",
+ "for i in cp:\n",
+ " print i,\n",
+ "\n",
+ "print \"\\n\"\n",
+ "for i in range(len(cp)):\n",
+ " print cp[i],"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "a s t r i n g \n",
+ "\n",
+ "a s t r i n g\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.9, page no. 152"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import numpy\n",
+ "\n",
+ "ARLEN=10\n",
+ "ar = numpy.zeros(ARLEN)\n",
+ "\n",
+ "print ar"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.10, page no. 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "Python does not have any concept of pointer.\n",
+ "Python will go same for all mentioned.\n",
+ "'''\n",
+ "\n",
+ "\n",
+ "MAXSTRING=50 # /* max no. of strings */\n",
+ "MAXLEN = 80 #/* max length. of strings */\n",
+ "\n",
+ "strings = [] # list of strings\n",
+ "\n",
+ "def print_arr(a):\n",
+ " print a\n",
+ "\n",
+ "\n",
+ "def sort_arr(a):\n",
+ " a.sort()\n",
+ " return a\n",
+ "\n",
+ "\n",
+ "def next_string():\n",
+ " s = raw_input(\"Enter Next String : \")\n",
+ " strings.append(s)\n",
+ " return s\n",
+ "\n",
+ "nstrings = 0\n",
+ "\n",
+ "while(nstrings < MAXSTRING and next_string() != '0'):\n",
+ " nstrings += 1\n",
+ "\n",
+ "print_arr(strings)\n",
+ "print sort_arr(strings)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Next String : abc\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Next String : def\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Next String : fed\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Next String : 0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "['abc', 'def', 'fed', '0']\n",
+ "['0', 'abc', 'def', 'fed']\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.13, page no. 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "'''\n",
+ "We need not to worry about memory allocation and deallocation in python.\n",
+ "It is simple to get a string, print and copy it.\n",
+ "'''\n",
+ "\n",
+ "\n",
+ "str_p = \"\"\n",
+ "tmp_p = \"\"\n",
+ "\n",
+ "str_p = raw_input(\"Enter String.. : \")\n",
+ "next_p = str_p\n",
+ "chars_read = len(str_p)\n",
+ "\n",
+ "\n",
+ "tmp_p = str_p\n",
+ "\n",
+ "print tmp_p\n",
+ "\n",
+ "\n",
+ "print str_p"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter String.. : hello\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "hello\n",
+ "hello\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.14, page no. 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "arr = \"hello\"\n",
+ "a = arr\n",
+ "print \"Size of arr %d\\n\" % len(arr)\n",
+ "print \"Size of a %d\\n\" % len(a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Size of arr 5\n",
+ "\n",
+ "Size of a 5\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 5.16, page no. 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "def func(arg):\n",
+ "\tprint \"%d\\n\" %(arg)\n",
+ "\n",
+ "func(5)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file