summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb')
-rwxr-xr-xC_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb288
1 files changed, 288 insertions, 0 deletions
diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb
new file mode 100755
index 00000000..32171653
--- /dev/null
+++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb
@@ -0,0 +1,288 @@
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:7c77241a39b4d43ac95986ef692466eff990f0d448827fcae58b424fe3cd8b1d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 8: Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example reverse.c, Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "N=10\n",
+ "li = raw_input(\"Enter %d numbers: \" % N) #accepting input in list\n",
+ "a = map(int, li.split()) \n",
+ "print \"In reverse order: \",\n",
+ "for i in a[::-1]: #print reversed list\n",
+ " print i,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In reverse order: 31 50 11 23 94 7 102 49 82 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example repdigit.c, Page 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "digit_seen=[False]*10 #initialise with false\n",
+ "n=int(raw_input(\"Enter a number: \"))#input number\n",
+ "while(n>0): #calculate if any digit repeated\n",
+ " digit=n%10\n",
+ " if (digit_seen[digit]==True):\n",
+ " break\n",
+ " digit_seen[digit]=True\n",
+ " n=n/10\n",
+ "if(n>0): \n",
+ " print \"Repeated digit\" #print result\n",
+ "else:\n",
+ " print \"No repeated digit\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 28212\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Repeated digit\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example interest.c, Page 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "#initialisation\n",
+ "value=[None]*5 \n",
+ "NUM_RATES=int(sys.getsizeof(value)/sys.getsizeof(value[0]))-1\n",
+ "INITIAL_BALANCE=100.00\n",
+ "#accept input\n",
+ "low_rate=int(raw_input(\"Enter interest rate: \"))\n",
+ "num_years=int(raw_input(\"Enter number of years: \"))\n",
+ "print \"\"\n",
+ "#print calculated result table\n",
+ "print \"Years\",\n",
+ "for i in range (NUM_RATES):\n",
+ " print \"%6d%%\" % (low_rate + i),\n",
+ " value[i]=INITIAL_BALANCE\n",
+ "print \"\"\n",
+ "for year in range (num_years):\n",
+ " print \"%3d\\t\"%(year+1),\n",
+ " for i in range (NUM_RATES):\n",
+ " value[i]=value[i]+ (low_rate+i)/100.0*value[i]\n",
+ " print \"%7.2f\"%value[i],\n",
+ " print \"\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter interest rate: 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of years: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Years 6% 7% 8% 9% 10% \n",
+ " 1\t 106.00 107.00 108.00 109.00 110.00 \n",
+ " 2\t 112.36 114.49 116.64 118.81 121.00 \n",
+ " 3\t 119.10 122.50 125.97 129.50 133.10 \n",
+ " 4\t 126.25 131.08 136.05 141.16 146.41 \n",
+ " 5\t 133.82 140.26 146.93 153.86 161.05 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example deal.c, Page 173"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import random\n",
+ "#initialisation\n",
+ "NUM_SUITS=4\n",
+ "NUM_RANKS=13\n",
+ "in_hand = [[False for i in xrange(13)] for i in xrange(4)]\n",
+ "rank_code=['2','3','4','5','6','7','8','9','t','j','q','k','a']\n",
+ "suit_code=['c','d','h','s']\n",
+ "#accet input\n",
+ "num_cards=int(raw_input(\"Enter number of cards in hand: \"))\n",
+ "print \"Your hand: \",\n",
+ "while(num_cards>0):\n",
+ " suit=random.randint(0,(NUM_SUITS-1)) #generate random suit\n",
+ " rank=random.randint(0,(NUM_RANKS-1)) #generate random rank\n",
+ " if (in_hand[suit][rank]!=True):\n",
+ " in_hand[suit][rank]=True\n",
+ " num_cards=num_cards-1\n",
+ " #print hand\n",
+ " print \"%c%c\" % (rank_code[rank],suit_code[suit]),\n",
+ "print \"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of cards in hand: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your hand: 9h 4d 5d 3d ad \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example reverse2.c, Page 174"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "n=int(raw_input(\"How many numbers do you want to reverse?\")) #accept number of elements\n",
+ "li = raw_input(\"Enter %d numbers: \" % n) #accepting input in list\n",
+ "a = map(int, li.split())\n",
+ "print \"In reverse order: \", #print reversed list\n",
+ "for i in a[::-1]:\n",
+ " print i,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many numbers do you want to reverse?10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In reverse order: 31 50 11 23 94 7 102 49 82 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file