diff options
Diffstat (limited to 'Teach_Yourself_C_in_24_Hours/hour12.ipynb')
-rwxr-xr-x | Teach_Yourself_C_in_24_Hours/hour12.ipynb | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/Teach_Yourself_C_in_24_Hours/hour12.ipynb b/Teach_Yourself_C_in_24_Hours/hour12.ipynb new file mode 100755 index 00000000..276fbcc6 --- /dev/null +++ b/Teach_Yourself_C_in_24_Hours/hour12.ipynb @@ -0,0 +1,311 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:b5e96d8861a1f6c8945a488114c3e560525b59b81ecaf015f068a3722502da99"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Hour 12 : Understnading Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.1, Page No 191"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "list_int=range(10)\n",
+ "\n",
+ "for i in range(10):\n",
+ " list_int[i]=i+1\n",
+ " print \"list_int[\"+str(i)+\"] is initialized with \"+str(list_int[i])+\"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "list_int[0] is initialized with 1\n",
+ "\n",
+ "list_int[1] is initialized with 2\n",
+ "\n",
+ "list_int[2] is initialized with 3\n",
+ "\n",
+ "list_int[3] is initialized with 4\n",
+ "\n",
+ "list_int[4] is initialized with 5\n",
+ "\n",
+ "list_int[5] is initialized with 6\n",
+ "\n",
+ "list_int[6] is initialized with 7\n",
+ "\n",
+ "list_int[7] is initialized with 8\n",
+ "\n",
+ "list_int[8] is initialized with 9\n",
+ "\n",
+ "list_int[9] is initialized with 10\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.2, Page No 193"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "list_int=range(10)\n",
+ "\n",
+ "total_byte= sys.getsizeof(int)*10\n",
+ "\n",
+ "print \"The size of int is \"+str(sys.getsizeof(int))+\" byte long\"\n",
+ "print \"The array of 10 ints has total \"+str(total_byte)+\" bytes\"\n",
+ "print \"The address of the first element: \"+str(id(list_int[0]))\n",
+ "print \"The address of the last element: \"+str(id(list_int[9]))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of int is 436 byte long\n",
+ "The array of 10 ints has total 4360 bytes\n",
+ "The address of the first element: 21357036\n",
+ "The address of the last element: 21356928\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.3, Page No 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "list_int=range(10)\n",
+ "\n",
+ "for i in range(10):\n",
+ " list_int[i]=i+1\n",
+ "\n",
+ "ptr_int=list_int\n",
+ "print \"The start address of the array: \"+str(id(ptr_int[0]))\n",
+ "print \"The value of the first element: \"+str(ptr_int[0])\n",
+ "ptr_int=id(list_int[0])\n",
+ "print \"The address of the first element: \"+str(ptr_int)\n",
+ "print \"The value of the first element: \"+str(list_int[0])"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The start address of the array: 20177376\n",
+ "The value of the first element: 1\n",
+ "The address of the first element: 20177376\n",
+ "The value of the first element: 1\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.4, Page No 196"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "array_ch=['H','e','l','l','o','!','\\0']\n",
+ "\n",
+ "for i in range(7):\n",
+ " print \"array_ch[\"+str(i)+\"] contains: \"+str(array_ch[i])\n",
+ " \n",
+ "'''Method I'''\n",
+ "print \"Put all elements together(Method I):\"\n",
+ "for ch in array_ch:\n",
+ " print ch,\n",
+ "\n",
+ "''' Method II'''\n",
+ "print \"\\nPut all elements together(Method II):\"\n",
+ "print \"\".join(array_ch)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "array_ch[0] contains: H\n",
+ "array_ch[1] contains: e\n",
+ "array_ch[2] contains: l\n",
+ "array_ch[3] contains: l\n",
+ "array_ch[4] contains: o\n",
+ "array_ch[5] contains: !\n",
+ "array_ch[6] contains: \u0000\n",
+ "Put all elements together(Method I):\n",
+ "H e l l o ! \u0000 \n",
+ "Put all elements together(Method II):\n",
+ "Hello!\u0000\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.5, Page No 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\\0']\n",
+ "\n",
+ "i=0\n",
+ "while(array_ch[i]!='\\0'):\n",
+ " print \"\"+array_ch[i],\n",
+ " i=i+1\n",
+ "\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C i s p o w e r f u l ! \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.6, Page No 200"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "two_dim=[[1,2,3,4,5],[10,20,30,40,50],[100,200,300,400,500]]\n",
+ "\n",
+ "for i in range(3):\n",
+ " print \"\\n\"\n",
+ " for j in range(5):\n",
+ " print \"%6u\" % two_dim[i][j],\n",
+ "print \"\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "\n",
+ " 1 2 3 4 5 \n",
+ "\n",
+ " 10 20 30 40 50 \n",
+ "\n",
+ " 100 200 300 400 500 \n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 12.7, Page No 202"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "array_ch=['C',' ','i','s',' ','p','o','w','e','r','f','u','l','!','\\0']\n",
+ "\n",
+ "list_int=[[1,1,1],[2,2,8],[3,9,27],[4,16,64],[5,25,125],[6,36,216],[7,49,343]]\n",
+ "\n",
+ "print \"The size of array_ch[] is %d bytes.\\n\" %sys.getsizeof(array_ch)\n",
+ "print \"The size of list_int[][] is %d bytes.\\n\" %sys.getsizeof(list_int)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The size of array_ch[] is 96 bytes.\n",
+ "\n",
+ "The size of list_int[][] is 64 bytes.\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |