diff options
author | hardythe1 | 2015-04-07 15:58:05 +0530 |
---|---|---|
committer | hardythe1 | 2015-04-07 15:58:05 +0530 |
commit | 92cca121f959c6616e3da431c1e2d23c4fa5e886 (patch) | |
tree | 205e68d0ce598ac5caca7de839a2934d746cce86 /C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb | |
parent | b14c13fcc6bb6d01c468805d612acb353ec168ac (diff) | |
download | Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.gz Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.tar.bz2 Python-Textbook-Companions-92cca121f959c6616e3da431c1e2d23c4fa5e886.zip |
added books
Diffstat (limited to 'C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb')
-rwxr-xr-x | C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb | 582 |
1 files changed, 582 insertions, 0 deletions
diff --git a/C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb b/C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb new file mode 100755 index 00000000..c1085a33 --- /dev/null +++ b/C_Programming_for_the_Absolute_Beginner/Chapter7.ipynb @@ -0,0 +1,582 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:be75d77e91d7005f064cd2c560347aec768e900f2e81ef47804d4b096bc083a6"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 7 : Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.1, Page No 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=5\n",
+ "iPtr=id(x)\n",
+ "iPtr=7"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.2, Page No 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=1\n",
+ "iPtr=id(x)\n",
+ "\n",
+ "iPtr=5\n",
+ "\n",
+ "print \"\\n*iPtr = \"+str(iPtr)+\"\\n &x = \"+str(id(x))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "*iPtr = 5\n",
+ " &x = 20176608\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.3, Page No 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=5\n",
+ "y=10\n",
+ "iPtr=None\n",
+ "\n",
+ "print \"Pointer points to: \",iPtr\n",
+ "iPtr=id(y)\n",
+ "print \"Ptr now points to: \",iPtr\n",
+ "x=iPtr\n",
+ "print \"The value of x is now\",x\n",
+ "iPtr=15\n",
+ "print \"The value of y is now\",iPtr"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Pointer points to: None\n",
+ "Ptr now points to: 20373108\n",
+ "The value of x is now 20373108\n",
+ "The value of y is now 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.4, Page No 159"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def addTwoNumbers(x,y):\n",
+ " return x+y\n",
+ "\n",
+ "\n",
+ "x=0\n",
+ "y=0\n",
+ "\n",
+ "x=int(raw_input(\"Enter first number:\"))\n",
+ "y=int(raw_input(\"Enter second number:\"))\n",
+ "\n",
+ "print \"Result is: \",addTwoNumbers(x,y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number:10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number:15\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Result is: 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.5, Page No 161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def demoPassByValue(x):\n",
+ " x+=5\n",
+ " print \"The value of x is:\",x\n",
+ "\n",
+ "x=0\n",
+ "x=int(raw_input(\"Enter a number:\"))\n",
+ "demoPassByValue(x)\n",
+ "print \"The original value of x did not change:\",x"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number:5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of x is: 10\n",
+ "The original value of x did not change: 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.6, Page No 162"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def demoPassByReference(x):\n",
+ " x+=5\n",
+ " print \"The value of x is now\",x\n",
+ "\n",
+ "x=0\n",
+ "x=int(raw_input(\"Enter a number:\"))\n",
+ "demoPassByReference(x)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number:10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The value of x is now 15\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.7, Page No 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "iArray=[1,2,3,4,5]\n",
+ "iPtr=id(iArray)\n",
+ "print \"Address of pointer: \",iPtr\n",
+ "print \"First address of array: \",id(iArray[0])\n",
+ "print \"Pointer points to: \",iArray[0]\n",
+ "print \"First element of array contains\",iArray[0]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of pointer: 28621960\n",
+ "First address of array: 19652320\n",
+ "Pointer points to: 1\n",
+ "First element of array contains 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.8, Page No 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def nameLength(name):\n",
+ " x=0\n",
+ " while(name[x] != '\\0'):\n",
+ " x+=1;\n",
+ " return x\n",
+ "aName='\\0'\n",
+ "aName=raw_input(\"Enter your first name: \")\n",
+ "aName=aName+'\\0'\n",
+ "print \"Your first name contains\"\n",
+ "print str(nameLength(aName))+\" characters\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your first name: mehta\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your first name contains\n",
+ "5 characters\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.9, Page No 167"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def squareNumber(num):\n",
+ " for x in range(3):\n",
+ " num[x]=num[x]*num[x]\n",
+ "\n",
+ "iNumbers=[2,4,6]\n",
+ "print \"The current array value are:\"\n",
+ "\n",
+ "for x in range(3):\n",
+ " print iNumbers[x]\n",
+ "\n",
+ "print \"\\n\"\n",
+ "\n",
+ "squareNumber(iNumbers)\n",
+ "\n",
+ "print \"The modified array values are :\"\n",
+ "\n",
+ "for x in range(3):\n",
+ " print iNumbers[x]\n",
+ "\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The current array value are:\n",
+ "2\n",
+ "4\n",
+ "6\n",
+ "\n",
+ "\n",
+ "The modified array values are :\n",
+ "4\n",
+ "16\n",
+ "36\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.10, Page No 168"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def printArgument(num):\n",
+ " print \"Read only argument is: \",num\n",
+ " \n",
+ "iNumber=5\n",
+ "printArgument(iNumber)\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Read only argument is: 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.11, Page No 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def printArray(num):\n",
+ " for x in range(3):\n",
+ " print num[x]\n",
+ "\n",
+ "iNumbers=[2,4,6]\n",
+ "printArray(iNumbers)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2\n",
+ "4\n",
+ "6\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.12, Page No 170"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def modifyArray(num):\n",
+ " for x in range(3):\n",
+ " num[x]=num[x]*num[x]\n",
+ " \n",
+ "iNumbers=[2,4,6]\n",
+ "modifyArray(iNumbers)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 7.13, Page No 173"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import random\n",
+ "def encrypt(sMessage,random):\n",
+ " x=0\n",
+ " while(sMessage[x]):\n",
+ " sMessage+=str(random)\n",
+ " x+=1\n",
+ " x=0\n",
+ " print \"Encrypted message is: \"\n",
+ " \n",
+ " while(sMessage[x]):\n",
+ " print sMessage[x]\n",
+ " x+=1\n",
+ "\n",
+ "def decrypt(sMessage,random):\n",
+ " x=0\n",
+ " while(sMessage[x]):\n",
+ " sMessage[x]=sMessage[x]-str(random)\n",
+ " x+=1\n",
+ " x=0\n",
+ " while(sMessage[x]):\n",
+ " print sMessage[x]\n",
+ " x+=1\n",
+ " \n",
+ "myString=range(21)\n",
+ "iSelection=0\n",
+ "iRand= (random.randint(1,4)%4)+1\n",
+ "\n",
+ "while(iSelection != 4):\n",
+ " print \"\\n\\n1\\t Encrypt Clear Text\\n\"\n",
+ " print \"2\\tDecrypt Cipher Text\\n\"\n",
+ " print \"3\\tGenerate New Key\\n\"\n",
+ " print \"4\\tQuit\\n\"\n",
+ " iSelection=int(raw_input(\"\\nSelect a Cryptography Option: \"))\n",
+ " \n",
+ " if(iSelection ==1):\n",
+ " myString=raw_input(\"\\nEnter one word as clear text to encrypt: \")\n",
+ " encrypt(myString,iRand)\n",
+ " break\n",
+ " elif(iSelection==2):\n",
+ " myString=raw_input(\"\\nEnter cipher text to decrypt: \")\n",
+ " decrypt(myString,iRand)\n",
+ " break\n",
+ " elif(iSelection==3):\n",
+ " iRand= (random.randrange(1,4,1)%4)+1\n",
+ " print \"\\nNew Key Generated\\n\"\n",
+ " break\n",
+ " else:\n",
+ " exit(0)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ "1\t Encrypt Clear Text\n",
+ "\n",
+ "2\tDecrypt Cipher Text\n",
+ "\n",
+ "3\tGenerate New Key\n",
+ "\n",
+ "4\tQuit\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Select a Cryptography Option: 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Enter one word as clear text to encrypt: bhavin\n"
+ ]
+ }
+ ]
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |