{ "metadata": { "name": "", "signature": "sha256:d4eeb6d301f04950d37e1192961f4665d398ce20c6e4b34ea5228762dfddaffa" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Hour 17: Allocating Memory" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17.1, Page No 281" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n", "def StrCopy(str1,str2):\n", " str1=list(str1)\n", " str2=list(str2)\n", " for i in range(len(str1)):\n", " str2[i]=str1[i]\n", " str2[i]='\\0'\n", "\n", "str1=\"Use malloc() to allocate memory\"\n", "ptr_str=str\n", "ptr_str= str1\n", "\n", "if(ptr_str!= None):\n", " StrCopy(str1,ptr_str)\n", " print \"The string pointed to by ptr_str is : %s\" % ptr_str\n", "else:\n", " print \"malloc() function failed.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The string pointed to by ptr_str is : Use malloc() to allocate memory\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17.2, Page No 283" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n", "def DataMultiply(max1,ptr_int):\n", " for i in range(max1):\n", " for j in range(max1):\n", " ptr_int=(i+1)*(j+1)\n", " return ptr_int\n", "def TablePrint(max1,ptr_int):\n", " print \"The Multiplication table of %d is:\" % max1\n", " print \"\"\n", " for i in range(max1):\n", " print \"%4d\" % (i+1),\n", " print \"\"\n", " for i in range(max1):\n", " print \"----\",\n", " print\"\"\n", " for i in range(max1):\n", " print \"%d|\" % (i+1),\n", " for j in range(max1):\n", " print \"%3d\" % ptr_int,\n", " print\"\"\n", "\n", "ptr_int=int\n", "ptr_int=0\n", "key='c'\n", "max1=0\n", "termination=0\n", "while(key != 'x'):\n", " max1=int(raw_input(\"Enter a single digit number: \"))\n", " if(ptr_int != None):\n", " print ptr_int\n", " ptr_int=DataMultiply(max1,ptr_int)\n", " print ptr_int\n", " TablePrint(max1,ptr_int)\n", " else:\n", " print \"Malloc() function failed.\"\n", " termination=1\n", " key='x'\n", " key=raw_input(\"\\n\\nPress x key to quit; other key to continue.\")\n", "print\"\\nBYE\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a single digit number: 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0\n", "3\n", "The Multiplication table of 3 is:\n", "\n", " 1 2 3 \n", "---- ---- ---- \n", "1| 3 3 3 \n", "2| 3 3 3 \n", "3| 3 3 3 \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "Press x key to quit; other key to continue.x\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "BYE\n", "\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17.3, Page No 287" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n", "ptr1=float\n", "ptr2=float\n", "ptr1=0.0\n", "ptr2=0.0\n", "termination=1\n", "n=5\n", "\n", "if(ptr1 == None):\n", " print \"Malloc() failed.\"\n", "elif(ptr2 == None):\n", " print \"Calloc() failed.\"\n", "else:\n", " for i in range(n):\n", " print \"ptr1[%d]= %5.2f, ptr2[%d]= %5.2f\" %(i,(ptr1+i),i,(ptr2+i))\n", " termination=0" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ptr1[0]= 0.00, ptr2[0]= 0.00\n", "ptr1[1]= 1.00, ptr2[1]= 1.00\n", "ptr1[2]= 2.00, ptr2[2]= 2.00\n", "ptr1[3]= 3.00, ptr2[3]= 3.00\n", "ptr1[4]= 4.00, ptr2[4]= 4.00\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 17.4, Page No 289" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# There is no concept of pointer and dynamic memory allocation in pyton so i have done in this manner\n", "def StrCopy(str1,str2):\n", " str1=list(str1)\n", " str2=list(str1)\n", " for i in range(len(str1)-1):\n", " str2[i]=str1[i]\n", " str2[i]='\\0'\n", " return str2\n", "\n", "str1=[\"There's music in the sighing of a reed;\",\"There's music in the gushing of a rill;\",\"There's music in all things if men had ears;\",\"There earth is but an echo of the spheres.\\n\"]\n", "termination=0\n", "if(ptr == None):\n", " print \"Malloc() failed.\"\n", " termination=1\n", "else:\n", " ptr1=StrCopy(str1[0],ptr)\n", " print \"\".join(ptr1)\n", " for i in range(1,4):\n", " if (ptr == None):\n", " print \"realloc() failed.\"\n", " termination=1\n", " i=4\n", " else:\n", " ptr1=StrCopy(str1[i],ptr)\n", " print \"\".join(ptr1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "There's music in the sighing of a ree\u0000;\n", "There's music in the gushing of a ril\u0000;\n", "There's music in all things if men had ear\u0000;\n", "There earth is but an echo of the spheres\u0000\n", "\n" ] } ], "prompt_number": 12 } ], "metadata": {} } ] }