diff options
Diffstat (limited to 'Beginning_C++_through_game_programming/ch7.ipynb')
-rwxr-xr-x | Beginning_C++_through_game_programming/ch7.ipynb | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Beginning_C++_through_game_programming/ch7.ipynb b/Beginning_C++_through_game_programming/ch7.ipynb new file mode 100755 index 00000000..ce1452b8 --- /dev/null +++ b/Beginning_C++_through_game_programming/ch7.ipynb @@ -0,0 +1,112 @@ +{ + "metadata": { + "name": "ch7" + }, + "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": "'''\nPointing\nDemonstrates using pointers\nNOte : pointer concept is not in python, so output would be differ.\n'''\n\npScore = 0 #declare and initialize a pointer\nscore = 1000 #assign pointer pScore address of variable score\npScore = score; \nprint \"Assigning &score to pScore\\n\";\nprint \"&score is: \" , id(score) # address of score variable\nprint \"pScore is: \" , pScore # address stored in pointer\nprint \"score is: \" , score\nprint \"pScore is: \" , pScore # value pointed to by pointer\nprint \"Adding 500 to score\";\nscore += 500;\nprint \"score is: \" , score \nprint \"*pScore is: \" , pScore \nprint \"Adding 500 to *pScore\\n\";\npScore += 500;\nprint \"score is: \" , score \nprint \"*pScore is: \" , pScore\nprint \"Assigning &newScore to pScore\\n\";\nnewScore = 5000;\npScore = newScore;\nprint \"&newScore is: \" , id(newScore)\nprint \"pScore is: \" , pScore\nprint \"newScore is: \" , newScore\nprint \"*pScore is: \" , pScore\n\nprint \"Assigning &str to pStr\\n\";\nstr = \"score\";\npStr = str; \nprint \"str is: \" , str\nprint \"*pStr is: \" , pStr\nprint \"(*pStr).size() is: \" , len(pStr)\nprint \"pStr->size() is: \" , len(pStr)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Assigning &score to pScore\n\n&score is: 24878128\npScore is: 1000\nscore is: 1000\npScore is: 1000\nAdding 500 to score\nscore is: 1500\n*pScore is: 1000\nAdding 500 to *pScore\n\nscore is: 1500\n*pScore is: 1500\nAssigning &newScore to pScore\n\n&newScore is: 24880648\npScore is: 5000\nnewScore is: 5000\n*pScore is: 5000\nAssigning &str to pStr\n\nstr is: score\n*pStr is: score\n(*pStr).size() is: 5\npStr->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// Swap Pointer\n// Demonstrates passing constant pointers to alter argument variables\n'''\n\ndef badSwap(x,y):\n x,y = y,x\n \ndef goodSwap(x,y):\n x[0],y[0] = y[0],x[0]\n\n\nmyScore = [150]\nyourScore = [1000]\nprint \"Original values\";\nprint \"myScore: \" , myScore[0]\nprint \"yourScore: \" , yourScore[0]\nprint \"Calling badSwap()\";\nbadSwap(myScore[0], yourScore[0]);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]\nprint \"Calling goodSwap()\\n\";\ngoodSwap(myScore,yourScore);\nprint \"myScore: \" , myScore[0] \nprint \"yourScore: \" , yourScore[0]", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Original values\nmyScore: 150\nyourScore: 1000\nCalling badSwap()\nmyScore: 150\nyourScore: 1000\nCalling goodSwap()\n\nmyScore: 1000\nyourScore: 150\n" + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": "example : 7.3 page no : 239" + }, + { + "cell_type": "code", + "collapsed": false, + "input": "'''\nInventory Pointer\nDemonstrates returning a pointer\n'''\ndef ptrToElement(pVec, i):\n return pVec[i]\n\ninventory = []\ninventory.append(\"sword\");\ninventory.append(\"armor\");\ninventory.append(\"shield\");\n#displays string object that the returned pointer points to\nprint \"Sending the objected pointed to by returned pointer:\";\nprint ptrToElement(inventory, 0)\n# assigns one pointer to another - - inexpensive assignment\nprint \"Assigning the returned pointer to another pointer.\";\npStr = ptrToElement(inventory, 1);\nprint \"Sending the object pointed to by new pointer to cout:\";\nprint pStr \n#copies a string object - - expensive assignment\nprint \"Assigning object pointed by pointer to a string object.\\n\";\ns = (ptrToElement(inventory, 2));\nprint \"Sending the new string object to cout:\";\nprint s \n#altering the string object through a returned pointer\nprint \"Altering an object through a returned pointer.\";\npStr = \"Healing Potion\";\ninventory[1] = pStr\nprint \"Sending the altered object to cout:\\n\";\nprint inventory[1] ", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Sending the objected pointed to by returned pointer:\nsword\nAssigning the returned pointer to another pointer.\nSending the object pointed to by new pointer to cout:\narmor\nAssigning object pointed by pointer to a string object.\n\nSending the new string object to cout:\nshield\nAltering an object through a returned pointer.\nSending the altered object to cout:\n\nHealing 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//Array Passer\n//Demonstrates relationship between pointers and arrays\n'''\ndef increase(array,NUM_ELEMENTS):\n for i in range(NUM_ELEMENTS):\n array[i] += 500;\n\ndef display( array, NUM_ELEMENTS):\n for i in range(NUM_ELEMENTS):\n print array[i]\n\n\nprint \"Creating an array of high scores.\\n\\n\";\nNUM_SCORES = 3;\nhighScores = [5000, 3500, 2700]\nprint \"Displaying scores using array name as a constant pointer.\";\nprint highScores[0]\nprint highScores[1]\nprint highScores[2]\nprint \"Increasing scores by passing array as a constant pointer.\";\nincrease(highScores, NUM_SCORES)\nprint \"Displaying scores by passing array as a constant pointer to a constant.\";\ndisplay(highScores, NUM_SCORES);", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "Creating an array of high scores.\n\n\nDisplaying scores using array name as a constant pointer.\n5000\n3500\n2700\nIncreasing scores by passing array as a constant pointer.\nDisplaying scores by passing array as a constant pointer to a constant.\n5500\n4000\n3200\n" + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |