{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 7 : Pointers: Tic-Tac-Toe 2.0" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 7.1 page no :225" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "pScore = 0 #declare and initialize a pointer\n", "score = 1000 #assign pointer pScore address of variable score\n", "pScore = score; \n", "print \"Assigning &score to pScore\\n\";\n", "print \"&score is: \" , id(score) # address of score variable\n", "print \"pScore is: \" , pScore # address stored in pointer\n", "print \"score is: \" , score\n", "print \"pScore is: \" , pScore # value pointed to by pointer\n", "print \"Adding 500 to score\";\n", "score += 500;\n", "print \"score is: \" , score \n", "print \"*pScore is: \" , pScore \n", "print \"Adding 500 to *pScore\\n\";\n", "pScore += 500;\n", "print \"score is: \" , score \n", "print \"*pScore is: \" , pScore\n", "print \"Assigning &newScore to pScore\\n\";\n", "newScore = 5000;\n", "pScore = newScore;\n", "print \"&newScore is: \" , id(newScore)\n", "print \"pScore is: \" , pScore\n", "print \"newScore is: \" , newScore\n", "print \"*pScore is: \" , pScore\n", "\n", "print \"Assigning &str to pStr\\n\";\n", "str = \"score\";\n", "pStr = str; \n", "print \"str is: \" , str\n", "print \"*pStr is: \" , pStr\n", "print \"(*pStr).size() is: \" , len(pStr)\n", "print \"pStr->size() is: \" , len(pStr)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Assigning &score to pScore\n", "\n", "&score is: 24878128\n", "pScore is: 1000\n", "score is: 1000\n", "pScore is: 1000\n", "Adding 500 to score\n", "score is: 1500\n", "*pScore is: 1000\n", "Adding 500 to *pScore\n", "\n", "score is: 1500\n", "*pScore is: 1500\n", "Assigning &newScore to pScore\n", "\n", "&newScore is: 24880648\n", "pScore is: 5000\n", "newScore is: 5000\n", "*pScore is: 5000\n", "Assigning &str to pStr\n", "\n", "str is: score\n", "*pStr is: score\n", "(*pStr).size() is: 5\n", "pStr->size() is: 5\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 7.2 page no : 235" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def badSwap(x,y):\n", " x,y = y,x\n", " \n", "def goodSwap(x,y):\n", " x[0],y[0] = y[0],x[0]\n", "\n", "\n", "myScore = [150]\n", "yourScore = [1000]\n", "print \"Original values\";\n", "print \"myScore: \" , myScore[0]\n", "print \"yourScore: \" , yourScore[0]\n", "print \"Calling badSwap()\";\n", "badSwap(myScore[0], yourScore[0]);\n", "print \"myScore: \" , myScore[0] \n", "print \"yourScore: \" , yourScore[0]\n", "print \"Calling goodSwap()\\n\";\n", "goodSwap(myScore,yourScore);\n", "print \"myScore: \" , myScore[0] \n", "print \"yourScore: \" , yourScore[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Original values\n", "myScore: 150\n", "yourScore: 1000\n", "Calling badSwap()\n", "myScore: 150\n", "yourScore: 1000\n", "Calling goodSwap()\n", "\n", "myScore: 1000\n", "yourScore: 150\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example : 7.3 page no : 239" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def ptrToElement(pVec, i):\n", " return pVec[i]\n", "\n", "inventory = []\n", "inventory.append(\"sword\");\n", "inventory.append(\"armor\");\n", "inventory.append(\"shield\");\n", "#displays string object that the returned pointer points to\n", "print \"Sending the objected pointed to by returned pointer:\";\n", "print ptrToElement(inventory, 0)\n", "# assigns one pointer to another - - inexpensive assignment\n", "print \"Assigning the returned pointer to another pointer.\";\n", "pStr = ptrToElement(inventory, 1);\n", "print \"Sending the object pointed to by new pointer to cout:\";\n", "print pStr \n", "#copies a string object - - expensive assignment\n", "print \"Assigning object pointed by pointer to a string object.\\n\";\n", "s = (ptrToElement(inventory, 2));\n", "print \"Sending the new string object to cout:\";\n", "print s \n", "#altering the string object through a returned pointer\n", "print \"Altering an object through a returned pointer.\";\n", "pStr = \"Healing Potion\";\n", "inventory[1] = pStr\n", "print \"Sending the altered object to cout:\\n\";\n", "print inventory[1] " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Sending the objected pointed to by returned pointer:\n", "sword\n", "Assigning the returned pointer to another pointer.\n", "Sending the object pointed to by new pointer to cout:\n", "armor\n", "Assigning object pointed by pointer to a string object.\n", "\n", "Sending the new string object to cout:\n", "shield\n", "Altering an object through a returned pointer.\n", "Sending the altered object to cout:\n", "\n", "Healing Potion\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "example 7.4 page no : 244" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def increase(array,NUM_ELEMENTS):\n", " for i in range(NUM_ELEMENTS):\n", " array[i] += 500;\n", "\n", "def display( array, NUM_ELEMENTS):\n", " for i in range(NUM_ELEMENTS):\n", " print array[i]\n", "\n", "\n", "print \"Creating an array of high scores.\\n\\n\";\n", "NUM_SCORES = 3;\n", "highScores = [5000, 3500, 2700]\n", "print \"Displaying scores using array name as a constant pointer.\";\n", "print highScores[0]\n", "print highScores[1]\n", "print highScores[2]\n", "print \"Increasing scores by passing array as a constant pointer.\";\n", "increase(highScores, NUM_SCORES)\n", "print \"Displaying scores by passing array as a constant pointer to a constant.\";\n", "display(highScores, NUM_SCORES);" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Creating an array of high scores.\n", "\n", "\n", "Displaying scores using array name as a constant pointer.\n", "5000\n", "3500\n", "2700\n", "Increasing scores by passing array as a constant pointer.\n", "Displaying scores by passing array as a constant pointer to a constant.\n", "5500\n", "4000\n", "3200\n" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }