summaryrefslogtreecommitdiff
path: root/Practical_C_Programming
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming')
-rw-r--r--Practical_C_Programming/Chapter_10_1.ipynb259
-rw-r--r--Practical_C_Programming/Chapter_11_1.ipynb77
-rw-r--r--Practical_C_Programming/Chapter_12_1.ipynb29
-rw-r--r--Practical_C_Programming/Chapter_13_1.ipynb348
-rw-r--r--Practical_C_Programming/Chapter_14_1.ipynb98
-rw-r--r--Practical_C_Programming/Chapter_15_1.ipynb232
-rw-r--r--Practical_C_Programming/Chapter_16_1.ipynb43
-rw-r--r--Practical_C_Programming/Chapter_17_1.ipynb128
-rw-r--r--Practical_C_Programming/Chapter_18_1.ipynb62
-rw-r--r--Practical_C_Programming/Chapter_19_1.ipynb106
-rw-r--r--Practical_C_Programming/Chapter_21_1.ipynb34
-rw-r--r--Practical_C_Programming/Chapter_23_1.ipynb28
-rw-r--r--Practical_C_Programming/Chapter_2_3.ipynb2292
-rw-r--r--Practical_C_Programming/Chapter_3_4.ipynb22
-rw-r--r--Practical_C_Programming/Chapter_4_4.ipynb137
-rw-r--r--Practical_C_Programming/Chapter_5_4.ipynb238
-rw-r--r--Practical_C_Programming/Chapter_6_4.ipynb148
-rw-r--r--Practical_C_Programming/Chapter_7_4.ipynb129
-rw-r--r--Practical_C_Programming/Chapter_8_4.ipynb252
-rw-r--r--Practical_C_Programming/Chapter_9_4.ipynb95
20 files changed, 3323 insertions, 1434 deletions
diff --git a/Practical_C_Programming/Chapter_10_1.ipynb b/Practical_C_Programming/Chapter_10_1.ipynb
index b3cda55e..56e12012 100644
--- a/Practical_C_Programming/Chapter_10_1.ipynb
+++ b/Practical_C_Programming/Chapter_10_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 10"
+ "name": "",
+ "signature": "sha256:55a41a15f53fea81d9fb349d875475d12f3934656e4b83a8ff10ced1f1672515"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,45 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 10: C Preprocessor"
+ "source": [
+ "Chapter 10: C Preprocessor"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.1, Page number: 172"
+ "source": [
+ "Example 10.1, Page number: 172"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.1.py\n# To print the contents of two arrays upto 10 indices\n\n\n# Variable declaration\ndata = []\ntwice = []\n\n# Calculation and result\nfor index in range (0, 10) :\n data.append(index)\n twice.append(2 * index)\n\nprint (data)\nprint (twice)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "data = []\n",
+ "twice = []\n",
+ "\n",
+ "# Calculation and result\n",
+ "for index in range (0, 10) :\n",
+ " data.append(index)\n",
+ " twice.append(2 * index)\n",
+ "\n",
+ "print (data)\n",
+ "print (twice)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
+ "text": [
+ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
+ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +59,38 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.2, Page number: 173"
+ "source": [
+ "Example 10.2, Page number: 173"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.2.py\n# To print the contents of two arrays upto 20 indices \n\n\n# Variable declaration\ndata = []\ntwice = []\n\n# Calculation and result\nfor index in range (0, 20) :\n data.append(index)\n twice.append(2 * index)\n\nprint (data)\nprint (twice)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "data = []\n",
+ "twice = []\n",
+ "\n",
+ "# Calculation and result\n",
+ "for index in range (0, 20) :\n",
+ " data.append(index)\n",
+ " twice.append(2 * index)\n",
+ "\n",
+ "print (data)\n",
+ "print (twice)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]\n"
+ "text": [
+ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
+ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +99,36 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.3, Page number: 175"
+ "source": [
+ "Example 10.3, Page number: 175"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.3.py\n# To print the value of variable 'index' \n\n\n# Variable declaration\nBIG_NUMBER = 10 * 10\nindex = 1\n\n# Calculation and result\nwhile (index < BIG_NUMBER) :\n index = index * 8\n print ('%d' % index)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "BIG_NUMBER = 10 * 10\n",
+ "index = 1\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (index < BIG_NUMBER) :\n",
+ " index = index * 8\n",
+ " print ('%d' % index)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "8\n64\n512\n"
+ "text": [
+ "8\n",
+ "64\n",
+ "512\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +137,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.4, Page number: 176"
+ "source": [
+ "Example 10.4, Page number: 176"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.4.py\n# To determine the square of ALL_PARTS \n\n\n# Variable declaration\nFIRST_PART = 7\nLAST_PART = 5\nALL_PARTS = FIRST_PART + LAST_PART\n\n# Calculation and result\nprint ('The square of all the parts is %d' % (ALL_PARTS * ALL_PARTS))",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "FIRST_PART = 7\n",
+ "LAST_PART = 5\n",
+ "ALL_PARTS = FIRST_PART + LAST_PART\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The square of all the parts is %d' % (ALL_PARTS * ALL_PARTS))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The square of all the parts is 144\n"
+ "text": [
+ "The square of all the parts is 144\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +172,38 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.5, Page number: 177"
+ "source": [
+ "Example 10.5, Page number: 177"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.5.py\n# To print 'Hi there' 10 times \n\n\n# Calculation and result\nfor i in reversed (range(10)) :\n print ('Hi there')",
+ "input": [
+ "\n",
+ "\n",
+ "# Calculation and result\n",
+ "for i in reversed (range(10)) :\n",
+ " print ('Hi there')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\nHi there\n"
+ "text": [
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n",
+ "Hi there\n"
+ ]
}
],
"prompt_number": 5
@@ -122,19 +212,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.6, Page number: 177"
+ "source": [
+ "Example 10.6, Page number: 177"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.6.py\n# To print the value of 'size' \n\n\n# Variable declaration\nsize = 10\nfudge = size - 2\n\n# Calculation and result\nsize = fudge\nprint ('Size is %d' % size)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "size = 10\n",
+ "fudge = size - 2\n",
+ "\n",
+ "# Calculation and result\n",
+ "size = fudge\n",
+ "print ('Size is %d' % size)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Size is 8\n"
+ "text": [
+ "Size is 8\n"
+ ]
}
],
"prompt_number": 6
@@ -143,19 +246,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.7, Page number: 178"
+ "source": [
+ "Example 10.7, Page number: 178"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.7.py\n# To exit the program based on a specific condition \n\n\n# Variable declaration\nimport sys\nvalue = 1\n\n# Calculation and result\nif (value < 0) :\n sys.exit()\n\nprint ('We did not die')",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "value = 1\n",
+ "\n",
+ "# Calculation and result\n",
+ "if (value < 0) :\n",
+ " sys.exit()\n",
+ "\n",
+ "print ('We did not die')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "We did not die\n"
+ "text": [
+ "We did not die\n"
+ ]
}
],
"prompt_number": 7
@@ -164,19 +282,41 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.8, Page number: 184"
+ "source": [
+ "Example 10.8, Page number: 184"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.8.py\n# To display the square of numbers from 1 to 5 \n\n\n# Function declaration, calculation and result\ndef SQR (x) :\n return x * x\n \nfor counter in range (0, 5) :\n print ('x %d, x squared %d\\n' % (counter + 1, SQR (counter + 1)))",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def SQR (x) :\n",
+ " return x * x\n",
+ " \n",
+ "for counter in range (0, 5) :\n",
+ " print ('x %d, x squared %d\\n' % (counter + 1, SQR (counter + 1)))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x 1, x squared 1\n\nx 2, x squared 4\n\nx 3, x squared 9\n\nx 4, x squared 16\n\nx 5, x squared 25\n\n"
+ "text": [
+ "x 1, x squared 1\n",
+ "\n",
+ "x 2, x squared 4\n",
+ "\n",
+ "x 3, x squared 9\n",
+ "\n",
+ "x 4, x squared 16\n",
+ "\n",
+ "x 5, x squared 25\n",
+ "\n"
+ ]
}
],
"prompt_number": 8
@@ -185,19 +325,45 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.9, Page number: 184"
+ "source": [
+ "Example 10.9, Page number: 184"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.9.py\n# To display the square of numbers from 1 to 5 \n\n\n# Variable declaration\ncounter = 0\n\n# Function declaration, calculation and result\ndef SQR (x) :\n return x * x\n \nwhile (counter < 5) :\n print ('x %d square %d\\n' % (counter + 1, SQR (counter + 1)))\n counter += 1",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "counter = 0\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def SQR (x) :\n",
+ " return x * x\n",
+ " \n",
+ "while (counter < 5) :\n",
+ " print ('x %d square %d\\n' % (counter + 1, SQR (counter + 1)))\n",
+ " counter += 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "x 1 square 1\n\nx 2 square 4\n\nx 3 square 9\n\nx 4 square 16\n\nx 5 square 25\n\n"
+ "text": [
+ "x 1 square 1\n",
+ "\n",
+ "x 2 square 4\n",
+ "\n",
+ "x 3 square 9\n",
+ "\n",
+ "x 4 square 16\n",
+ "\n",
+ "x 5 square 25\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -206,19 +372,48 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 10.10, Page number: 185"
+ "source": [
+ "Example 10.10, Page number: 185"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 10.10.py\n# To calculate the reciprocal of numbers from 1 to 9\n\n\n# Function declaration, calculation and result\ndef reciprocal (number) :\n return 1 / number\n \nfor counter in range (1, 10) :\n print ('1/%f = %f\\n' % (counter, reciprocal (counter)))",
+ "input": [
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def reciprocal (number) :\n",
+ " return 1 / number\n",
+ " \n",
+ "for counter in range (1, 10) :\n",
+ " print ('1/%f = %f\\n' % (counter, reciprocal (counter)))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1/1.000000 = 1.000000\n\n1/2.000000 = 0.000000\n\n1/3.000000 = 0.000000\n\n1/4.000000 = 0.000000\n\n1/5.000000 = 0.000000\n\n1/6.000000 = 0.000000\n\n1/7.000000 = 0.000000\n\n1/8.000000 = 0.000000\n\n1/9.000000 = 0.000000\n\n"
+ "text": [
+ "1/1.000000 = 1.000000\n",
+ "\n",
+ "1/2.000000 = 0.000000\n",
+ "\n",
+ "1/3.000000 = 0.000000\n",
+ "\n",
+ "1/4.000000 = 0.000000\n",
+ "\n",
+ "1/5.000000 = 0.000000\n",
+ "\n",
+ "1/6.000000 = 0.000000\n",
+ "\n",
+ "1/7.000000 = 0.000000\n",
+ "\n",
+ "1/8.000000 = 0.000000\n",
+ "\n",
+ "1/9.000000 = 0.000000\n",
+ "\n"
+ ]
}
],
"prompt_number": 10
diff --git a/Practical_C_Programming/Chapter_11_1.ipynb b/Practical_C_Programming/Chapter_11_1.ipynb
index e3193adb..1dc24c59 100644
--- a/Practical_C_Programming/Chapter_11_1.ipynb
+++ b/Practical_C_Programming/Chapter_11_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 11"
+ "name": "",
+ "signature": "sha256:e68b678f99332c0d6ad607ce4c8f99007b9b612142be0ab04cb6c8b4b0cb5969"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,41 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 11: Bit operations"
+ "source": [
+ "Chapter 11: Bit operations"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 11.1, Page number: 193"
+ "source": [
+ "Example 11.1, Page number: 193"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 11.1.py\n# To check whether two numbers are equal to 0 or not\n\n\n# Variable declaration\ni1 = 4\ni2 = 2\n\n# Calculation and result\nif ((i1 != 0) and (i2 != 0)) :\n print ('Both are not zero\\n')",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "i1 = 4\n",
+ "i2 = 2\n",
+ "\n",
+ "# Calculation and result\n",
+ "if ((i1 != 0) and (i2 != 0)) :\n",
+ " print ('Both are not zero\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Both are not zero\n\n"
+ "text": [
+ "Both are not zero\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +55,42 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 11.2, Page number: 201"
+ "source": [
+ "Example 11.2, Page number: 201"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 11.2.py\n# To illustrate the use of bitwise and shift operators\n\n\n# Variable declaration\nHIGH_SPEED = 1 << 7\nDIRECT_CONNECT = 1 << 8\n\nflags = 0\nflags |= HIGH_SPEED\nflags |= DIRECT_CONNECT\n\n# Calculation and result\nif ((flags & HIGH_SPEED) != 0) :\n print ('High speed set\\n')\n\nif ((flags & DIRECT_CONNECT) != 0) :\n print ('Direct connect set\\n')",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "HIGH_SPEED = 1 << 7\n",
+ "DIRECT_CONNECT = 1 << 8\n",
+ "\n",
+ "flags = 0\n",
+ "flags |= HIGH_SPEED\n",
+ "flags |= DIRECT_CONNECT\n",
+ "\n",
+ "# Calculation and result\n",
+ "if ((flags & HIGH_SPEED) != 0) :\n",
+ " print ('High speed set\\n')\n",
+ "\n",
+ "if ((flags & DIRECT_CONNECT) != 0) :\n",
+ " print ('Direct connect set\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "High speed set\n\nDirect connect set\n\n"
+ "text": [
+ "High speed set\n",
+ "\n",
+ "Direct connect set\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +99,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 11.4, Page number: 207"
+ "source": [
+ "Example 11.4, Page number: 207"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 11.4.py\n# To test a loop counter\n\n\n# Variable declaration\ni = 0x80\n\n# Calculation and result\nif (i != 0) :\n print ('i is %x (%d) \\n' % (i, i))\n i = i >> 1",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "i = 0x80\n",
+ "\n",
+ "# Calculation and result\n",
+ "if (i != 0) :\n",
+ " print ('i is %x (%d) \\n' % (i, i))\n",
+ " i = i >> 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "i is 80 (128) \n\n"
+ "text": [
+ "i is 80 (128) \n",
+ "\n"
+ ]
}
],
"prompt_number": 3
diff --git a/Practical_C_Programming/Chapter_12_1.ipynb b/Practical_C_Programming/Chapter_12_1.ipynb
index b711a066..090a4ee1 100644
--- a/Practical_C_Programming/Chapter_12_1.ipynb
+++ b/Practical_C_Programming/Chapter_12_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 12"
+ "name": "",
+ "signature": "sha256:184261c50791da479b656a01719125426370a323ab51da034b4dc57d574572c3"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,43 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 12: Advanced types"
+ "source": [
+ "Chapter 12: Advanced types"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 12.1, Page number: 212"
+ "source": [
+ "Example 12.1, Page number: 212"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 12.1.py\n# To illustrate the use of a 'union'\n\n\n# Union declaration\nfrom ctypes import *\nclass value (Union) :\n _fields_ = [(\"i_value\", c_int),\n (\"f_value\", c_float)]\n\n# Calculation and result\ndata = value (3, 5.0)\nprint (data.i_value, data.f_value)",
+ "input": [
+ "\n",
+ "\n",
+ "# Union declaration\n",
+ "from ctypes import *\n",
+ "class value (Union) :\n",
+ " _fields_ = [(\"i_value\", c_int),\n",
+ " (\"f_value\", c_float)]\n",
+ "\n",
+ "# Calculation and result\n",
+ "data = value (3, 5.0)\n",
+ "print (data.i_value, data.f_value)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "(1084227584, 5.0)\n"
+ "text": [
+ "(1084227584, 5.0)\n"
+ ]
}
],
"prompt_number": 1
diff --git a/Practical_C_Programming/Chapter_13_1.ipynb b/Practical_C_Programming/Chapter_13_1.ipynb
index 95603acb..e3f18fc3 100644
--- a/Practical_C_Programming/Chapter_13_1.ipynb
+++ b/Practical_C_Programming/Chapter_13_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 13"
+ "name": "",
+ "signature": "sha256:e550d25cd0a5a3cbfd731b0cd1d4a1ad11e5182b8955b4cee863068b4cfdaf40"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,43 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 13: Simple pointers"
+ "source": [
+ "Chapter 13: Simple pointers"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.1, Page number: 225"
+ "source": [
+ "Example 13.1, Page number: 225"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.1.py\n# To print the value of 'thing_var' and 'thing_ptr'\n\n\n# Variable declaration\nthing_var = 2\n\n# Calculation and result\nprint ('Thing %d' % thing_var)\n\nthing_ptr = thing_var\nthing_ptr = 3\nprint ('Thing %d' % thing_ptr)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "thing_var = 2\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Thing %d' % thing_var)\n",
+ "\n",
+ "thing_ptr = thing_var\n",
+ "thing_ptr = 3\n",
+ "print ('Thing %d' % thing_ptr)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Thing 2\nThing 3\n"
+ "text": [
+ "Thing 2\n",
+ "Thing 3\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +57,56 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.2, Page number: 227"
+ "source": [
+ "Example 13.2, Page number: 227"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.2.py\n# To loop through the value of 'count' from 1 to 10\n\n\n# Function declaration\ndef inc_count (count_ptr) :\n count_ptr += 1\n return count_ptr\n\n# Calculation and result\ncount = 0\n\nwhile (count < 10) :\n print ('Count %d\\n' % inc_count (count))\n count += 1",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration\n",
+ "def inc_count (count_ptr) :\n",
+ " count_ptr += 1\n",
+ " return count_ptr\n",
+ "\n",
+ "# Calculation and result\n",
+ "count = 0\n",
+ "\n",
+ "while (count < 10) :\n",
+ " print ('Count %d\\n' % inc_count (count))\n",
+ " count += 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Count 1\n\nCount 2\n\nCount 3\n\nCount 4\n\nCount 5\n\nCount 6\n\nCount 7\n\nCount 8\n\nCount 9\n\nCount 10\n\n"
+ "text": [
+ "Count 1\n",
+ "\n",
+ "Count 2\n",
+ "\n",
+ "Count 3\n",
+ "\n",
+ "Count 4\n",
+ "\n",
+ "Count 5\n",
+ "\n",
+ "Count 6\n",
+ "\n",
+ "Count 7\n",
+ "\n",
+ "Count 8\n",
+ "\n",
+ "Count 9\n",
+ "\n",
+ "Count 10\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +115,54 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.3, Page number: 230"
+ "source": [
+ "Example 13.3, Page number: 230"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.3.py\n# To print the addresses and elements of a character array\n\n\n# Variable declaration\narray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n\n# Calculation and result\nprint ('&array[index] (array+index) array[index]\\n')\n\nfor index in range (0, 10) :\n print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('&array[index] (array+index) array[index]\\n')\n",
+ "\n",
+ "for index in range (0, 10) :\n",
+ " print ('0x%s 0x%s 0x%s\\n' % (id (array[index]), id (array[index]), array[index]))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "&array[index] (array+index) array[index]\n\n0x20916008 0x20916008 0x0\n\n0x20270280 0x20270280 0x1\n\n0x20733728 0x20733728 0x2\n\n0x20916464 0x20916464 0x3\n\n0x20270232 0x20270232 0x4\n\n0x20733560 0x20733560 0x5\n\n0x20270256 0x20270256 0x6\n\n0x20733752 0x20733752 0x7\n\n0x20916512 0x20916512 0x8\n\n0x20916032 0x20916032 0x9\n\n"
+ "text": [
+ "&array[index] (array+index) array[index]\n",
+ "\n",
+ "0x20916008 0x20916008 0x0\n",
+ "\n",
+ "0x20270280 0x20270280 0x1\n",
+ "\n",
+ "0x20733728 0x20733728 0x2\n",
+ "\n",
+ "0x20916464 0x20916464 0x3\n",
+ "\n",
+ "0x20270232 0x20270232 0x4\n",
+ "\n",
+ "0x20733560 0x20733560 0x5\n",
+ "\n",
+ "0x20270256 0x20270256 0x6\n",
+ "\n",
+ "0x20733752 0x20733752 0x7\n",
+ "\n",
+ "0x20916512 0x20916512 0x8\n",
+ "\n",
+ "0x20916032 0x20916032 0x9\n",
+ "\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +171,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.4, Page number: 232"
+ "source": [
+ "Example 13.4, Page number: 232"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.4.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n",
+ "index = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (array[index] != 0) :\n",
+ " index += 1\n",
+ "\n",
+ "print ('Number of elements before zero %d\\n' % index)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Number of elements before zero 6\n\n"
+ "text": [
+ "Number of elements before zero 6\n",
+ "\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +208,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.5, Page number: 232"
+ "source": [
+ "Example 13.5, Page number: 232"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.5.py\n# To count the number of elements before zero in an array\n\n\n# Variable declaration\narray = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\nindex = 0\n\n# Calculation and result\nwhile (array[index] != 0) :\n index += 1\n\nprint ('Number of elements before zero %d\\n' % index)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "array = [4, 5, 8, 9, 8, 1, 0, 1, 9, 3]\n",
+ "index = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (array[index] != 0) :\n",
+ " index += 1\n",
+ "\n",
+ "print ('Number of elements before zero %d\\n' % index)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Number of elements before zero 6\n\n"
+ "text": [
+ "Number of elements before zero 6\n",
+ "\n"
+ ]
}
],
"prompt_number": 5
@@ -122,19 +245,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.6, Page number: 233"
+ "source": [
+ "Example 13.6, Page number: 233"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.6.py\n# To initialize the elements of an array to 0\n\n\n# Variable declaration\ndata = []\n\n# Calculation and result\nfor index in range (0, 10) :\n data.append(0)\nprint (data)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "data = []\n",
+ "\n",
+ "# Calculation and result\n",
+ "for index in range (0, 10) :\n",
+ " data.append(0)\n",
+ "print (data)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
+ "text": [
+ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
+ ]
}
],
"prompt_number": 6
@@ -143,19 +279,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.10, Page number: 238"
+ "source": [
+ "Example 13.10, Page number: 238"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.10.py\n# To split a string of the form 'First/Last' into two strings\n\n\n# Variable declaration\nline = 'Steve/Oualline'\n\n# Calculation and result\nfirst_ptr, last_ptr = line.split('/')\nprint ('First: %s Last: %s\\n' % (first_ptr, last_ptr))",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "line = 'Steve/Oualline'\n",
+ "\n",
+ "# Calculation and result\n",
+ "first_ptr, last_ptr = line.split('/')\n",
+ "print ('First: %s Last: %s\\n' % (first_ptr, last_ptr))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "First: Steve Last: Oualline\n\n"
+ "text": [
+ "First: Steve Last: Oualline\n",
+ "\n"
+ ]
}
],
"prompt_number": 8
@@ -164,19 +314,37 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.11, Page number: 240"
+ "source": [
+ "Example 13.11, Page number: 240"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.11.py\n# To return a temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nprint ('Name: %s\\n' % tmp_name())",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def tmp_name() :\n",
+ " if not hasattr (tmp_name, 'sequence') :\n",
+ " tmp_name.sequence = 0\n",
+ " tmp_name.sequence += 1\n",
+ " name = 'tmp'\n",
+ " name += str (tmp_name.sequence)\n",
+ " return name\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Name: %s\\n' % tmp_name())"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Name: tmp1\n\n"
+ "text": [
+ "Name: tmp1\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -185,12 +353,87 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.12, Page number: 245"
+ "source": [
+ "Example 13.12, Page number: 245"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.12.py\n# To format files for printing\n\n\n# Variable and function declaration\nimport sys\nv_count = sys.argv\ncounter = len(sys.argv)\n\n# Produces verbose messages\nverbose = 0\n\n# Sends output to a file\nout_file = open ('print.out', 'w')\n\n# Sets the number of lines per page\nline_max = 66\n\ndef do_file (name) :\n print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n\ndef usage() :\n print ('Usage is %s [options] [file-list]\\n' % program_name)\n print ('Options\\n')\n print (' -v verbose\\n')\n print (' -l<number> Number of lines\\n')\n print (' -o<name> Set output filename\\n')\n sys.exit(1)\n \n# Calculation and result\nprogram_name = str (sys.argv[0])\n\nwhile ((counter > 1) and (sys.argv[1][0] == '-')) :\n if (sys.argv[1][1] == 'v') :\n verbose = 1\n break\n\n elif (sys.argv[1][1] == 'o') :\n temp = str (sys.argv[1])\n out_file.write (temp)\n break\n\n elif (sys.argv[1][1] == 'l') :\n line_max = int (sys.argv[1][2])\n break\n\n else :\n print ('Bad option %s\\n' % sys.argv[1])\n usage()\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n\n counter -= 1\n\nif (counter == 1) :\n do_file ('print.in')\n\nelse :\n while (counter > 1) :\n do_file (sys.argv[1])\n\n for index in range (0, counter) :\n if (index == counter - 1) :\n break\n else :\n v_count[index] = v_count[index + 1]\n \n counter -= 1\n\nout_file.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable and function declaration\n",
+ "import sys\n",
+ "v_count = sys.argv\n",
+ "counter = len(sys.argv)\n",
+ "\n",
+ "# Produces verbose messages\n",
+ "verbose = 0\n",
+ "\n",
+ "# Sends output to a file\n",
+ "out_file = open ('print.out', 'w')\n",
+ "\n",
+ "# Sets the number of lines per page\n",
+ "line_max = 66\n",
+ "\n",
+ "def do_file (name) :\n",
+ " print ('Verbose %d Lines %d Input %s Output %s\\n' % (verbose, line_max, name, out_file.name))\n",
+ "\n",
+ "def usage() :\n",
+ " print ('Usage is %s [options] [file-list]\\n' % program_name)\n",
+ " print ('Options\\n')\n",
+ " print (' -v verbose\\n')\n",
+ " print (' -l<number> Number of lines\\n')\n",
+ " print (' -o<name> Set output filename\\n')\n",
+ " sys.exit(1)\n",
+ " \n",
+ "# Calculation and result\n",
+ "program_name = str (sys.argv[0])\n",
+ "\n",
+ "while ((counter > 1) and (sys.argv[1][0] == '-')) :\n",
+ " if (sys.argv[1][1] == 'v') :\n",
+ " verbose = 1\n",
+ " break\n",
+ "\n",
+ " elif (sys.argv[1][1] == 'o') :\n",
+ " temp = str (sys.argv[1])\n",
+ " out_file.write (temp)\n",
+ " break\n",
+ "\n",
+ " elif (sys.argv[1][1] == 'l') :\n",
+ " line_max = int (sys.argv[1][2])\n",
+ " break\n",
+ "\n",
+ " else :\n",
+ " print ('Bad option %s\\n' % sys.argv[1])\n",
+ " usage()\n",
+ "\n",
+ " for index in range (0, counter) :\n",
+ " if (index == counter - 1) :\n",
+ " break\n",
+ " else :\n",
+ " v_count[index] = v_count[index + 1]\n",
+ "\n",
+ " counter -= 1\n",
+ "\n",
+ "if (counter == 1) :\n",
+ " do_file ('print.in')\n",
+ "\n",
+ "else :\n",
+ " while (counter > 1) :\n",
+ " do_file (sys.argv[1])\n",
+ "\n",
+ " for index in range (0, counter) :\n",
+ " if (index == counter - 1) :\n",
+ " break\n",
+ " else :\n",
+ " v_count[index] = v_count[index + 1]\n",
+ " \n",
+ " counter -= 1\n",
+ "\n",
+ "out_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -206,12 +449,27 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "Bad option -f\n\nUsage is -c [options] [file-list]\n\nOptions\n\n -v verbose\n\n -l<number> Number of lines\n\n -o<name> Set output filename\n\n"
+ "text": [
+ "Bad option -f\n",
+ "\n",
+ "Usage is -c [options] [file-list]\n",
+ "\n",
+ "Options\n",
+ "\n",
+ " -v verbose\n",
+ "\n",
+ " -l<number> Number of lines\n",
+ "\n",
+ " -o<name> Set output filename\n",
+ "\n"
+ ]
},
{
"output_type": "stream",
"stream": "stderr",
- "text": "To exit: use 'exit', 'quit', or Ctrl-D."
+ "text": [
+ "To exit: use 'exit', 'quit', or Ctrl-D."
+ ]
}
],
"prompt_number": 10
@@ -220,19 +478,43 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 13.13, Page number: 248"
+ "source": [
+ "Example 13.13, Page number: 248"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 13.13.py\n# To return a new temporary filename\n\n\n# Function declaration\ndef tmp_name() :\n if not hasattr (tmp_name, 'sequence') :\n tmp_name.sequence = 0\n tmp_name.sequence += 1\n name = 'tmp'\n name += str (tmp_name.sequence)\n return name\n\n# Calculation and result\nname1 = tmp_name()\nname2 = tmp_name()\nprint ('Name1: %s\\n' % name1)\nprint ('Name2: %s\\n' % name2)",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration\n",
+ "def tmp_name() :\n",
+ " if not hasattr (tmp_name, 'sequence') :\n",
+ " tmp_name.sequence = 0\n",
+ " tmp_name.sequence += 1\n",
+ " name = 'tmp'\n",
+ " name += str (tmp_name.sequence)\n",
+ " return name\n",
+ "\n",
+ "# Calculation and result\n",
+ "name1 = tmp_name()\n",
+ "name2 = tmp_name()\n",
+ "print ('Name1: %s\\n' % name1)\n",
+ "print ('Name2: %s\\n' % name2)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Name1: tmp1\n\nName2: tmp2\n\n"
+ "text": [
+ "Name1: tmp1\n",
+ "\n",
+ "Name2: tmp2\n",
+ "\n"
+ ]
}
],
"prompt_number": 11
diff --git a/Practical_C_Programming/Chapter_14_1.ipynb b/Practical_C_Programming/Chapter_14_1.ipynb
index b2876b44..6d3c736e 100644
--- a/Practical_C_Programming/Chapter_14_1.ipynb
+++ b/Practical_C_Programming/Chapter_14_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 14"
+ "name": "",
+ "signature": "sha256:aaf3603d1f605477459dd54990dcc4c8c154c9c808805468e92c30fde9487343"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,18 +12,42 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Chapter 14: File input/output"
+ "source": [
+ "Chapter 14: File input/output"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.1, Page number: 253"
+ "source": [
+ "Example 14.1, Page number: 253"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.1.py\n# To count the number of characters in the file 'input.txt'\n\n\n# Variable declaration\nimport os\ncount = 0\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists ('input.txt') :\n print ('Cannot open input.txt\\n')\n\nwhile (1) :\n ch = in_file.read(1)\n if not ch :\n break\n count += 1\n\nprint ('Number of characters in input.txt is %d\\n' % count)\n\nin_file.close()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import os\n",
+ "count = 0\n",
+ "in_file = open ('input.txt', 'r')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('input.txt') :\n",
+ " print ('Cannot open input.txt\\n')\n",
+ "\n",
+ "while (1) :\n",
+ " ch = in_file.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " count += 1\n",
+ "\n",
+ "print ('Number of characters in input.txt is %d\\n' % count)\n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -43,12 +68,29 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.3, Page number: 257"
+ "source": [
+ "Example 14.3, Page number: 257"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.3.py\n# To check whether a file exists or not\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists (name) :\n print ('Could not open file\\n')\n sys.exit(1)\nprint ('File found\\n')\n\nin_file.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "in_file = open ('input.txt', 'r')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists (name) :\n",
+ " print ('Could not open file\\n')\n",
+ " sys.exit(1)\n",
+ "print ('File found\\n')\n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -69,12 +111,31 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.4, Page number: 260"
+ "source": [
+ "Example 14.4, Page number: 260"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.4.py\n# To write to a file\n\n\n# Variable declaration\nimport sys\nimport os\nout_file = open ('test.out', 'w')\n \n# Calculation and result\nif not os.path.exists ('test.out') :\n print ('Cannot open output file\\n')\n sys.exit(1)\n\nfor cur_char in range (0, 128) :\n out_file.write(str (cur_char))\n out_file.write('\\n')\n\nout_file.close()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "out_file = open ('test.out', 'w')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('test.out') :\n",
+ " print ('Cannot open output file\\n')\n",
+ " sys.exit(1)\n",
+ "\n",
+ "for cur_char in range (0, 128) :\n",
+ " out_file.write(str (cur_char))\n",
+ " out_file.write('\\n')\n",
+ "\n",
+ "out_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -84,12 +145,29 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.5, Page number: 267"
+ "source": [
+ "Example 14.5, Page number: 267"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.5.py\n# To copy the contents of a file to another file\n\n\n# Variable declaration\ntxt_file = open ('input.txt')\n\nsource_content = txt_file.read()\n\ntarget = open ('output.txt', 'w')\n\n# Calculation and result\ntarget.write(source_content)\nprint ('Content copied')\n\ntarget.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "txt_file = open ('input.txt')\n",
+ "\n",
+ "source_content = txt_file.read()\n",
+ "\n",
+ "target = open ('output.txt', 'w')\n",
+ "\n",
+ "# Calculation and result\n",
+ "target.write(source_content)\n",
+ "print ('Content copied')\n",
+ "\n",
+ "target.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Practical_C_Programming/Chapter_15_1.ipynb b/Practical_C_Programming/Chapter_15_1.ipynb
index 72f7aaee..34a86a67 100644
--- a/Practical_C_Programming/Chapter_15_1.ipynb
+++ b/Practical_C_Programming/Chapter_15_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 15"
+ "name": "",
+ "signature": "sha256:23b3891085deb8af52412d44380b6187a4110d7e026531ec90b3b2ae79d61d0f"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,53 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 15: Debugging and optimization"
+ "source": [
+ "Chapter 15: Debugging and optimization"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.1, Page number: 275"
+ "source": [
+ "Example 15.1, Page number: 275"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.1.py\n# A database program to look up names in a hardcoded list\n\n\n# Function declaration\ndef lookup (name) :\n lists = ['John', 'Jim', 'Jane', 'Clyde']\n result = 0\n\n for index in range (0, 4) :\n if (lists[index] == name) :\n result = 1\n break\n \n return result\n\n# Calculation and result\nname = 'John'\n\nif (lookup (name)) :\n print ('%s is in the list\\n' % name)\nelse :\n print ('%s is not in the list\\n' % name)",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def lookup (name) :\n",
+ " lists = ['John', 'Jim', 'Jane', 'Clyde']\n",
+ " result = 0\n",
+ "\n",
+ " for index in range (0, 4) :\n",
+ " if (lists[index] == name) :\n",
+ " result = 1\n",
+ " break\n",
+ " \n",
+ " return result\n",
+ "\n",
+ "# Calculation and result\n",
+ "name = 'John'\n",
+ "\n",
+ "if (lookup (name)) :\n",
+ " print ('%s is in the list\\n' % name)\n",
+ "else :\n",
+ " print ('%s is not in the list\\n' % name)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "John is in the list\n\n"
+ "text": [
+ "John is in the list\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -38,19 +67,48 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.6, Page number: 288"
+ "source": [
+ "Example 15.6, Page number: 288"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.6.py\n# To count the number of 3's and 7's in an array\n\n\n# Variable and function declaration\nseven_count = 0\nthree_count = 0\ndata = []\n\ndef get_data (data) :\n for i in range (0, 5) :\n x = 3\n data.append(int(x))\n print (data)\n\n# Calculation\nget_data (data)\nfor index in range (0, 5) :\n if data[index] == 3 :\n three_count += 1\n\n if data[index] == 7 :\n seven_count += 1\n\n# Result\nprint ('Threes %d Sevens %d' % (three_count, seven_count))",
+ "input": [
+ "\n",
+ "# Variable and function declaration\n",
+ "seven_count = 0\n",
+ "three_count = 0\n",
+ "data = []\n",
+ "\n",
+ "def get_data (data) :\n",
+ " for i in range (0, 5) :\n",
+ " x = 3\n",
+ " data.append(int(x))\n",
+ " print (data)\n",
+ "\n",
+ "# Calculation\n",
+ "get_data (data)\n",
+ "for index in range (0, 5) :\n",
+ " if data[index] == 3 :\n",
+ " three_count += 1\n",
+ "\n",
+ " if data[index] == 7 :\n",
+ " seven_count += 1\n",
+ "\n",
+ "# Result\n",
+ "print ('Threes %d Sevens %d' % (three_count, seven_count))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[3, 3, 3, 3, 3]\nThrees 5 Sevens 0\n"
+ "text": [
+ "[3, 3, 3, 3, 3]\n",
+ "Threes 5 Sevens 0\n"
+ ]
}
],
"prompt_number": 4
@@ -59,12 +117,60 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.7, Page number: 292"
+ "source": [
+ "Example 15.7, Page number: 292"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.7.py\n# To perform binary search on a number in a file 'numbers.dat'\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('numbers.dat', 'r')\n\ndata = []\nmax_count = 0\n \n# Calculation and result\nif not os.path.exists ('numbers.dat') :\n print ('Error:Unable to open numbers.dat\\n')\n sys.exit(1)\n\nfor line in in_file :\n data.append(line)\n max_count += 1\n\nwhile (1) :\n search = 6\n \n if (search == -1) :\n break\n\n low = 0\n high = max_count\n\n while (1) :\n mid = (low + high) / 2\n middle = int (mid) \n\n if (int (data[middle]) == search) :\n print ('Found at index %d\\n' % (middle + 1))\n break\n\n if (low == high) :\n print ('Not found\\n')\n break\n\n if (int (data[middle]) < search) :\n low = middle\n else :\n high = middle \n\nin_file.close()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "in_file = open ('numbers.dat', 'r')\n",
+ "\n",
+ "data = []\n",
+ "max_count = 0\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('numbers.dat') :\n",
+ " print ('Error:Unable to open numbers.dat\\n')\n",
+ " sys.exit(1)\n",
+ "\n",
+ "for line in in_file :\n",
+ " data.append(line)\n",
+ " max_count += 1\n",
+ "\n",
+ "while (1) :\n",
+ " search = 6\n",
+ " \n",
+ " if (search == -1) :\n",
+ " break\n",
+ "\n",
+ " low = 0\n",
+ " high = max_count\n",
+ "\n",
+ " while (1) :\n",
+ " mid = (low + high) / 2\n",
+ " middle = int (mid) \n",
+ "\n",
+ " if (int (data[middle]) == search) :\n",
+ " print ('Found at index %d\\n' % (middle + 1))\n",
+ " break\n",
+ "\n",
+ " if (low == high) :\n",
+ " print ('Not found\\n')\n",
+ " break\n",
+ "\n",
+ " if (int (data[middle]) < search) :\n",
+ " low = middle\n",
+ " else :\n",
+ " high = middle \n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -85,12 +191,61 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.8, Page number: 300"
+ "source": [
+ "Example 15.8, Page number: 300"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.8.py\n# To perform binary search on a number in a file 'numbers.dat'\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('numbers.dat', 'r')\n\ndata = []\nmax_count = 0\n \n# Calculation and result\nif not os.path.exists ('numbers.dat') :\n print ('Error:Unable to open numbers.dat\\n')\n sys.exit(1)\n\nfor line in in_file :\n data.append(line)\n max_count += 1\n\nwhile (1) :\n search = 14\n \n if (search == -1) :\n break\n\n low = 0\n high = max_count\n\n while (1) :\n mid = (low + high) / 2\n middle = int (mid) \n\n if (int (data[middle]) == search) :\n print ('Found at index %d\\n' % (middle + 1))\n break\n\n if (low == high) :\n print ('Not found\\n')\n break\n\n if (int (data[middle]) < search) :\n low = middle\n else :\n high = middle \n\nin_file.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "in_file = open ('numbers.dat', 'r')\n",
+ "\n",
+ "data = []\n",
+ "max_count = 0\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('numbers.dat') :\n",
+ " print ('Error:Unable to open numbers.dat\\n')\n",
+ " sys.exit(1)\n",
+ "\n",
+ "for line in in_file :\n",
+ " data.append(line)\n",
+ " max_count += 1\n",
+ "\n",
+ "while (1) :\n",
+ " search = 14\n",
+ " \n",
+ " if (search == -1) :\n",
+ " break\n",
+ "\n",
+ " low = 0\n",
+ " high = max_count\n",
+ "\n",
+ " while (1) :\n",
+ " mid = (low + high) / 2\n",
+ " middle = int (mid) \n",
+ "\n",
+ " if (int (data[middle]) == search) :\n",
+ " print ('Found at index %d\\n' % (middle + 1))\n",
+ " break\n",
+ "\n",
+ " if (low == high) :\n",
+ " print ('Not found\\n')\n",
+ " break\n",
+ "\n",
+ " if (int (data[middle]) < search) :\n",
+ " low = middle\n",
+ " else :\n",
+ " high = middle \n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -111,12 +266,25 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.10, Page number: 304"
+ "source": [
+ "Example 15.10, Page number: 304"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.10.py\n# To illustrate divide by zero error\n\n\n# Variable declaration\ni = 1\nj = 0\n\n# Calculation and result\nprint ('Starting\\n')\nprint ('Before divide...')\ni = i / j\nprint ('After\\n')",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "i = 1\n",
+ "j = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Starting\\n')\n",
+ "print ('Before divide...')\n",
+ "i = i / j\n",
+ "print ('After\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -133,7 +301,11 @@
{
"output_type": "stream",
"stream": "stdout",
- "text": "Starting\n\nBefore divide...\n"
+ "text": [
+ "Starting\n",
+ "\n",
+ "Before divide...\n"
+ ]
}
],
"prompt_number": 7
@@ -142,24 +314,46 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 15.11, Page number: 304"
+ "source": [
+ "Example 15.11, Page number: 304"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 15.11.py\n# To illustrate divide by zero error with flush() function\n\n\n# Variable declaration\nimport sys\ni = 1\nj = 0\n\n# Calculation and result\nprint ('Starting\\n')\nsys.stdout.flush()\nprint ('Before divide...')\nsys.stdout.flush()\ni = i / j\nprint ('After\\n')\nsys.stdout.flush()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "i = 1\n",
+ "j = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Starting\\n')\n",
+ "sys.stdout.flush()\n",
+ "print ('Before divide...')\n",
+ "sys.stdout.flush()\n",
+ "i = i / j\n",
+ "print ('After\\n')\n",
+ "sys.stdout.flush()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Starting\n\n"
+ "text": [
+ "Starting\n",
+ "\n"
+ ]
},
{
"output_type": "stream",
"stream": "stdout",
- "text": "Before divide...\n"
+ "text": [
+ "Before divide...\n"
+ ]
},
{
"ename": "ZeroDivisionError",
diff --git a/Practical_C_Programming/Chapter_16_1.ipynb b/Practical_C_Programming/Chapter_16_1.ipynb
index d1d60ff9..505718fd 100644
--- a/Practical_C_Programming/Chapter_16_1.ipynb
+++ b/Practical_C_Programming/Chapter_16_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 16"
+ "name": "",
+ "signature": "sha256:8846db95d13f18ddbaba03cb28aa141be8268e397ec14735e02a0f66209b0b25"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,57 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 16: Floating point"
+ "source": [
+ "Chapter 16: Floating point"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 16.1, Page number: 322"
+ "source": [
+ "Example 16.1, Page number: 322"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 16.1.py\n# To calculate the accuracy of calculations and storage\n\n\n# Variable declaration\nnumber1 = 1.0\nnumber2 = 1.0\ncounter = 0\n\n# Calculation and result\nwhile (number1 + number2 != number1) :\n counter += 1\n number2 = number2 / 10.0\n\nprint ('%2d digits accuracy in calculations\\n' % counter)\n\n\nnumber2 = 1.0\ncounter = 0\n\nresult = number1 + number2\ncounter += 1\nnumber2 = number2 / 10.0\n\nprint ('%2d digits accuracy in storage\\n' % counter)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "number1 = 1.0\n",
+ "number2 = 1.0\n",
+ "counter = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (number1 + number2 != number1) :\n",
+ " counter += 1\n",
+ " number2 = number2 / 10.0\n",
+ "\n",
+ "print ('%2d digits accuracy in calculations\\n' % counter)\n",
+ "\n",
+ "\n",
+ "number2 = 1.0\n",
+ "counter = 0\n",
+ "\n",
+ "result = number1 + number2\n",
+ "counter += 1\n",
+ "number2 = number2 / 10.0\n",
+ "\n",
+ "print ('%2d digits accuracy in storage\\n' % counter)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "16 digits accuracy in calculations\n\n 1 digits accuracy in storage\n\n"
+ "text": [
+ "16 digits accuracy in calculations\n",
+ "\n",
+ " 1 digits accuracy in storage\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
diff --git a/Practical_C_Programming/Chapter_17_1.ipynb b/Practical_C_Programming/Chapter_17_1.ipynb
index c9e7bf83..3d179b54 100644
--- a/Practical_C_Programming/Chapter_17_1.ipynb
+++ b/Practical_C_Programming/Chapter_17_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 17"
+ "name": "",
+ "signature": "sha256:359ba0990b58235e80b2e0192de2b1c02844fb6d8275025bc2e3f3cf33cfcdf1"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,18 +12,29 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 17: Advanced Pointers "
+ "source": [
+ "Chapter 17: Advanced Pointers "
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 17.1, Page number: 331"
+ "source": [
+ "Example 17.1, Page number: 331"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 17.1\n# To declare a structure\n\n\n# Structure declaration\nfrom ctypes import *\n\nclass person (Structure) :\n_fields_ = [(\"name\", c_wchar_p), (\"address\", c_wchar_p), (\"city_state_zip\", c_wchar_p), (\"age\", c_int), (\"height\", c_float)]",
+ "input": [
+ "\n",
+ "# Structure declaration\n",
+ "from ctypes import *\n",
+ "\n",
+ "class person (Structure) :\n",
+ "_fields_ = [(\"name\", c_wchar_p), (\"address\", c_wchar_p), (\"city_state_zip\", c_wchar_p), (\"age\", c_int), (\"height\", c_float)]"
+ ],
"language": "python",
"metadata": {},
"outputs": []
@@ -31,19 +43,45 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 17.2, Page number: 336"
+ "source": [
+ "Example 17.2, Page number: 336"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 17.2\n# To look for a data item in a list\n\n\n# Function declaration\ndef lookup (name) :\n lists = ['John', 'Jim', 'Jane', 'Clyde']\n result = 0\n\n for index in range (0, 4) :\n if (lists[index] == name) :\n result = 1\n break\n \n return result\n\n# Calculation and result\nname = 'Jane'\n\nif (lookup (name)) :\n print ('%s is in the list\\n' % name)\nelse :\n print ('%s is not in the list\\n' % name)",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def lookup (name) :\n",
+ " lists = ['John', 'Jim', 'Jane', 'Clyde']\n",
+ " result = 0\n",
+ "\n",
+ " for index in range (0, 4) :\n",
+ " if (lists[index] == name) :\n",
+ " result = 1\n",
+ " break\n",
+ " \n",
+ " return result\n",
+ "\n",
+ "# Calculation and result\n",
+ "name = 'Jane'\n",
+ "\n",
+ "if (lookup (name)) :\n",
+ " print ('%s is in the list\\n' % name)\n",
+ "else :\n",
+ " print ('%s is not in the list\\n' % name)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Jane is in the list\n\n"
+ "text": [
+ "Jane is in the list\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -52,19 +90,31 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 17.3, Page number: 338"
+ "source": [
+ "Example 17.3, Page number: 338"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 17.3\n# To insert an element at an index in a list\n\n\n# Declaration and result\naList = [45, 89, 123]\n\naList.insert(1, 53)\n\nprint 'Final List : ', aList",
+ "input": [
+ "\n",
+ "# Declaration and result\n",
+ "aList = [45, 89, 123]\n",
+ "\n",
+ "aList.insert(1, 53)\n",
+ "\n",
+ "print 'Final List : ', aList"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Final List : [45, 53, 89, 123]\n"
+ "text": [
+ "Final List : [45, 53, 89, 123]\n"
+ ]
}
],
"prompt_number": 3
@@ -73,19 +123,71 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 17.4, Page number: 348"
+ "source": [
+ "Example 17.4, Page number: 348"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 17.4\n# To print the contents of a binary search tree in ASCII order\n\n\n# Class declaration\nclass Node:\n def __init__(self, val):\n self.l_child = None\n self.r_child = None\n self.data = val\n\ndef binary_insert(root, node):\n if root is None:\n root = node\n else:\n if root.data > node.data:\n if root.l_child == None:\n root.l_child = node\n else:\n binary_insert(root.l_child, node)\n else:\n if root.r_child == None:\n root.r_child = node\n else:\n binary_insert(root.r_child, node)\n\ndef in_order_print(root):\n if not root:\n return\n in_order_print(root.l_child)\n print root.data\n in_order_print(root.r_child)\n\nr = Node('Lemon')\nbinary_insert(r, Node('Plum'))\nbinary_insert(r, Node('Apple'))\nbinary_insert(r, Node('Orange'))\nbinary_insert(r, Node('Pear'))\nbinary_insert(r, Node('Grape'))\n\n\n# Result\nprint \"List of words in ASCII order:\"\nin_order_print(r)",
+ "input": [
+ "\n",
+ "# Class declaration\n",
+ "class Node:\n",
+ " def __init__(self, val):\n",
+ " self.l_child = None\n",
+ " self.r_child = None\n",
+ " self.data = val\n",
+ "\n",
+ "def binary_insert(root, node):\n",
+ " if root is None:\n",
+ " root = node\n",
+ " else:\n",
+ " if root.data > node.data:\n",
+ " if root.l_child == None:\n",
+ " root.l_child = node\n",
+ " else:\n",
+ " binary_insert(root.l_child, node)\n",
+ " else:\n",
+ " if root.r_child == None:\n",
+ " root.r_child = node\n",
+ " else:\n",
+ " binary_insert(root.r_child, node)\n",
+ "\n",
+ "def in_order_print(root):\n",
+ " if not root:\n",
+ " return\n",
+ " in_order_print(root.l_child)\n",
+ " print root.data\n",
+ " in_order_print(root.r_child)\n",
+ "\n",
+ "r = Node('Lemon')\n",
+ "binary_insert(r, Node('Plum'))\n",
+ "binary_insert(r, Node('Apple'))\n",
+ "binary_insert(r, Node('Orange'))\n",
+ "binary_insert(r, Node('Pear'))\n",
+ "binary_insert(r, Node('Grape'))\n",
+ "\n",
+ "\n",
+ "# Result\n",
+ "print \"List of words in ASCII order:\"\n",
+ "in_order_print(r)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "List of words in ASCII order:\nApple\nGrape\nLemon\nOrange\nPear\nPlum\n"
+ "text": [
+ "List of words in ASCII order:\n",
+ "Apple\n",
+ "Grape\n",
+ "Lemon\n",
+ "Orange\n",
+ "Pear\n",
+ "Plum\n"
+ ]
}
],
"prompt_number": 4
diff --git a/Practical_C_Programming/Chapter_18_1.ipynb b/Practical_C_Programming/Chapter_18_1.ipynb
index ac690274..0369d712 100644
--- a/Practical_C_Programming/Chapter_18_1.ipynb
+++ b/Practical_C_Programming/Chapter_18_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 18"
+ "name": "",
+ "signature": "sha256:2d44df2c3fb888f51302e9d5a213984c3dfb884f4d72dcb60cfc8091b69443e6"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,46 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 18: Modular Programming"
+ "source": [
+ "Chapter 18: Modular Programming"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 18.2, Page number: 364"
+ "source": [
+ "Example 18.2, Page number: 364"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 18.2\n# To create an infinite array\n\n\n# Variable declaration\narray = [1, 2, 3, 4, 5]\narray_size = 10\nnum = 6\n\n# Calculation\nfor index in range (5, 10) :\n array.insert(index, num)\n num = num + 1\n\n# Result\nprint \"Contents of array of size 10 elements is\", array",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "array = [1, 2, 3, 4, 5]\n",
+ "array_size = 10\n",
+ "num = 6\n",
+ "\n",
+ "# Calculation\n",
+ "for index in range (5, 10) :\n",
+ " array.insert(index, num)\n",
+ " num = num + 1\n",
+ "\n",
+ "# Result\n",
+ "print \"Contents of array of size 10 elements is\", array"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Contents of array of size 10 elements is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
+ "text": [
+ "Contents of array of size 10 elements is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n"
+ ]
}
],
"prompt_number": 1
@@ -38,20 +60,44 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 18.3, Page number: 372"
+ "source": [
+ "Example 18.3, Page number: 372"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 18.3\n# To plot a histogram of an array of numbers\n\n\n# Calculation\nimport numpy as np \n\n# hist indicates that there are 0 items in bin #0, 2 in bin #1, 4 in bin #3, 1 in bin #4\n# bin_edges indicates that bin #0 is the interval [0,1), bin #1 is [1,2), ..., bin #3 is [3,4)\n\nhist, bin_edges = np.histogram([1, 1, 2, 2, 2, 2, 3], bins = range(5))\n\n\n# Result\nimport matplotlib.pyplot as plt\nplt.bar(bin_edges[:-1], hist, width=1) and plt.xlim(min(bin_edges), max(bin_edges))\nplt.savefig('histogram.png')\n\nfrom IPython.core.display import Image \nImage(filename='histogram.png')",
+ "input": [
+ "\n",
+ "\n",
+ "# Calculation\n",
+ "import numpy as np \n",
+ "\n",
+ "# hist indicates that there are 0 items in bin #0, 2 in bin #1, 4 in bin #3, 1 in bin #4\n",
+ "# bin_edges indicates that bin #0 is the interval [0,1), bin #1 is [1,2), ..., bin #3 is [3,4)\n",
+ "\n",
+ "hist, bin_edges = np.histogram([1, 1, 2, 2, 2, 2, 3], bins = range(5))\n",
+ "\n",
+ "\n",
+ "# Result\n",
+ "import matplotlib.pyplot as plt\n",
+ "plt.bar(bin_edges[:-1], hist, width=1) and plt.xlim(min(bin_edges), max(bin_edges))\n",
+ "plt.savefig('histogram.png')\n",
+ "\n",
+ "from IPython.core.display import Image \n",
+ "Image(filename='histogram.png')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
+ "metadata": {},
"output_type": "pyout",
"png": "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\nPZo3obfiqquuisbGxnj00Ueb1vbv3x/l5eUxefLkOPvssyMiYvv27bF58+Y4ePBgs3N37NgRa9as\naVrbvXt3rF69OmbPnh2nn356uicCAADdSCabzWa7eojuqqSkJJ5++um45ZZb4txzz42VK1fGxo0b\n42c/+1lcfPHFEXH4p6VXVFREbW1tjBw5MiIO/yT0iy++OH7729/GwoULY8iQIfHII4/EBx98EK+/\n/nqMGTOmK58WAAB0GbdgtaGioiLuueeeeOKJJ+Ljjz+Or3zlK/HTn/60KT4iIjKZTLPbrCIicnJy\nYu3atbFw4cJYunRp7Nu3Ly688MKoqKgQHwAA9GpuwWpD375948EHH4xt27bFvn374rXXXouvf/3r\nzY4pLy+PxsbGplc/jhg8eHCsWLEidu3aFR999FFMnDgx/vEf/zE+85nPxOTJk2PdunXtmmHPnj0x\nb968GDZsWAwYMCAuueSS2LRp00l7jt3Z/v374/bbb4/8/PwOXbfHH388cnJyjvq1c+fOBJN3jT/9\n6U+xePHimDFjRnz2s5+NnJycWLlyZbvP76177USuW2/da6+//nrcdNNNMW7cuBgwYECcc845UVJS\nEu+88067zu+te+1Erltv3Wu/+93voqioKM4999zIzc2NIUOGRGFhYVRWtu8jY3vrXjuR69Zb99rR\n3H///ZGTkxPnn39+u47vrfvteHgFJIHrrrsunnrqqbjllltizJgxUV5eHpdeemm89NJLbX5826FD\nh2LWrFnx5ptvxqJFi5pu5Zo6dWq88cYbMXr06ITPIr3jvW5HlJWVxahRo5qtDRo0qLPG7XK7du2K\nsrKyOOecc2L8+PHx8ssvt3h1rjW9ea+dyHU7orfttQceeCDWr18fRUVF8eUvfznq6upi2bJlUVBQ\nEK+99lqMGzeu1XN78147ket2RG/ba1u3bo1PP/00rrvuusjPz4/6+vqoqqqKa665Jmpra+M73/lO\nq+f25r12ItftiN621/7aBx98EP/6r/8aubm57fr/hN68345Llk61YcOGbCaTyX7/+99vWmtoaMiO\nHj06W1hY2Oa5P/rRj7KZTCb71FNPNa3t2rUre+aZZ2b/+Z//udNm7g5O5LqVl5dnM5lM9o033ujs\nMbuV/fv3Z3fs2JHNZrPZjRs3ZjOZTHblypXtOrc377UTuW69da/993//d/bAgQPN1t55551sv379\nsnPmzGnz3N68107kuvXWvXY0jY2N2fHjx2dHjhzZ5nG9ea8dTXuvm712WElJSfbv//7vs1OnTs1+\n6UtfOubx9lvHuAWrk1VVVUWfPn1i3rx5TWt9+/aN66+/PtavXx8ffvhhm+cOHz48rrjiiqa1oUOH\nRnFxcTzzzDNx4MCBTp29K53IdTsim83G3r17o7GxsTNH7TbOOOOMOOussyLi8HPviN68107kuh3R\n2/baV7/61ejTp/kL6KNHj44vfvGLsXnz5jbP7c177USu2xG9ba8dTU5OTowYMeKYnyjZm/fa0bT3\nuh3Rm/faL37xi3jqqadiyZIlLX6kQmvst44RIJ1s06ZNMXbs2BgwYECz9YkTJ0ZExK9+9as2zy0o\nKGixPnHixKivr4+ampqTO2w3ciLX7Yhp06bFoEGDIjc3Ny6//PLYsmVLp8x6KujNe+1ksNcO/2Fl\nx44dMXTo0DaPs9eaa+91O6K37rX6+vrYvXt3/OEPf4iHH344nn/++Vi0aFGb59hrx3fdjuite62x\nsTFuvvnm+Pa3v92u2yKPsN86xntAOlldXV3k5eW1WD+ydrSfmP5/z506dWqb53bkP46e5ESuW25u\nbsydOzemTZsWAwcOjI0bN8ZDDz0UhYWFUV1dHSNGjOi0uXuq3rzXToS99heVlZWxbdu2+O53v9vm\ncfZac+29br19r916661NP5erT58+sXTp0mavkB+NvXZ8162377Uf/OAHsXXr1vj5z3/eofPst44R\nIJ1s37590bdv3xbr/fr1a/p+axoaGo773J7uRK5bUVFRFBUVNf36sssui+nTp8eUKVPi/vvvj+XL\nl5/8gXu43rzXToS9dtjmzZvjxhtvjMLCwrj22mvbPNZe+4uOXLfevtduueWWKC4ujm3btkVlZWXc\ndNNN0b9//zavm712fNetN++1jz76KO6999649957Y8iQIR06137rGAHSyfr37x/79+9vsd7Q0ND0\n/c44t6c72c/9oosuikmTJrX74497m96810623rbXtm/fHrNmzYozzzwzqqqqjnmvtL12WEev29H0\npr123nnnxXnnnRcREXPmzInp06fHggULori4uNU9Y68d33U7mt6y1+6+++4YOnRo3HzzzR0+137r\nGO8B6WR5eXlHvV2orq4uIiLy8/M75dyerjOe+4gRI+Ljjz8+4dlORb15r3WG3rLXPvnkk5g5c2b8\n8Y9/jP/6r/+K4cOHH/Mce+34rltreste+2tXXnllfPLJJ/H222+3eoy91lJ7rltrTvW99s4778SK\nFSvi5ptvjg8++CBqa2ujtrY2Ghoa4s9//nO8//77bT5/+61jBEgnu+CCC6Kmpib27t3bbH3Dhg0R\nETF+/PhWzx0/fnxUV1e3+GSeDRs2RG5ubowdO/bkD9xNnMh1a827774bw4YNOynznWp6817rDL1h\nrzU0NMTs2bNjy5Yt8dOf/jQ+//nPt+u83r7Xjve6taY37LWjOXI7S05O63+M6e177Wjac91ac6rv\ntQ8//DAOHToU8+fPj7/9279t+vrlL38ZNTU1MWrUqCgrK2v1fPutYwRIJ7vqqquisbGx6U1gEYd/\nwnd5eXlMnjw5zj777Ig4/HL85s2b4+DBg83O3bFjR6xZs6Zpbffu3bF69eqYPXt2uz9Kryc6keu2\na9euFo+3du3aqK6ujhkzZnT+8N2cvXZ87LW/aGxsjJKSktiwYUOsXr06Jk2adNTj7LXmTuS69da9\ndrTnfeDAgaioqIghQ4Y0vam3rq7OXvs/TuS69da9dv7558fTTz8dP/7xj5u+nn766Rg3blycc845\n8eMf/ziuv/76iLDfToZM9ng/+J52KykpiaeffjpuueWWOPfcc2PlypWxcePG+NnPfhYXX3xxRBz+\nqd8VFRVRW1sbI0eOjIjDP1Xz4osvjt/+9rexcOHCpp+q+cEHH8Trr78eY8aM6cqn1emO97qNGTMm\nCgoKYsKECTFo0KCorq6Oxx57LM4+++x4/fXXT+m/wVm2bFns2bMntm3bFj/4wQ/iiiuuaHq1aP78\n+TFw4EB77SiO97r11r22YMGCWLp0acyePbvZm1WPmDNnTkT4fe2vnch166177Z/+6Z9i7969MWXK\nlMjPz4/t27dHZWVl1NTURHl5eXzzm9+MCHvtr53Ideute601U6dOjY8++ih+85vfNK3ZbydB+p99\n2Ps0NDRkFy5cmM3Ly8v269cvO2nSpOwLL7zQ7Jjrrrsum5OTk33//febrX/88cfZb33rW9mhQ4dm\nc3Nzs9OmTes1P530eK/b3Xffnb3ggguygwcPzp5xxhnZz33uc9kbb7wxu3PnztRPIbnPfe5z2Uwm\nk81kMtmcnJxsTk5O0z8fuUb2WkvHe916616bOnVq0zX666+cnJym4+y15k7kuvXWvfYf//Ef2a9/\n/evZ4cOHZ08//fTskCFDspdeeml23bp1zY6z15o7kevWW/daa6ZOnZo9//zzm63ZbyfOKyAAAEAy\n3gMCAAAkI0AAAIBkBAgAAJCMAAEAAJIRIAAAQDICBAAASEaAAAAAyQgQAAAgGQECAAAkI0AAAIBk\nBAgAAJCMAAEAAJIRIAAAQDICBAAASEaAAAAAyQgQAAAgGQECAAAkI0AAAIBkBAgAAJCMAAEAAJIR\nIAAAQDICBAAASEaAAAAAyQgQAAAgGQECAAAkI0AAAIBkBAgAAJCMAAEAAJIRIAAAQDICBAAASEaA\nAAAAyQgQAAAgGQECAAAkI0AAAIBkBAgAAJCMAAEAAJIRIAAAQDICBAAASEaAAAAAyQgQAAAgmf8P\npDpIO5V4wm8AAAAASUVORK5CYII=\n",
"prompt_number": 5,
- "text": "<IPython.core.display.Image at 0x5a46130>"
+ "text": [
+ "<IPython.core.display.Image at 0x5a46130>"
+ ]
}
],
"prompt_number": 5
diff --git a/Practical_C_Programming/Chapter_19_1.ipynb b/Practical_C_Programming/Chapter_19_1.ipynb
index f02444d9..00ddd7e8 100644
--- a/Practical_C_Programming/Chapter_19_1.ipynb
+++ b/Practical_C_Programming/Chapter_19_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 19"
+ "name": "",
+ "signature": "sha256:4ee1b1671641fa0922389d9bf11b94f42d9755534b4f0476bbe2f9ceb3b1bc7e"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,41 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 19: Ancient compilers"
+ "source": [
+ "Chapter 19: Ancient compilers"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 19.1, Page number: 385"
+ "source": [
+ "Example 19.1, Page number: 385"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 19.1.py\n# To calculate the area of a rectangle\n\n\n# Function declaration\ndef area (width, height) :\n return width * height\n\n# Calculation and result\nsize = area (3.0, 2)\nprint ('Area is %f\\n' % size)",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def area (width, height) :\n",
+ " return width * height\n",
+ "\n",
+ "# Calculation and result\n",
+ "size = area (3.0, 2)\n",
+ "print ('Area is %f\\n' % size)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Area is 6.000000\n\n"
+ "text": [
+ "Area is 6.000000\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +55,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 19.2, Page number: 386"
+ "source": [
+ "Example 19.2, Page number: 386"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 19.2.py\n# To calculate the square of a number\n\n\n# Function declaration\ndef square (s) :\n return s * s\n\n# Calculation and result\ni = square (5)\nprint ('i is %d\\n' % i)",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def square (s) :\n",
+ " return s * s\n",
+ "\n",
+ "# Calculation and result\n",
+ "i = square (5)\n",
+ "print ('i is %d\\n' % i)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "i is 25\n\n"
+ "text": [
+ "i is 25\n",
+ "\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +90,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 19.3, Page number: 386"
+ "source": [
+ "Example 19.3, Page number: 386"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 19.3.py\n# To calculate the sum of 3 numbers\n\n\n# Function declaration\ndef sum (i1, i2, i3) :\n return i1 + i2 + i3\n\n# Calculation and result\nprint ('Sum is %d\\n' % sum (1, 2, 3))",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def sum (i1, i2, i3) :\n",
+ " return i1 + i2 + i3\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('Sum is %d\\n' % sum (1, 2, 3))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Sum is 6\n\n"
+ "text": [
+ "Sum is 6\n",
+ "\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +124,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 19.4, Page number: 387"
+ "source": [
+ "Example 19.4, Page number: 387"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 19.4.py\n# To print the full name of a person\n\n\n# Variable declaration\nfirst = 'John'\nlast = 'Doe'\n\n# Calculation and result\nfull = first + ' ' + last\nprint ('The name is %s\\n' % full)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "first = 'John'\n",
+ "last = 'Doe'\n",
+ "\n",
+ "# Calculation and result\n",
+ "full = first + ' ' + last\n",
+ "print ('The name is %s\\n' % full)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The name is John Doe\n\n"
+ "text": [
+ "The name is John Doe\n",
+ "\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +159,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 19.5, Page number: 390"
+ "source": [
+ "Example 19.5, Page number: 390"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 19.5.py\n# To calculate the area of a rectangle\n\n\n# Function declaration\ndef area (width, height) :\n return width * height\n\n# Calculation and result\nsize = area (3.0, 2.0)\nprint ('Area is %f\\n' % size)",
+ "input": [
+ "\n",
+ "# Function declaration\n",
+ "def area (width, height) :\n",
+ " return width * height\n",
+ "\n",
+ "# Calculation and result\n",
+ "size = area (3.0, 2.0)\n",
+ "print ('Area is %f\\n' % size)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Area is 6.000000\n\n"
+ "text": [
+ "Area is 6.000000\n",
+ "\n"
+ ]
}
],
"prompt_number": 5
diff --git a/Practical_C_Programming/Chapter_21_1.ipynb b/Practical_C_Programming/Chapter_21_1.ipynb
index 8d3e6b65..144ac453 100644
--- a/Practical_C_Programming/Chapter_21_1.ipynb
+++ b/Practical_C_Programming/Chapter_21_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 21"
+ "name": "",
+ "signature": "sha256:519d4be291344b242a6ae999f129aea597642f5315e1fe947ec2b9d627dcc018"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,48 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 21: C's dustier corners"
+ "source": [
+ "Chapter 21: C's dustier corners"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 21.1, Page number: 401"
+ "source": [
+ "Example 21.1, Page number: 401"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 21.1.py\n# To check whether the character input by the user is for 'add', 'delete' or 'quit'\n\n\n# Variable declaration\nimport sys\nline = 'a'\n\n# Calculation and result\nif (line == 'a') :\n print ('Add\\n')\nelif (line == 'd') :\n print ('Delete\\n')\nelif (line == 'q') :\n print ('Quit\\n')\n sys.exit(1) \nelse :\n print ('Error:Bad command %c\\n' % line)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "line = 'a'\n",
+ "\n",
+ "# Calculation and result\n",
+ "if (line == 'a') :\n",
+ " print ('Add\\n')\n",
+ "elif (line == 'd') :\n",
+ " print ('Delete\\n')\n",
+ "elif (line == 'q') :\n",
+ " print ('Quit\\n')\n",
+ " sys.exit(1) \n",
+ "else :\n",
+ " print ('Error:Bad command %c\\n' % line)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Add\n\n"
+ "text": [
+ "Add\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
diff --git a/Practical_C_Programming/Chapter_23_1.ipynb b/Practical_C_Programming/Chapter_23_1.ipynb
index 065813d7..bde77ef6 100644
--- a/Practical_C_Programming/Chapter_23_1.ipynb
+++ b/Practical_C_Programming/Chapter_23_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 23"
+ "name": "",
+ "signature": "sha256:456077e76cba45d73549d009e1de5e86e8fe318cfd47c138325bf27f45638790"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,42 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 23: Programming adages"
+ "source": [
+ "Chapter 23: Programming adages"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 23.1, Page number: 447"
+ "source": [
+ "Example 23.1, Page number: 447"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 23.1.py\n# To check whether the number input by the user is 2 or not\n\n\n# Variable declaration\nnumber = 2\n\n# Calculation and result\nif (number != 2) :\n print ('Number is not two\\n')\nelse :\n print ('Number is two\\n')",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "number = 2\n",
+ "\n",
+ "# Calculation and result\n",
+ "if (number != 2) :\n",
+ " print ('Number is not two\\n')\n",
+ "else :\n",
+ " print ('Number is two\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Number is two\n\n"
+ "text": [
+ "Number is two\n",
+ "\n"
+ ]
}
],
"prompt_number": 1
diff --git a/Practical_C_Programming/Chapter_2_3.ipynb b/Practical_C_Programming/Chapter_2_3.ipynb
index f981ce8e..36e059c9 100644
--- a/Practical_C_Programming/Chapter_2_3.ipynb
+++ b/Practical_C_Programming/Chapter_2_3.ipynb
@@ -1,1156 +1,1138 @@
-{
- "metadata": {
- "name": ""
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "Chapter 2:. Convective Mass Transfer"
- ]
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.1,Page number94"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.1\n",
- "#Mass-Transfer Coefficients in a Blood Oxygenator\n",
- "\n",
- "#Variable declaration\n",
- "\n",
- "\t# a-oxygen b-stagnant water\n",
- "T = 310 \t\t\t\t\t# [K]\n",
- "\t# Since the solubility of oxygen in water at 310 K is extremely low, we are dealing with \tdilute solutions\n",
- "k_L = 3.3*10**-5 \t\t\t\t# [coefficient based on the oxygen concentration \t\t\t\t\t\tdifference in the water, m/s]\n",
- "row = 993 \t\t\t\t\t# [kg/cubic m]\n",
- "M_b = 18 \t\t\t\t\t# [gram/mole]\n",
- "\n",
- "\n",
- "#Calculation\n",
- " \n",
- "\t# Since we are dealing with very dilute solutions\n",
- "\t# Therefore, c = (row/M_avg) = row/M_b\n",
- "c = row/M_b \t\t\t\t\t# [kmole/cubic m]\n",
- "\t# Using equation 2.10\n",
- "k_x = k_L*c \t\t\t\t\t# [kmole/square m.s]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"The mass-transfer coefficient based on the mole fraction of oxygen in the liquid is\",round(k_x,5),\" kmole/square m.s\" \n",
- "\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The mass-transfer coefficient based on the mole fraction of oxygen in the liquid is 0.00182 kmole/square m.s\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.2,Page number:95"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.2\n",
- "#Mass-Transfer Coefficient in a Gas Absorber\n",
- "\n",
- "#Variable declaration\n",
- "\n",
- "\t# a-ammonia b-air\n",
- "T = 300 \t\t\t\t\t# [K]\n",
- "P = 1 \t\t\t\t\t\t# [atm]\n",
- "y_a1 = 0.8 \t\t\t\t\t# [ammonia mole fraction in the bulk of the gas \t\t\t\t\t\t\tphase]\n",
- "y_a2 = 0.732 \t\t\t\t\t# [ammonia gas-phase mole fraction on interface]\n",
- "N_a = 4.3*10**-4 \t\t\t\t# [ammonia flux, kmole/square m.s]\n",
- "\n",
- "#Calculations\n",
- "\n",
- "import math\n",
- "\t# Using equation 2.2\n",
- "F_g = N_a/math.log10((1-y_a2)/(1-y_a1)) \t\t# [kmole/square m.s]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"The mass-transfer coefficient in the gas phase at that point where flux is 4.3*10**-4 is\",round(F_g,5),\" kmole/square m.s\"\n",
- "print\"\\n\\nNOTE:Calculation mistake in book:\\nAnswer written as 1.47*10^-4,Actually,,...it is 1.47*10^-3.Please REDO last calculation manually to check\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The mass-transfer coefficient in the gas phase at that point where flux is 4.3*10**-4 is 0.00338 kmole/square m.s\n",
- "\n",
- "\n",
- "NOTE:Calculation mistake in book:\n",
- "Answer written as 1.47*10^-4,Actually,,...it is 1.47*10^-3.Please REDO last calculation manually to check\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.3,Page number:96"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.3\n",
- "#Mass-Transfer Coefficient in a Packed-Bed Distillation Column\n",
- "\n",
- "#Variable declaration\n",
- "\n",
- "\t\t# a-methanol b-water\n",
- "P = 101.3 \t\t\t\t\t\t# [kPa]\n",
- "y_a1 = 0.707 \t\t\t\t\t\t# [mole fraction at interface]\n",
- "y_a2 = 0.656 \t\t\t\t\t\t# [mole fraction at bulk of the gas]\n",
- "k_g = 1.62*10**-5 \t\t\t\t\t# [mass-transfer coefficient at a point \t\t\t\t\t\t\t in the column, kmole/square m.s.kPa]\n",
- "#Calculations\n",
- "\n",
- "\t# Using equation 2.14\n",
- "k_y = k_g*P \t\t\t\t\t\t# [kmole/square m.s]\n",
- "\t# Using equation 2.12\n",
- "N_a = k_y*(y_a1-y_a2) \t\t\t\t\t# [kmole/square m.s]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"The methanol flux at the point of given mass transfer coefficient is\",round(N_a,7),\"kmole/square m.s\" \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The methanol flux at the point of given mass transfer coefficient is 8.37e-05 kmole/square m.s\n"
- ]
- }
- ],
- "prompt_number": 10
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.4,Page number:99"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.4\n",
- "# Mass Transfer into a Dilute Stream Flowing Under Forced Convection in a Circular Conduit\n",
- "\n",
- "n = 6 # [number of variables]\n",
- "\n",
- "#Calculations\n",
- "\n",
- "from scipy.optimize import fsolve\n",
- "from numpy import *\n",
- "import math\n",
- "# To determine the number of dimensionless parameters to be formed, we must know the rank, r, of the dimensional matrix.\n",
- "# The dimensional matrix is \n",
- "DM =matrix([[0,0,1,1,0,0],[1,1,-3,-1,2,1],[-1,-1,0,0,-1,-1]]) \n",
- "rk= linalg.matrix_rank(DM)\n",
- "print\"Rank of matrix is \",rk \n",
- "\n",
- "#The numbers in the table represent the exponent of M, L, and t in the dimensional expression of each of the six variables involved. For example, the dimensional expression of p is M/Lt hence the exponents are 1, -1, and -1\n",
- "\n",
- "# From equation 2.16\n",
- "i = n-rk # [number of dimensional groups]\n",
- "# Let the dimensional groups are pi1, pi2 and pi3\n",
- "# Therefore pi1 = (D_AB)**a*(row)**b*(D)**c*kc\n",
- "# pi2 = (D_AB)**d*(row)**e*(D)**f*v\n",
- "# pi3 = (D_AB)**g*(row)**h*(D)**i*u\n",
- "\n",
- "# Solving for pi1\n",
- "# M**0*L**0*t**0 = 1 = (L**2/t)**a*(M/L**3)**b*(L)**c*(L/t)\n",
- "\n",
- "# Solution of simultaneous equation\n",
- "def F(e):\n",
- " f1 = 2*e[0]-3*e[1]+e[2]+1 \n",
- " f2 = -e[0]-1 \n",
- " f3 = -e[1] \n",
- " return(f1,f2,f3)\n",
- "\n",
- "\n",
- "# Initial guess:\n",
- "e = [0.1,0.8,0.5] \n",
- "y = fsolve(F,e) \n",
- "a = y[0] \n",
- "b = y[1] \n",
- "c = y[2] \n",
- "print\"The coefficients of pi1 are\",a,round(b),c \n",
- "# Similarly the coefficients of pi2 and pi3 are calculated\n",
- "# Therefore we get pi1 = kc*D/D_AB = Sh i.e. Sherwood Number\n",
- "# pi2 = v*D/D_AB = P_ed i.e. Peclet Number\n",
- "# pi3 = u/(row*D_AB) = Sc i.e. Schmidt Number\n",
- "\n",
- "# Dividing pi2 by pi3 gives\n",
- "# pi2/pi3 = D*v*row/u = Re i.e. Renoylds number\n",
- "\n",
- "print\"The result of the dimensional analysis of forced-convection mass transfer in a circular conduit indicates that a correlating relation could be of the form\\n Sh = function(Re,Sc)\\n which is analogous to the heat transfer correlation \\n Nu = function(Re,Pr)\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Rank of matrix is 3\n",
- "The coefficients of pi1 are -1.0 0.0 1.0\n",
- "The result of the dimensional analysis of forced-convection mass transfer in a circular conduit indicates that a correlating relation could be of the form\n",
- " Sh = function(Re,Sc)\n",
- " which is analogous to the heat transfer correlation \n",
- " Nu = function(Re,Pr)\n"
- ]
- }
- ],
- "prompt_number": 11
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.6,Page number:111"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.6\n",
- "#Mass Transfer to Fluid Flow Normal to a Cylinder\n",
- "\n",
- "#Variable declaration\n",
- "\t# a-UF6 b-air\n",
- "M_a = 352 \t\t\t\t\t# [molecular weight of UF6, gram/mole]\n",
- "M_b = 29 \t\t\t\t\t# [gram/mole]\n",
- "d = 0.01 \t\t\t\t\t# [diameter, m]\n",
- "x = 0.1 \t\t\t\t\t# [length exposed to air stream, m]\n",
- "v = 1 \t\t\t\t\t\t# [m/s]\n",
- "Ts = 303 \t\t\t\t\t# [surface temperature of solid, K]\n",
- "P_a = 27 \t\t\t\t\t# [vapor pressure of UF6, kPa]\n",
- "Tb = 325 \t\t\t\t\t# [bulk temperature of solid ,K]\n",
- "P = 101.3 \t\t\t\t\t# [kPa]\n",
- "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
- "import math\n",
- "\n",
- "y_a1 = P_a/P \t\t\t\t\t# [mole fraction at point 1]\n",
- "y_a2 = 0 \t\t\t\t\t# [mole fraction at point 2]\n",
- "\n",
- "\t# Along the mass-transfer path-cylinder surface (point 1) to bulk air (point 2)\n",
- "Tavg = (Ts+Tb)/2 \t\t\t\t# [K]\n",
- "\n",
- "\t# At point 1, the gas is saturated with UF6 vapor, while at point 2 the gas is virtually \tfree of UF6\n",
- "\t# Therefore\n",
- "Pavg = (P_a+0)/2 \t\t\t\t# [average partial pressure, kPa]\n",
- "y_a = Pavg/P \t\t\t\t\t# [mole fraction of UF6]\n",
- "\n",
- "Mavg = M_a*y_a+M_b*(1-y_a) \t\t\t# [gram/mole]\n",
- "row_avg = P*Mavg/(R*Tavg) \t\t\t# [kg/cubic m]\n",
- "\n",
- "\t# Parameter for c-O2, d-N2 and a-UF6\n",
- "yi_c = 0.182 \n",
- "yi_d = 0.685 \n",
- "yi_a = 0.133 \n",
- "Tc_c = 154.6 \t\t\t\t# [K]\n",
- "Tc_d = 126.2 \t\t\t\t\t# [K]\n",
- "Tc_a = 505.8 \t\t\t\t\t# [K]\n",
- "Pc_c = 50.4 \t\t\t\t\t# [bar]\n",
- "Pc_d = 33.9 \t\t\t\t\t# [bar] \n",
- "Pc_a = 46.6 \t\t\t\t\t# [bar]\n",
- "M_c = 32 \t\t\t\t# [gram/mole]\n",
- "M_d = 28 \t\t\t\t\t# [gram/mole] \n",
- "M_a = 352 \t\t\t\t\t# [gram/mole]\n",
- "V_c = 73.4 \t\t\t\t# [cubic cm/mole]\n",
- "V_d = 89.8 \t\t\t\t\t# [cubic cm/mole]\n",
- "V_a = 250 \t\t\t\t\t# [cubic cm/mole]\n",
- "Z_c = 0.288 \n",
- "Z_d = 0.290 \n",
- "Z_a = 0.277 \n",
- "\n",
- "#Calculations\n",
- "\n",
- "\n",
- "\t# From equation 2.52 and 2.53\n",
- "Tcm = yi_c*Tc_c+yi_d*Tc_d+yi_a*Tc_a \t\t# [K]\n",
- "Pcm = 10**6*R*Tcm*(yi_c*Z_c+yi_d*Z_d+yi_a*Z_a)/((yi_c*V_c+yi_d*V_d+yi_a*V_a)*100000) \t# [bar]\n",
- "M_avg = yi_c*M_c+yi_d*M_d+yi_a*M_a \t\t# [gram/mole]\n",
- "\n",
- "\t# From equation 2.50\n",
- "Em = 0.176*(Tcm/(M_avg**3*Pcm**4))**(1.0/6.0) \t# [uP]**-1\n",
- "\n",
- "\t# From equation 2.51\n",
- "Trm = Tavg/Tcm \n",
- "f_Trm = (0.807*Trm**0.618)-(0.357*math.exp(-0.449*Trm))+(0.340*math.exp(-4.058*Trm))+0.018 \n",
- "\t# From equation 2.49 \n",
- "u = f_Trm/Em \t\t\t\t\t\t# [uP]\n",
- "u = u*10**-7 \t\t\t\t\t\t# [viscosity, kg/m.s]\n",
- "\n",
- "Re = d*v*row_avg/u \t\t\t\t\t# [Renoylds number]\n",
- "\n",
- "\t\t# Diffusivity of UF6 vapors in air at 314 K and 1 atm from equation 1.49\n",
- "D_ab = 0.0904 \t\t\t\t\t\t# [square cm/s]\n",
- "\n",
- "Sc = u/(row_avg*D_ab*10**-4) \t\t\t\t# [Schmidt number]\n",
- "\n",
- "Sh_avg = 0.43 + 0.532*Re**0.5*Sc**0.31 \t\t# [Sherwood number]\n",
- "\t\t# From equation 1.7\n",
- "c = P/(R*Tavg) \t\t\t\t\t# [kmole/cubic m]\n",
- "\t\t# From Table 2.1 \n",
- "F_av = Sh_avg*D_ab*c*10**-4/d \t\t\t\t# [kmole/square m.s]\n",
- "\n",
- "\t\t# From equation 2.2\n",
- "N_avg = F_av*math.log((1-y_a2)/(1-y_a1)) \t\t# [kmole/square m.s]\n",
- "S = 2*math.pi*d**2/4 +math.pi*d*x \t\t\t# [total surface area of the cylinder, \t\t\t\t\t\t\tsquare m]\n",
- "\n",
- "w_a = N_avg*S*M_a \t\t\t\t\t# [rate of sublimation of the solid, \t\t\t\t\t\t\tkg/s] \n",
- "\n",
- "#Result\n",
- "print\"Rate of sublimation of a cylinder of UF6 is\",round(w_a,5),\"kg/s\\n\\n\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Rate of sublimation of a cylinder of UF6 is 0.00023 kg/s\n",
- "\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 12
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.7,Page number:116"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.7\n",
- "# The Chilton-Colburn Analogy\n",
- "import math\n",
- "#Variable declaration\n",
- "\n",
- "\t# a-benzene b-nitrogen\n",
- "T = 300 \t\t\t\t\t# [K]\n",
- "P = 101.3 \t\t\t\t\t# [kPa]\n",
- "v =10 \t\t\t\t\t\t# [m/s]\n",
- "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
- "\n",
- "\n",
- "n = -0.5 \n",
- "\t# Data on the properties of C02 at 300 K and 1 bar\n",
- "u = 1.5*10**-5 \t\t\t\t# [viscosity, P]\n",
- "Pr = 0.77 \t\t\t\t\t# [Prandtl number]\n",
- "Cp = 853 \t\t\t\t\t# [J/kg.K]\n",
- "\t# Therefore\n",
- "\t# b = 5.086*l**0.5\n",
- "\t# j_D = j_H = f(Re) = 5.086*(l**0.5)*Re**-0.5\n",
- "\t# From Table 2.1\n",
- "\t# F = j_D*c*v/Sc**(2/3) = 5.086*(l**0.5)*c*v/(Re**0.5*Sc**(2/3)) = \t\t5.086*(row*v*u)**0.5/(Mavg*Sc**(2.0/3.0))\n",
- "\n",
- "#Calculations\n",
- "\n",
- "\t# Vapor pressure of benzene\n",
- "P_a = math.exp(15.9008-(2788.51/(T-52.36))) \t# [mm of Hg]\n",
- "P_a = P_a*101.3/760 \t\t\t\t\t# [kPa]\n",
- "\n",
- "\t# Parameter for a-benzene, b-nitrogen \n",
- "yi_a = 0.07 \n",
- "yi_b = 0.93 \n",
- "Tc_a = 562.2 \n",
- "Tc_b = 126.2 \t\t\t\t\t\t# [K]\n",
- "Pc_a = 48.9 \n",
- "Pc_b = 33.9 \t\t\t\t\t\t# [bar]\n",
- "M_a = 78.1 \n",
- "M_b = 28 \t\t\t\t\t\t# [gram/mole]\n",
- "V_a = 259 \n",
- "V_b = 89.8 \t\t\t\t\t\t# [cubic cm/mole]\n",
- "Z_a = 0.271 \n",
- "Z_b = 0.290 \n",
- "sigma_a = 5.349 \n",
- "sigma_b = 3.798 \t\t\t\t\t# [Angstrom]\n",
- "ek_a = 412.3 \n",
- "ek_b = 71.4 \t\t\t\t\t\t# [E/k, K]\n",
- "\n",
- "\n",
- "\t# From equation 2.52 and 2.53\n",
- "Tcm = yi_b*Tc_b+yi_a*Tc_a \t\t\t\t# [K]\n",
- "Pcm = 10**6*R*Tcm*(yi_b*Z_b+yi_a*Z_a)/((yi_b*V_b+yi_a*V_a)*100000) \t # [bar]\n",
- "M_avg = yi_b*M_b+yi_a*M_a \t\t\t\t\t\t# [kg/kmole]\n",
- "\n",
- "#RESULT\n",
- "\n",
- "print\"Average molecular weight is\",round(M_avg,1),\"kg/kmole\" \n",
- "\n",
- "row = P*M_avg/(R*T) \t\t\t\t\t\t\t# [kg/cubic m]\n",
- "\n",
- "#RESULT\n",
- "\n",
- "print\"Density of mixture is\",round(row,2),\"kg/cubic\"\n",
- "\t# From equation 2.50\n",
- "Em = 0.176*(Tcm/(M_avg**3*Pcm**4))**(1.0/6.0) \t\t\t# [uP]**-1\n",
- "\n",
- "\t# From equation 2.51\n",
- "Trm = T/Tcm \n",
- "f_Trm = (0.807*Trm**0.618)-(0.357*math.exp(-0.449*Trm))+(0.340*math.exp(-4.058*Trm))+0.018 \n",
- "\t# From equation 2.49 \n",
- "u = f_Trm/Em \t\t\t\t\t\t\t\t# [uP]\n",
- "u = u*10**-7 \t\t\t\t\t\t\t\t# [viscosity, kg/m.s]\n",
- "print\"Average viscosity of mixture is \",round(u,7),\"kg/m.s\\n\\n\" \n",
- "\n",
- "\t# Calculating diffusivity of benzene using equation 1.49\n",
- "D_ab = 0.0986 \t\t\t\t\t\t\t\t# [square cm/s]\n",
- "Sc = u/(row*D_ab*10**-4) \t\t\t\t\t\t# [Schmidt number]\n",
- "\n",
- "F = 5.086*(row*v*u)**0.5/(M_avg*Sc**(2.0/3.0)) \t\t\t# [kmole/square m.s]\n",
- "\n",
- "\n",
- "#RESULT\n",
- "\n",
- "print\"The required mass transfer coefficient is\",round(F,5),\"kmole/square m.s\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Average molecular weight is 31.5 kg/kmole\n",
- "Density of mixture is 1.28 kg/cubic\n",
- "Average viscosity of mixture is 1.64e-05 kg/m.s\n",
- "\n",
- "\n",
- "The required mass transfer coefficient is 0.00196 kmole/square m.s\n"
- ]
- }
- ],
- "prompt_number": 13
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.8,Page number:120"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Illustration 2.8\n",
- "#Benzene Evaporation Along a Vertical Flat Plate\n",
- "\n",
- "import math\n",
- "#Variable declaration\n",
- "\t# a-liquid benzene b-nitrogen\n",
- "T = 300 \t\t\t\t\t\t# [K]\n",
- "l = 3 \t\t\t\t\t\t\t# [length of vertical plate, m]\n",
- "b = 1.5 \t\t\t\t\t\t# [width of vertical plate, m]\n",
- "P = 101.3 \t\t\t\t\t\t# [kPa]\n",
- "v = 5 \t\t\t\t\t\t\t# [velocity across the width of plate, \t\t\t\t\t\t\tm/s]\n",
- "row_a = 0.88 \t\t\t\t\t\t# [gram/cubic cm]\n",
- "\n",
- "\n",
- "y_a1 = 0.139 \t\t\t\t\t\t# [mole fraction of benzene at inner \t\t\t\t\t\t\tedge]\n",
- "y_a2 = 0 \n",
- "\n",
- "\t# The film conditions, and average properties, are identical to those in Example 2.7, \tonly the geometry is different\n",
- "\t# Therefore\n",
- "M_avg = 31.4 \t\t\t\t\t\t# [kg/kmole]\n",
- "row = 1.2 \t\t\t\t\t\t# [kg/cubic m]\n",
- "u = 161*10**-7 \t\t\t\t\t# [kg/m.s]\n",
- "D_ab = 0.0986 \t\t \t\t\t\t# [square cm/s]\n",
- "Sc = 1.3 \t\t\t\t\t\t# [Schmidt Number]\n",
- "Re = row*v*b/u \t\t\t\t\t# [Renoylds Number]\n",
- "\n",
- "if Re > 4000:\n",
- " print\"The flow across the plate is turbulent\\n\" \n",
- "elif Re<2000:\n",
- " print\"The flow across the plate is laminar\\n\" \n",
- "\t#Using equation 2.57\n",
- "Sh_l = 0.036*Re**0.8*Sc**(1.0/3.0) \n",
- "\n",
- "\t# Nitrogen (component B) does not react with benzene (component A), neither dissolves in \t\tthe liquid therefore, NB = 0 and siA = 1. The F-form of the mass-transfer coefficient \t\t\tshould be used \n",
- "F = Sh_l*1.26*D_ab*10**-4/(M_avg*b) \t\t\t# [kmole/square m.s]\n",
- "N_a = F*math.log((1-y_a2)/(1-y_a1)) \t\t\t# [kmole/square m.s]\n",
- "\n",
- "\t# The total mass rate of evaporation over the surface of the plate\n",
- "S = 1.5*3 \t\t\t\t\t\t# [square m]\n",
- "M_a = 78.1 \t\t\t\t\t\t# [gram/mole]\n",
- "wa = N_a*S*M_a*60*1000 \t\t\t\t# [gram/min]\n",
- "\n",
- "V = wa/row_a \t\t\t\t\t\t# [volumetric flow rate, ml/min]\n",
- "\n",
- "print\"Liquid benzene should be supplied at the top of the plate at the rate \",round(V),\"ml/min\\nso that evaporation will just prevent it from reaching the bottom of the plate.\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The flow across the plate is turbulent\n",
- "\n",
- "Liquid benzene should be supplied at the top of the plate at the rate 1473.0 ml/min\n",
- "so that evaporation will just prevent it from reaching the bottom of the plate.\n"
- ]
- }
- ],
- "prompt_number": 14
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.9,Page number:123"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Illustration 2.9\n",
- "#Evaporation of a Drop of Water Falling in Air\n",
- "\n",
- "#Variable declaration\n",
- "\t# a-water b-air\n",
- "dp1 = 10**-3 \t\t\t\t\t# [diameter of spherical drop of water, m]\n",
- "Tair = 323 \t\t\t\t\t# [K]\n",
- "P = 101.3 \t\t\t\t\t# [kPa]\n",
- "Twater = 293 \t\t\t\t\t# [K]\n",
- "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
- "M_a = 18.0 \t\t\t\t\t# [gram/mole]\n",
- "M_b = 29.0 \t\t\t\t\t# [gram/mole]\n",
- "import math\n",
- "\n",
- "\n",
- "#Calculation\n",
- "\n",
- "dp2 = (1.0/2.0)**(1.0/3.0)*dp1 \t\t# [m]\n",
- "dp = (dp1+dp2)/2 \t\t\t\t# [m]\n",
- "\n",
- "row_p = 995 \t\t\t\t\t# [density of water, kg/cubic m]\n",
- "row1b = 1.094 \t\t\t\t\t# [density of air, kg/cubic m]\n",
- "u = 1.95*10**-5 \t\t\t\t# [kg/m.s]\n",
- "row_pr = row_p-row1b \t\t\t\t# [kg/cubic m]\n",
- "g = 9.8 \t\t\t\t\t# [accleration due to gravity, square m/s]\n",
- "\t# Combining equation 2.68 and 2.69\n",
- "Ga = 4*dp**3*row1b*row_pr*g/(3*u**2) \t\t# [Galileo Number]\n",
- "\n",
- "\t# Relationship between Re and Cd\n",
- "\t# Re/Cd = Re**3/Ga = 3*row**2*vt**3/(4*g*u*row_pr)\n",
- "\n",
- "\t# The following correlation is used to relate Re/Cd, to Ga\n",
- "\t# ln(Re/Cd)**(1/3) = -3.194 + 2.153*ln(Ga)**(1/3) - 0.238*(ln(Ga)**(1/3))**2 + \t0.01068*(ln(Ga)**(1/3))**3\n",
- "\t# Therefore let A = (Re/Cd)\n",
- "A = math.exp(-3.194 + 2.153*math.log(Ga**(1.0/3.0)) - 0.238*(math.log(Ga**(1.0/3.0)))**2 + 0.01068*(math.log(Ga**(1.0/3.0)))**3) \n",
- "\n",
- "\t# Therefore 'vt' will be\n",
- "vt = A*(4*g*row_pr*u/(3*row1b**2))**(1.0/3.0) \t# [Terminal velocity of particle, m/s]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"Terminal velocity of particle is\",round(vt,2),\"m/s\" \n",
- "\n",
- "\n",
- "#Calculation\n",
- "\n",
- "P_w = 2.34 \t\t\t\t\t# [vapor pressure of water, kPa]\n",
- "y_w = P_w/P \t\t\t\t\t# [mole fraction of water at the inner edge of \t\t\t\t\t\tthe gas film]\n",
- "M_avg = 18*y_w+29*(1-y_w) \t\t\t# [gram/mole]\n",
- "\n",
- "row2b = P*M_avg/(R*Twater) \t\t\t# [kg/cubic.m]\n",
- "delta_row = row2b - row1b \t\t\t# [kg/cubic.m]\n",
- "\n",
- "Tavg = (Tair+Twater)/2 \t\t\t# [K]\n",
- "\t\t# At Temperature equal to Tavg density and viscosity are\n",
- "row3 = 1.14 \t\t\t\t\t# [kg/cubic.m]\n",
- "u1 = 1.92*10**-5 \t\t\t\t# [kg/m.s]\n",
- "\n",
- "Grd = g*row3*delta_row*(dp**3)/(u1**2) \n",
- "\n",
- "\t\t# Diffusivity of water at Tavg and 1 atm is\n",
- "D_ab = 0.242*10**-4 \t\t\t\t# [square m/s]\n",
- "Sc = u1/(row3*D_ab) \t\t\t\t# [Schmidt Number]\n",
- "Re = dp*row3*vt/u1 \t\t\t\t# [Renoylds Number]\n",
- "\t\n",
- "\t# From equation 2.65 Re is greater than 0.4*Grd**0.5*Sc**(-1/6)\n",
- "\t# Therfore equation 2.64 can be used to calculate mass transfer coefficient\n",
- "\n",
- "Sh = 2+0.552*(Re**0.5)*Sc**(1.0/3.0) \t\t# [Sherwood Number]\n",
- "\t# From Table 2.1\n",
- "\t# Sh = kc*P_bm*dp/(P*D_ab), since P_bm is almost equal to P\n",
- "\t# Therefore \n",
- "\t# Sh = kc*dp/D_ab \n",
- "kc = Sh*D_ab/dp \t\t\t\t# [m/s]\n",
- "\n",
- "ca2 = 0 \t\t\t\t\t# [dry air concentration]\n",
- "ca1 = P_w/(R*Twater) \t\t\t\t# [interface concentration, kmole/cubic.m]\n",
- "\t# Average rate of evaporation \n",
- "wa = math.pi*dp**2*M_a*kc*(ca1-ca2)*1000 \t# [g/s]\n",
- "\n",
- "\t# Amount of water evaporated\n",
- "m = row_p*math.pi*dp1**3/12*1000 \t\t# [g]\n",
- "\t# Time necessary to reduce the volume by 50%\n",
- "t = m/wa \t\t\t\t\t# [s]\n",
- "\n",
- "D = t*vt \t\t\t\t\t# [distance of fall, m]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"The distance of fall is\",round(D),\"m\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Terminal velocity of particle is 3.59 m/s\n",
- "The distance of fall is 90.0 m\n"
- ]
- }
- ],
- "prompt_number": 15
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.10,Page number:127"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Illustration 2.10\n",
- "#Mass Transfer for a Single Cylinder\n",
- "\n",
- "#Variable declaratiopn\n",
- "\n",
- "\t# Example 2.6 using equation 2.73\n",
- "\t# Values of the dimensionless parameters calculated in Example 2.6\n",
- "Re = 1223 \t\t\t\t# [Renoylds Number]\n",
- "Sc = 0.905 \t\t\t\t# [Schmidt Number]\n",
- "c = 0.039 \t\t\t\t# [molar density, kg/cubic m]\n",
- "v = 1 \t\t\t\t\t# [gas velocity, m/s]\n",
- "\t# Therefore \n",
- "#Calculations\n",
- "\n",
- "Gm = c*v \t\t\t\t# [kmole/square m.s]\n",
- "\t# From equation 2.9 \n",
- "\t# Kg*P = ky\n",
- "\t# Therefore substituting in equation 2.73 we obtain\n",
- "ky = 0.281*Gm/(Re**0.4*Sc**0.56) \t# [kmole/square m.s]\n",
- "\t# Now equation 2.73 were obtained under very dilute concentrations\n",
- "\t# Therefore\n",
- "y_bm = 1 \n",
- "\t# From equation 2.6\n",
- "F = ky*y_bm \t\t\t\t# [kmole/square m.s]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"Mass transfer coefficient is \",round(F,6),\"kmol/m.^2-s\" \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Mass transfer coefficient is 0.000675 kmol/m.^2-s\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.11,Page number:129"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.11\n",
- "#Simultaneous Heat and Mass Transfer in Pipe\n",
- "\n",
- "#Variable declaration\n",
- "\n",
- "# a-water b-air\n",
- "D = 25.4*10**-3 \t\t\t\t# [diameter of wetted wall tower, m]\n",
- "Gy = 10 \t\t\t\t\t# [mass velocity, kg/square m.s]\n",
- "T1 = 308 \t\t\t\t\t# [K]\n",
- "P = 101.3 \t\t\t\t\t# [kPa]\n",
- "p_a1 = 1.95 \t\t\t\t\t# [partial pressure of water vapor, kPa]\n",
- "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
- "M_a = 18 \t\t\t\t\t# [gram/mole]\n",
- "Cpa = 1.88 \t\t\t\t\t# [kJ/kg.K]\n",
- "\n",
- "\n",
- "# Properties of dry air at 308 K and 1 atm pressure are\n",
- "u = 1.92*10**-5 \t\t\t\t# [kg/m.s]\n",
- "row = 1.14 \t\t\t\t\t# [kg/cubic m]\n",
- "D_ab = 0.242*10**-4 \t\t\t\t# [square m/s]\n",
- "Sc = 0.696 \t\t\t\t\t# [Schmidt number]\n",
- "Cp = 1.007 \t\t\t\t\t# [kJ/kg.K]\n",
- "k = 0.027 \t\t\t\t\t# [W/m.K]\n",
- "Pr = 0.7 \t\t\t\t\t# [Prandtl number]\n",
- "\n",
- "\n",
- "#Calculations\n",
- "\n",
- "import math\n",
- "from scipy.optimize import fsolve\n",
- "from numpy import *\n",
- "\n",
- "Re = D*Gy/u \t\t\t\t\t# [Renoylds number]\n",
- "# From equation 2,74\n",
- "Sh = 0.023*Re**0.83*Sc**0.44 \t\t\t#[Sherwood number]\n",
- "# From Table 2.1\n",
- "kg = Sh*D_ab/(R*T1*D)*1000 \t\t\t# [mole/square m.s.kPa]\n",
- "\n",
- "# To estimate the heat-transfer coefficient, we use the Dittus-Boelter equation for cooling, equation 2.80\n",
- "Nu = 0.023*Re**0.8*Pr**0.3 \t\t\t# [Nusselt number]\n",
- "# From Table 2.1\n",
- "h = Nu*k/D \t\t\t\t\t# [W/square m.K]\n",
- "\n",
- "T =373.15 \t\t\t\t\t# [K]\n",
- "lambda_a = 40.63 \t\t\t\t# [kJ/mole]\n",
- "Tc = 647.1 \t\t\t\t\t# [K]\n",
- "\n",
- "# Solution of simultaneous equation 2.78 and 2.79\n",
- "def F(e): \n",
- " f1=kg*(p_a1 - math.exp(16.3872-(3885.7/(e[0]-42.98))))-e[1] \n",
- " f2=e[1]*M_a*Cpa*(T1-e[0])/(1-exp(-e[1]*M_a*Cpa/h)) + 1000*e[1]*lambda_a*((1-(e[0]/Tc))/(1-(T/Tc)))**0.38 \n",
- " return(f1,f2) \n",
- "\n",
- "\n",
- "# Initial guess\n",
- "e = [292,-0.003] \n",
- "y = fsolve(F,e) \n",
- "Ti = y[0] \n",
- "Na = y[1] \n",
- "\n",
- "print\"The temperature of the liquid water and the rate of water evaporation is\",round(Ti),\"K and\",round(Na,3),\" mole/square m.s respectively\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "The temperature of the liquid water and the rate of water evaporation is 295.0 K and -0.013 mole/square m.s respectively\n"
- ]
- }
- ],
- "prompt_number": 17
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.12,Page number:131"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.12\n",
- "#Air Humidification in Wetted- Wall Column \n",
- "\n",
- "\n",
- "#Variable declaration\n",
- "\t# a-water b-dry air\n",
- "D = 25.4*10**-3 \t\t\t# [Internal diameter of tower, m]\n",
- "Z = 1.5 \t\t\t\t# [length of the wetted section, m]\n",
- "Gy = 10 \t\t\t\t# [mass velocity of air, kg/square m.s]\n",
- "Tair = 308 \t\t\t\t# [K]\n",
- "Twater = 295 \t\t\t\t# [K]\n",
- "P = 101.3 \t\t\t\t# [kPa]\n",
- "M_a = 18.0 \t\t\t\t# [gram/mole]\n",
- "M_b = 29.0 \t\t\t\t# [gram/mole]\n",
- "R = 8.314 \t\t\t\t# [cubic m.Pa/mole.K]\n",
- "\n",
- "#Calculations\n",
- "\n",
- "import math\n",
- "\n",
- "Pa = 2.64 # [kPa]\n",
- "\n",
- "Gm = Gy/M_b \t# [Assuming that gas phase is basically dry air, kmole/square m.s]\n",
- "\t\t# The properties of dry air at 308 K and 1 atm are (from example 2.9)\n",
- "row = 1.14 \t\t\t\t# [kg/cubic m]\n",
- "u = 1.92*10**-5 \t\t\t# [kg/m.s]\n",
- "D_ab = 0.242*10**-4 \t\t\t# [square m/s]\n",
- "Sc = 0.692 \t\t\t\t# [Schmidt number]\n",
- "\n",
- "Re = Gy*D/u \t\t\t\t# [Renoylds number]\n",
- "\n",
- "if Re<35000 and Re>2000:\n",
- " Sh = 0.023*Re**0.83*Sc**0.44 \t# [Sherwood number] \n",
- " print\"Sherwood number is\",round(Sh,1) \n",
- "else:\n",
- " print\"We cannot use equation 2.74\"\n",
- "\n",
- "c = P/(R*Tair) \t\t\t# [kmole/cubic m]\n",
- "\t# Now using equation 2.89\n",
- "Pa_out = Pa*(1-math.exp((-4*Sh*Z*c*D_ab)/(Gm*D**2))) \t\t# [kPa]\n",
- "\n",
- "#Result\n",
- "print\"The partial pressure of water in the air leaving the tower is\",round(Pa_out,2),\"kPa\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Sherwood number is 51.6\n",
- "The partial pressure of water in the air leaving the tower is 1.94 kPa\n"
- ]
- }
- ],
- "prompt_number": 18
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.13,Page number:134"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Illustration 2.13\n",
- "# Air Humidification in a Packed Bed\n",
- "\n",
- "#Variable declaration\n",
- "\t# a-water b-dry air\n",
- "Gy = 10.0 \t\t\t\t# [kg/square m.s]\n",
- "dp = 3.5*10**-3 \t\t\t# [diameter of spherical glass beads, m]\n",
- "D = 25.4*10**-3 \t\t\t# [Internal diameter of tower, m]\n",
- "Tair = 308 \t\t\t\t# [K]\n",
- "Twater = 295 \t\t\t\t# [K]\n",
- "P = 101.3 \t\t\t\t# [kPa]\n",
- "M_a = 18 \t\t\t\t# [gram/mole]\n",
- "M_b = 29 \t\t\t\t# [gram/mole]\n",
- "R = 8.314 \t\t\t\t# [cubic m.Pa/mole.K]\n",
- "\n",
- "#Calculation\n",
- "\n",
- "import math\n",
- "from scipy.optimize import fsolve\n",
- "\t# The properties of dry air at 308 K and 1 atm are (from example 2.12)\n",
- "row = 1.14 \t\t\t\t# [kg/cubic m]\n",
- "u = 1.92*10**-5 \t\t\t# [kg/m.s]\n",
- "D_ab = 0.242*10**-4 \t\t\t# [square m/s]\n",
- "Sc = 0.692 \t\t\t\t# [Schmidt number]\n",
- "c = 0.04 \t \t\t\t# [mole/cubic m]\n",
- "Gm = 0.345 \t\t\t\t# [kmole/square m.s]\n",
- "\n",
- "Re = Gy*dp/u \t\t\t\t# [Renoylds number]\n",
- "if Re<2500 and Re>10:\n",
- " \t\t\t\t\t# Subsituting in equation 2.90\n",
- " jd = 1.17*Re**-0.415 \n",
- " print\"Renoylds number is \",Re\n",
- "else:\n",
- " print \" \"\n",
- "Std = 0.052/(Sc**(2.0/3.0)) \n",
- "\t\t# From Table 2.1 \n",
- "Sh = Std*Re*Sc \t\t\t# [Sherwood number]\n",
- "\t\t# From equation 2.94\n",
- "e = 0.406+0.571*(dp/D) \t\t# [bed porosity]\n",
- "e=round(e,3)\n",
- "\t#Illustration 2.13(a) \n",
- "\t# Solution(a)\n",
- "\t# Now Paout = 0.99*Pa\n",
- "\t# Using equation 2.93 to calculate 'Z'\n",
- "def f12(Z):\n",
- " return(0.99 - 1 + math.exp(-6*(1-e)*Sh*c*Z*D_ab/(Gm*dp**2))) \n",
- "Z = fsolve(f12,0.06) \n",
- "\n",
- "#Result\n",
- "Z=round(Z[0],3)\n",
- "print\"The depth of packing required is\",Z,\"m=\",Z*100,\"cm\" \n",
- "\n",
- "\t#Illustration 2.13(b)\n",
- "\t# Solution(b)\n",
- "\t# From equation 2.95\n",
- "deltaP = (150*(1-e)/Re + 1.75)*((1-e)*(Gy**2)*Z)/(dp*row*e**3) \t# [Pa]\n",
- "\n",
- "#Result\n",
- "print\"The gas pressure drop through the bed is\",round(deltaP),\"Pa (Approx) \\nDUE TO LACK OF PRECISION IN CALCULATION IN BOOK.\\niF DONE MANUALLY,THIS ANSWER STANDS CORRECT\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Renoylds number is 1822.91666667\n",
- "The depth of packing required is 0.078 m= 7.8 cm\n",
- "The gas pressure drop through the bed is 15817.0 Pa (Approx) \n",
- "DUE TO LACK OF PRECISION IN CALCULATION IN BOOK.\n",
- "iF DONE MANUALLY,THIS ANSWER STANDS CORRECT\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.14,Page number:138"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Illustration 2.14\n",
- "#Design of a Hollow-Fiber Boiler Feed Water Deaerator\n",
- "\n",
- "#Variable declaration\n",
- "\t# a-oxygen b-water\n",
- "\t# To design the deaerator, We will use commercially available microporous polypropylene \thollow fibers in a module\n",
- "\n",
- "m = 40000.0 \t\t\t\t\t# [kg/hr]\n",
- "Twater = 298 \t\t\t\t\t# [K]\n",
- "v = 0.1 \t\t\t\t\t# [superficial velocity, m/s]\n",
- "P = 101.3 \t\t\t\t\t# [kPa]\n",
- "V = 40*10**-3 \t\t\t\t\t# [Flow rate of nitrogen, cubic m/min]\n",
- "d = 2.90*10**-4 \t\t\t\t# [Outside diameter of fibres, m]\n",
- "pf = 0.4 \t\t\t\t\t# [Packing factor]\n",
- "a = 46.84*100 \t\t\t\t\t# [surface area per unit volume, m**-1]\n",
- "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
- "\n",
- "#Calculation\n",
- "\n",
- "import math\n",
- "\n",
- "dw = 1000 \t\t\t\t\t# [density of water, kg/cubic m]\n",
- "Ql = m/(3600*1000) \t\t\t\t# [volumetric water flow rate, cubic m/s]\n",
- "\t# Shell diameter\n",
- "D = (4*Ql/(math.pi*v))**0.5 \t\t\t# [Shell diameter, m]\n",
- "\n",
- "\t# the properties of dilute mixtures of oxygen in water at 298 K\n",
- "u = 0.9 \t\t\t\t\t# [cP]\n",
- "\t# Diffusivity from equation 1.53\n",
- "D_ab = 1.93*10**-9 \t\t\t\t# [square m/s]\n",
- "Sc = 467 \t\t\t\t\t# [Schmidt number]\n",
- "\n",
- "Re = d*v*dw/(u*10**-3) \t\t\t# [Renoylds number]\n",
- "\n",
- "\t# Substituting in equation (2-97) gives\n",
- "Sh = 0.53*(1-1.1*pf)*((1-pf)/pf)**-0.47*(Re**0.53*Sc**0.33) \n",
- "\n",
- "kl = Sh*D_ab/d \t\t\t\t# [mass-transfer coefficient on the shell side, \t\t\t\t\t\tm/s]\n",
- "\n",
- "\t# From the specified BFW flow rate\n",
- "L = m/(3600*18) \t\t\t\t# [kmole/s]\n",
- "\t# From ideal gas law\n",
- "V1 = V*P/(Twater*R*60) \t\t\t# [kmole/s]\n",
- "\t# From the solubility of oxygen in water at 298 K,\n",
- "M = 4.5*10**4 \n",
- "A = L/(M*V1) \t\t\t\t\t# [Absorption factor]\n",
- "\n",
- "#Result\n",
- "\n",
- "print\"Absorption factor is\",round(A,3) \n",
- "\n",
- "#Calculation\n",
- "\n",
- "\t# For 99% removal of the dissolved oxygen\n",
- "\t# x_in/x_out = b = 100\n",
- "b = 100 \n",
- "c = 55.5 \t\t\t\t\t# [molar density, kmole/cubic m]\n",
- "\t# Substituting in equation 2.99 yields\n",
- "V_T = (L*math.log(b*(1-A)+A))/(kl*a*c*(1-A)) \t # [cubic m]\n",
- "\n",
- "\t# The module length, Z is\n",
- "Z = V_T/(math.pi*D**2.0/4.0) \n",
- "\n",
- "#Result\n",
- "print\"The shell diameter and module length is\",round(D,3),\"m and\",round(Z,2),\" m respectively\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Absorption factor is 0.503\n",
- "The shell diameter and module length is 0.376 m and 2.15 m respectively\n"
- ]
- }
- ],
- "prompt_number": 20
- },
- {
- "cell_type": "heading",
- "level": 3,
- "metadata": {},
- "source": [
- "Example 2.15,Page number:140"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "#Example 2.15\n",
- "\n",
- "#Variable declaration\n",
- "d=2.21/100\t\t\t#[m]\n",
- "mu=8.82*10**-6\t\t#[kg/m-s]\n",
- "rho=2.81\t\t#[kg/m**3]\n",
- "\n",
- "c=34.14\t\t\t#[mol/m**3]\n",
- "D=array([2.228/(10**6),2.065/(10**6),1.832/(10**6)]) #Velocities in [m**2/s]\n",
- "\n",
- "\n",
- "#Calculation\n",
- "#Gy=rho*v\t\t\n",
- "#Re=Gy*d/mu\t\t#Reynolds number\n",
- "Re=21750\n",
- "print \"Reynolds number=\",Re\n",
- "Sc=[]\n",
- "Sh=[]\n",
- "F=[]\n",
- "for i in range(0, 3):\n",
- " sc=mu/(rho*D[i]) #Schmidt number\n",
- " Sc.append(sc)\n",
- " sh=0.023*Re**(0.83)*sc**(0.44) #Sherwood number\n",
- " Sh.append(sh)\n",
- " f=sh*c*D[i]/d #Binary mass transfer coefficient in [mol/m^2-s]\n",
- " F.append(f)\n",
- "print \"Schmidt number are:\"\n",
- "for i in range(0,3):\n",
- " print round(Sc[i],3)\n",
- "print \"Sherwood number number are:\"\n",
- "for i in range(0,3):\n",
- " print round(Sh[i],1)\n",
- "print\"Binary mass transfer coefficients are:\"\n",
- "for i in range(0,3):\n",
- " print round(F[i],3)\n",
- "#After modifying mathcad program of example 1.17,we have\n",
- "N1=-0.0527 #[mol/m^2-s]\n",
- "N2=0.0395 #[mol/m^2-s]\n",
- "N3=0.0132 #[mol/m^2-s]\n",
- "print\"The program yields the following results:\"\n",
- "print \"N1=\",N1,\"mol/m**2-s\"\n",
- "print \"N2=\",N2,\"mol/m**2-s\"\n",
- "print \"N3=\",N3,\"mol/m**2-s\"\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Reynolds number= 21750\n",
- "Schmidt number are:\n",
- "1.409\n",
- "1.52\n",
- "1.713\n",
- "Sherwood number number are:\n",
- "106.5\n",
- "110.1\n",
- "116.1\n",
- "Binary mass transfer coefficients are:\n",
- "0.367\n",
- "0.351\n",
- "0.328\n",
- "The program yields the following results:\n",
- "N1= -0.0527 mol/m**2-s\n",
- "N2= 0.0395 mol/m**2-s\n",
- "N3= 0.0132 mol/m**2-s\n"
- ]
- }
- ],
- "prompt_number": 36
- }
- ],
- "metadata": {}
- }
- ]
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1bdbe534af4c41744d0b20620d61031047f924bf7d6e0c91a540c9ad26997cbe"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 2:. Convective Mass Transfer"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.1,Page number94"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\t# a-oxygen b-stagnant water\n",
+ "T = 310 \t\t\t\t\t# [K]\n",
+ "\t# Since the solubility of oxygen in water at 310 K is extremely low, we are dealing with \tdilute solutions\n",
+ "k_L = 3.3*10**-5 \t\t\t\t# [coefficient based on the oxygen concentration \t\t\t\t\t\tdifference in the water, m/s]\n",
+ "row = 993 \t\t\t\t\t# [kg/cubic m]\n",
+ "M_b = 18 \t\t\t\t\t# [gram/mole]\n",
+ "\n",
+ "\n",
+ "#Calculation\n",
+ " \n",
+ "\t# Since we are dealing with very dilute solutions\n",
+ "\t# Therefore, c = (row/M_avg) = row/M_b\n",
+ "c = row/M_b \t\t\t\t\t# [kmole/cubic m]\n",
+ "\t# Using equation 2.10\n",
+ "k_x = k_L*c \t\t\t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"The mass-transfer coefficient based on the mole fraction of oxygen in the liquid is\",round(k_x,5),\" kmole/square m.s\" \n",
+ "\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The mass-transfer coefficient based on the mole fraction of oxygen in the liquid is 0.00182 kmole/square m.s\n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.2,Page number:95"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "\n",
+ "\t# a-ammonia b-air\n",
+ "T = 300 \t\t\t\t\t# [K]\n",
+ "P = 1 \t\t\t\t\t\t# [atm]\n",
+ "y_a1 = 0.8 \t\t\t\t\t# [ammonia mole fraction in the bulk of the gas \t\t\t\t\t\t\tphase]\n",
+ "y_a2 = 0.732 \t\t\t\t\t# [ammonia gas-phase mole fraction on interface]\n",
+ "N_a = 4.3*10**-4 \t\t\t\t# [ammonia flux, kmole/square m.s]\n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "import math\n",
+ "\t# Using equation 2.2\n",
+ "F_g = N_a/math.log10((1-y_a2)/(1-y_a1)) \t\t# [kmole/square m.s]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"The mass-transfer coefficient in the gas phase at that point where flux is 4.3*10**-4 is\",round(F_g,5),\" kmole/square m.s\"\n",
+ "print\"\\n\\nNOTE:Calculation mistake in book:\\nAnswer written as 1.47*10^-4,Actually,,...it is 1.47*10^-3.Please REDO last calculation manually to check\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The mass-transfer coefficient in the gas phase at that point where flux is 4.3*10**-4 is 0.00338 kmole/square m.s\n",
+ "\n",
+ "\n",
+ "NOTE:Calculation mistake in book:\n",
+ "Answer written as 1.47*10^-4,Actually,,...it is 1.47*10^-3.Please REDO last calculation manually to check\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.3,Page number:96"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "\n",
+ "\t\t# a-methanol b-water\n",
+ "P = 101.3 \t\t\t\t\t\t# [kPa]\n",
+ "y_a1 = 0.707 \t\t\t\t\t\t# [mole fraction at interface]\n",
+ "y_a2 = 0.656 \t\t\t\t\t\t# [mole fraction at bulk of the gas]\n",
+ "k_g = 1.62*10**-5 \t\t\t\t\t# [mass-transfer coefficient at a point \t\t\t\t\t\t\t in the column, kmole/square m.s.kPa]\n",
+ "#Calculations\n",
+ "\n",
+ "\t# Using equation 2.14\n",
+ "k_y = k_g*P \t\t\t\t\t\t# [kmole/square m.s]\n",
+ "\t# Using equation 2.12\n",
+ "N_a = k_y*(y_a1-y_a2) \t\t\t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"The methanol flux at the point of given mass transfer coefficient is\",round(N_a,7),\"kmole/square m.s\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The methanol flux at the point of given mass transfer coefficient is 8.37e-05 kmole/square m.s\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.4,Page number:99"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "n = 6 # [number of variables]\n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "from scipy.optimize import fsolve\n",
+ "from numpy import *\n",
+ "import math\n",
+ "# To determine the number of dimensionless parameters to be formed, we must know the rank, r, of the dimensional matrix.\n",
+ "# The dimensional matrix is \n",
+ "DM =matrix([[0,0,1,1,0,0],[1,1,-3,-1,2,1],[-1,-1,0,0,-1,-1]]) \n",
+ "rk= linalg.matrix_rank(DM)\n",
+ "print\"Rank of matrix is \",rk \n",
+ "\n",
+ "#The numbers in the table represent the exponent of M, L, and t in the dimensional expression of each of the six variables involved. For example, the dimensional expression of p is M/Lt hence the exponents are 1, -1, and -1\n",
+ "\n",
+ "# From equation 2.16\n",
+ "i = n-rk # [number of dimensional groups]\n",
+ "# Let the dimensional groups are pi1, pi2 and pi3\n",
+ "# Therefore pi1 = (D_AB)**a*(row)**b*(D)**c*kc\n",
+ "# pi2 = (D_AB)**d*(row)**e*(D)**f*v\n",
+ "# pi3 = (D_AB)**g*(row)**h*(D)**i*u\n",
+ "\n",
+ "# Solving for pi1\n",
+ "# M**0*L**0*t**0 = 1 = (L**2/t)**a*(M/L**3)**b*(L)**c*(L/t)\n",
+ "\n",
+ "# Solution of simultaneous equation\n",
+ "def F(e):\n",
+ " f1 = 2*e[0]-3*e[1]+e[2]+1 \n",
+ " f2 = -e[0]-1 \n",
+ " f3 = -e[1] \n",
+ " return(f1,f2,f3)\n",
+ "\n",
+ "\n",
+ "# Initial guess:\n",
+ "e = [0.1,0.8,0.5] \n",
+ "y = fsolve(F,e) \n",
+ "a = y[0] \n",
+ "b = y[1] \n",
+ "c = y[2] \n",
+ "print\"The coefficients of pi1 are\",a,round(b),c \n",
+ "# Similarly the coefficients of pi2 and pi3 are calculated\n",
+ "# Therefore we get pi1 = kc*D/D_AB = Sh i.e. Sherwood Number\n",
+ "# pi2 = v*D/D_AB = P_ed i.e. Peclet Number\n",
+ "# pi3 = u/(row*D_AB) = Sc i.e. Schmidt Number\n",
+ "\n",
+ "# Dividing pi2 by pi3 gives\n",
+ "# pi2/pi3 = D*v*row/u = Re i.e. Renoylds number\n",
+ "\n",
+ "print\"The result of the dimensional analysis of forced-convection mass transfer in a circular conduit indicates that a correlating relation could be of the form\\n Sh = function(Re,Sc)\\n which is analogous to the heat transfer correlation \\n Nu = function(Re,Pr)\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Rank of matrix is 3\n",
+ "The coefficients of pi1 are -1.0 0.0 1.0\n",
+ "The result of the dimensional analysis of forced-convection mass transfer in a circular conduit indicates that a correlating relation could be of the form\n",
+ " Sh = function(Re,Sc)\n",
+ " which is analogous to the heat transfer correlation \n",
+ " Nu = function(Re,Pr)\n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.6,Page number:111"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "\t# a-UF6 b-air\n",
+ "M_a = 352 \t\t\t\t\t# [molecular weight of UF6, gram/mole]\n",
+ "M_b = 29 \t\t\t\t\t# [gram/mole]\n",
+ "d = 0.01 \t\t\t\t\t# [diameter, m]\n",
+ "x = 0.1 \t\t\t\t\t# [length exposed to air stream, m]\n",
+ "v = 1 \t\t\t\t\t\t# [m/s]\n",
+ "Ts = 303 \t\t\t\t\t# [surface temperature of solid, K]\n",
+ "P_a = 27 \t\t\t\t\t# [vapor pressure of UF6, kPa]\n",
+ "Tb = 325 \t\t\t\t\t# [bulk temperature of solid ,K]\n",
+ "P = 101.3 \t\t\t\t\t# [kPa]\n",
+ "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "import math\n",
+ "\n",
+ "y_a1 = P_a/P \t\t\t\t\t# [mole fraction at point 1]\n",
+ "y_a2 = 0 \t\t\t\t\t# [mole fraction at point 2]\n",
+ "\n",
+ "\t# Along the mass-transfer path-cylinder surface (point 1) to bulk air (point 2)\n",
+ "Tavg = (Ts+Tb)/2 \t\t\t\t# [K]\n",
+ "\n",
+ "\t# At point 1, the gas is saturated with UF6 vapor, while at point 2 the gas is virtually \tfree of UF6\n",
+ "\t# Therefore\n",
+ "Pavg = (P_a+0)/2 \t\t\t\t# [average partial pressure, kPa]\n",
+ "y_a = Pavg/P \t\t\t\t\t# [mole fraction of UF6]\n",
+ "\n",
+ "Mavg = M_a*y_a+M_b*(1-y_a) \t\t\t# [gram/mole]\n",
+ "row_avg = P*Mavg/(R*Tavg) \t\t\t# [kg/cubic m]\n",
+ "\n",
+ "\t# Parameter for c-O2, d-N2 and a-UF6\n",
+ "yi_c = 0.182 \n",
+ "yi_d = 0.685 \n",
+ "yi_a = 0.133 \n",
+ "Tc_c = 154.6 \t\t\t\t# [K]\n",
+ "Tc_d = 126.2 \t\t\t\t\t# [K]\n",
+ "Tc_a = 505.8 \t\t\t\t\t# [K]\n",
+ "Pc_c = 50.4 \t\t\t\t\t# [bar]\n",
+ "Pc_d = 33.9 \t\t\t\t\t# [bar] \n",
+ "Pc_a = 46.6 \t\t\t\t\t# [bar]\n",
+ "M_c = 32 \t\t\t\t# [gram/mole]\n",
+ "M_d = 28 \t\t\t\t\t# [gram/mole] \n",
+ "M_a = 352 \t\t\t\t\t# [gram/mole]\n",
+ "V_c = 73.4 \t\t\t\t# [cubic cm/mole]\n",
+ "V_d = 89.8 \t\t\t\t\t# [cubic cm/mole]\n",
+ "V_a = 250 \t\t\t\t\t# [cubic cm/mole]\n",
+ "Z_c = 0.288 \n",
+ "Z_d = 0.290 \n",
+ "Z_a = 0.277 \n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "\n",
+ "\t# From equation 2.52 and 2.53\n",
+ "Tcm = yi_c*Tc_c+yi_d*Tc_d+yi_a*Tc_a \t\t# [K]\n",
+ "Pcm = 10**6*R*Tcm*(yi_c*Z_c+yi_d*Z_d+yi_a*Z_a)/((yi_c*V_c+yi_d*V_d+yi_a*V_a)*100000) \t# [bar]\n",
+ "M_avg = yi_c*M_c+yi_d*M_d+yi_a*M_a \t\t# [gram/mole]\n",
+ "\n",
+ "\t# From equation 2.50\n",
+ "Em = 0.176*(Tcm/(M_avg**3*Pcm**4))**(1.0/6.0) \t# [uP]**-1\n",
+ "\n",
+ "\t# From equation 2.51\n",
+ "Trm = Tavg/Tcm \n",
+ "f_Trm = (0.807*Trm**0.618)-(0.357*math.exp(-0.449*Trm))+(0.340*math.exp(-4.058*Trm))+0.018 \n",
+ "\t# From equation 2.49 \n",
+ "u = f_Trm/Em \t\t\t\t\t\t# [uP]\n",
+ "u = u*10**-7 \t\t\t\t\t\t# [viscosity, kg/m.s]\n",
+ "\n",
+ "Re = d*v*row_avg/u \t\t\t\t\t# [Renoylds number]\n",
+ "\n",
+ "\t\t# Diffusivity of UF6 vapors in air at 314 K and 1 atm from equation 1.49\n",
+ "D_ab = 0.0904 \t\t\t\t\t\t# [square cm/s]\n",
+ "\n",
+ "Sc = u/(row_avg*D_ab*10**-4) \t\t\t\t# [Schmidt number]\n",
+ "\n",
+ "Sh_avg = 0.43 + 0.532*Re**0.5*Sc**0.31 \t\t# [Sherwood number]\n",
+ "\t\t# From equation 1.7\n",
+ "c = P/(R*Tavg) \t\t\t\t\t# [kmole/cubic m]\n",
+ "\t\t# From Table 2.1 \n",
+ "F_av = Sh_avg*D_ab*c*10**-4/d \t\t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "\t\t# From equation 2.2\n",
+ "N_avg = F_av*math.log((1-y_a2)/(1-y_a1)) \t\t# [kmole/square m.s]\n",
+ "S = 2*math.pi*d**2/4 +math.pi*d*x \t\t\t# [total surface area of the cylinder, \t\t\t\t\t\t\tsquare m]\n",
+ "\n",
+ "w_a = N_avg*S*M_a \t\t\t\t\t# [rate of sublimation of the solid, \t\t\t\t\t\t\tkg/s] \n",
+ "\n",
+ "#Result\n",
+ "print\"Rate of sublimation of a cylinder of UF6 is\",round(w_a,5),\"kg/s\\n\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Rate of sublimation of a cylinder of UF6 is 0.00023 kg/s\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.7,Page number:116"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "import math\n",
+ "#Variable declaration\n",
+ "\n",
+ "\t# a-benzene b-nitrogen\n",
+ "T = 300 \t\t\t\t\t# [K]\n",
+ "P = 101.3 \t\t\t\t\t# [kPa]\n",
+ "v =10 \t\t\t\t\t\t# [m/s]\n",
+ "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "\n",
+ "\n",
+ "n = -0.5 \n",
+ "\t# Data on the properties of C02 at 300 K and 1 bar\n",
+ "u = 1.5*10**-5 \t\t\t\t# [viscosity, P]\n",
+ "Pr = 0.77 \t\t\t\t\t# [Prandtl number]\n",
+ "Cp = 853 \t\t\t\t\t# [J/kg.K]\n",
+ "\t# Therefore\n",
+ "\t# b = 5.086*l**0.5\n",
+ "\t# j_D = j_H = f(Re) = 5.086*(l**0.5)*Re**-0.5\n",
+ "\t# From Table 2.1\n",
+ "\t# F = j_D*c*v/Sc**(2/3) = 5.086*(l**0.5)*c*v/(Re**0.5*Sc**(2/3)) = \t\t5.086*(row*v*u)**0.5/(Mavg*Sc**(2.0/3.0))\n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "\t# Vapor pressure of benzene\n",
+ "P_a = math.exp(15.9008-(2788.51/(T-52.36))) \t# [mm of Hg]\n",
+ "P_a = P_a*101.3/760 \t\t\t\t\t# [kPa]\n",
+ "\n",
+ "\t# Parameter for a-benzene, b-nitrogen \n",
+ "yi_a = 0.07 \n",
+ "yi_b = 0.93 \n",
+ "Tc_a = 562.2 \n",
+ "Tc_b = 126.2 \t\t\t\t\t\t# [K]\n",
+ "Pc_a = 48.9 \n",
+ "Pc_b = 33.9 \t\t\t\t\t\t# [bar]\n",
+ "M_a = 78.1 \n",
+ "M_b = 28 \t\t\t\t\t\t# [gram/mole]\n",
+ "V_a = 259 \n",
+ "V_b = 89.8 \t\t\t\t\t\t# [cubic cm/mole]\n",
+ "Z_a = 0.271 \n",
+ "Z_b = 0.290 \n",
+ "sigma_a = 5.349 \n",
+ "sigma_b = 3.798 \t\t\t\t\t# [Angstrom]\n",
+ "ek_a = 412.3 \n",
+ "ek_b = 71.4 \t\t\t\t\t\t# [E/k, K]\n",
+ "\n",
+ "\n",
+ "\t# From equation 2.52 and 2.53\n",
+ "Tcm = yi_b*Tc_b+yi_a*Tc_a \t\t\t\t# [K]\n",
+ "Pcm = 10**6*R*Tcm*(yi_b*Z_b+yi_a*Z_a)/((yi_b*V_b+yi_a*V_a)*100000) \t # [bar]\n",
+ "M_avg = yi_b*M_b+yi_a*M_a \t\t\t\t\t\t# [kg/kmole]\n",
+ "\n",
+ "#RESULT\n",
+ "\n",
+ "print\"Average molecular weight is\",round(M_avg,1),\"kg/kmole\" \n",
+ "\n",
+ "row = P*M_avg/(R*T) \t\t\t\t\t\t\t# [kg/cubic m]\n",
+ "\n",
+ "#RESULT\n",
+ "\n",
+ "print\"Density of mixture is\",round(row,2),\"kg/cubic\"\n",
+ "\t# From equation 2.50\n",
+ "Em = 0.176*(Tcm/(M_avg**3*Pcm**4))**(1.0/6.0) \t\t\t# [uP]**-1\n",
+ "\n",
+ "\t# From equation 2.51\n",
+ "Trm = T/Tcm \n",
+ "f_Trm = (0.807*Trm**0.618)-(0.357*math.exp(-0.449*Trm))+(0.340*math.exp(-4.058*Trm))+0.018 \n",
+ "\t# From equation 2.49 \n",
+ "u = f_Trm/Em \t\t\t\t\t\t\t\t# [uP]\n",
+ "u = u*10**-7 \t\t\t\t\t\t\t\t# [viscosity, kg/m.s]\n",
+ "print\"Average viscosity of mixture is \",round(u,7),\"kg/m.s\\n\\n\" \n",
+ "\n",
+ "\t# Calculating diffusivity of benzene using equation 1.49\n",
+ "D_ab = 0.0986 \t\t\t\t\t\t\t\t# [square cm/s]\n",
+ "Sc = u/(row*D_ab*10**-4) \t\t\t\t\t\t# [Schmidt number]\n",
+ "\n",
+ "F = 5.086*(row*v*u)**0.5/(M_avg*Sc**(2.0/3.0)) \t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "\n",
+ "#RESULT\n",
+ "\n",
+ "print\"The required mass transfer coefficient is\",round(F,5),\"kmole/square m.s\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average molecular weight is 31.5 kg/kmole\n",
+ "Density of mixture is 1.28 kg/cubic\n",
+ "Average viscosity of mixture is 1.64e-05 kg/m.s\n",
+ "\n",
+ "\n",
+ "The required mass transfer coefficient is 0.00196 kmole/square m.s\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.8,Page number:120"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "import math\n",
+ "#Variable declaration\n",
+ "\t# a-liquid benzene b-nitrogen\n",
+ "T = 300 \t\t\t\t\t\t# [K]\n",
+ "l = 3 \t\t\t\t\t\t\t# [length of vertical plate, m]\n",
+ "b = 1.5 \t\t\t\t\t\t# [width of vertical plate, m]\n",
+ "P = 101.3 \t\t\t\t\t\t# [kPa]\n",
+ "v = 5 \t\t\t\t\t\t\t# [velocity across the width of plate, \t\t\t\t\t\t\tm/s]\n",
+ "row_a = 0.88 \t\t\t\t\t\t# [gram/cubic cm]\n",
+ "\n",
+ "\n",
+ "y_a1 = 0.139 \t\t\t\t\t\t# [mole fraction of benzene at inner \t\t\t\t\t\t\tedge]\n",
+ "y_a2 = 0 \n",
+ "\n",
+ "\t# The film conditions, and average properties, are identical to those in Example 2.7, \tonly the geometry is different\n",
+ "\t# Therefore\n",
+ "M_avg = 31.4 \t\t\t\t\t\t# [kg/kmole]\n",
+ "row = 1.2 \t\t\t\t\t\t# [kg/cubic m]\n",
+ "u = 161*10**-7 \t\t\t\t\t# [kg/m.s]\n",
+ "D_ab = 0.0986 \t\t \t\t\t\t# [square cm/s]\n",
+ "Sc = 1.3 \t\t\t\t\t\t# [Schmidt Number]\n",
+ "Re = row*v*b/u \t\t\t\t\t# [Renoylds Number]\n",
+ "\n",
+ "if Re > 4000:\n",
+ " print\"The flow across the plate is turbulent\\n\" \n",
+ "elif Re<2000:\n",
+ " print\"The flow across the plate is laminar\\n\" \n",
+ "\t#Using equation 2.57\n",
+ "Sh_l = 0.036*Re**0.8*Sc**(1.0/3.0) \n",
+ "\n",
+ "\t# Nitrogen (component B) does not react with benzene (component A), neither dissolves in \t\tthe liquid therefore, NB = 0 and siA = 1. The F-form of the mass-transfer coefficient \t\t\tshould be used \n",
+ "F = Sh_l*1.26*D_ab*10**-4/(M_avg*b) \t\t\t# [kmole/square m.s]\n",
+ "N_a = F*math.log((1-y_a2)/(1-y_a1)) \t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "\t# The total mass rate of evaporation over the surface of the plate\n",
+ "S = 1.5*3 \t\t\t\t\t\t# [square m]\n",
+ "M_a = 78.1 \t\t\t\t\t\t# [gram/mole]\n",
+ "wa = N_a*S*M_a*60*1000 \t\t\t\t# [gram/min]\n",
+ "\n",
+ "V = wa/row_a \t\t\t\t\t\t# [volumetric flow rate, ml/min]\n",
+ "\n",
+ "print\"Liquid benzene should be supplied at the top of the plate at the rate \",round(V),\"ml/min\\nso that evaporation will just prevent it from reaching the bottom of the plate.\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The flow across the plate is turbulent\n",
+ "\n",
+ "Liquid benzene should be supplied at the top of the plate at the rate 1473.0 ml/min\n",
+ "so that evaporation will just prevent it from reaching the bottom of the plate.\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.9,Page number:123"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "\t# a-water b-air\n",
+ "dp1 = 10**-3 \t\t\t\t\t# [diameter of spherical drop of water, m]\n",
+ "Tair = 323 \t\t\t\t\t# [K]\n",
+ "P = 101.3 \t\t\t\t\t# [kPa]\n",
+ "Twater = 293 \t\t\t\t\t# [K]\n",
+ "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "M_a = 18.0 \t\t\t\t\t# [gram/mole]\n",
+ "M_b = 29.0 \t\t\t\t\t# [gram/mole]\n",
+ "import math\n",
+ "\n",
+ "\n",
+ "#Calculation\n",
+ "\n",
+ "dp2 = (1.0/2.0)**(1.0/3.0)*dp1 \t\t# [m]\n",
+ "dp = (dp1+dp2)/2 \t\t\t\t# [m]\n",
+ "\n",
+ "row_p = 995 \t\t\t\t\t# [density of water, kg/cubic m]\n",
+ "row1b = 1.094 \t\t\t\t\t# [density of air, kg/cubic m]\n",
+ "u = 1.95*10**-5 \t\t\t\t# [kg/m.s]\n",
+ "row_pr = row_p-row1b \t\t\t\t# [kg/cubic m]\n",
+ "g = 9.8 \t\t\t\t\t# [accleration due to gravity, square m/s]\n",
+ "\t# Combining equation 2.68 and 2.69\n",
+ "Ga = 4*dp**3*row1b*row_pr*g/(3*u**2) \t\t# [Galileo Number]\n",
+ "\n",
+ "\t# Relationship between Re and Cd\n",
+ "\t# Re/Cd = Re**3/Ga = 3*row**2*vt**3/(4*g*u*row_pr)\n",
+ "\n",
+ "\t# The following correlation is used to relate Re/Cd, to Ga\n",
+ "\t# ln(Re/Cd)**(1/3) = -3.194 + 2.153*ln(Ga)**(1/3) - 0.238*(ln(Ga)**(1/3))**2 + \t0.01068*(ln(Ga)**(1/3))**3\n",
+ "\t# Therefore let A = (Re/Cd)\n",
+ "A = math.exp(-3.194 + 2.153*math.log(Ga**(1.0/3.0)) - 0.238*(math.log(Ga**(1.0/3.0)))**2 + 0.01068*(math.log(Ga**(1.0/3.0)))**3) \n",
+ "\n",
+ "\t# Therefore 'vt' will be\n",
+ "vt = A*(4*g*row_pr*u/(3*row1b**2))**(1.0/3.0) \t# [Terminal velocity of particle, m/s]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"Terminal velocity of particle is\",round(vt,2),\"m/s\" \n",
+ "\n",
+ "\n",
+ "#Calculation\n",
+ "\n",
+ "P_w = 2.34 \t\t\t\t\t# [vapor pressure of water, kPa]\n",
+ "y_w = P_w/P \t\t\t\t\t# [mole fraction of water at the inner edge of \t\t\t\t\t\tthe gas film]\n",
+ "M_avg = 18*y_w+29*(1-y_w) \t\t\t# [gram/mole]\n",
+ "\n",
+ "row2b = P*M_avg/(R*Twater) \t\t\t# [kg/cubic.m]\n",
+ "delta_row = row2b - row1b \t\t\t# [kg/cubic.m]\n",
+ "\n",
+ "Tavg = (Tair+Twater)/2 \t\t\t# [K]\n",
+ "\t\t# At Temperature equal to Tavg density and viscosity are\n",
+ "row3 = 1.14 \t\t\t\t\t# [kg/cubic.m]\n",
+ "u1 = 1.92*10**-5 \t\t\t\t# [kg/m.s]\n",
+ "\n",
+ "Grd = g*row3*delta_row*(dp**3)/(u1**2) \n",
+ "\n",
+ "\t\t# Diffusivity of water at Tavg and 1 atm is\n",
+ "D_ab = 0.242*10**-4 \t\t\t\t# [square m/s]\n",
+ "Sc = u1/(row3*D_ab) \t\t\t\t# [Schmidt Number]\n",
+ "Re = dp*row3*vt/u1 \t\t\t\t# [Renoylds Number]\n",
+ "\t\n",
+ "\t# From equation 2.65 Re is greater than 0.4*Grd**0.5*Sc**(-1/6)\n",
+ "\t# Therfore equation 2.64 can be used to calculate mass transfer coefficient\n",
+ "\n",
+ "Sh = 2+0.552*(Re**0.5)*Sc**(1.0/3.0) \t\t# [Sherwood Number]\n",
+ "\t# From Table 2.1\n",
+ "\t# Sh = kc*P_bm*dp/(P*D_ab), since P_bm is almost equal to P\n",
+ "\t# Therefore \n",
+ "\t# Sh = kc*dp/D_ab \n",
+ "kc = Sh*D_ab/dp \t\t\t\t# [m/s]\n",
+ "\n",
+ "ca2 = 0 \t\t\t\t\t# [dry air concentration]\n",
+ "ca1 = P_w/(R*Twater) \t\t\t\t# [interface concentration, kmole/cubic.m]\n",
+ "\t# Average rate of evaporation \n",
+ "wa = math.pi*dp**2*M_a*kc*(ca1-ca2)*1000 \t# [g/s]\n",
+ "\n",
+ "\t# Amount of water evaporated\n",
+ "m = row_p*math.pi*dp1**3/12*1000 \t\t# [g]\n",
+ "\t# Time necessary to reduce the volume by 50%\n",
+ "t = m/wa \t\t\t\t\t# [s]\n",
+ "\n",
+ "D = t*vt \t\t\t\t\t# [distance of fall, m]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"The distance of fall is\",round(D),\"m\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Terminal velocity of particle is 3.59 m/s\n",
+ "The distance of fall is 90.0 m\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.10,Page number:127"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaratiopn\n",
+ "\n",
+ "\t# Example 2.6 using equation 2.73\n",
+ "\t# Values of the dimensionless parameters calculated in Example 2.6\n",
+ "Re = 1223 \t\t\t\t# [Renoylds Number]\n",
+ "Sc = 0.905 \t\t\t\t# [Schmidt Number]\n",
+ "c = 0.039 \t\t\t\t# [molar density, kg/cubic m]\n",
+ "v = 1 \t\t\t\t\t# [gas velocity, m/s]\n",
+ "\t# Therefore \n",
+ "#Calculations\n",
+ "\n",
+ "Gm = c*v \t\t\t\t# [kmole/square m.s]\n",
+ "\t# From equation 2.9 \n",
+ "\t# Kg*P = ky\n",
+ "\t# Therefore substituting in equation 2.73 we obtain\n",
+ "ky = 0.281*Gm/(Re**0.4*Sc**0.56) \t# [kmole/square m.s]\n",
+ "\t# Now equation 2.73 were obtained under very dilute concentrations\n",
+ "\t# Therefore\n",
+ "y_bm = 1 \n",
+ "\t# From equation 2.6\n",
+ "F = ky*y_bm \t\t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"Mass transfer coefficient is \",round(F,6),\"kmol/m.^2-s\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Mass transfer coefficient is 0.000675 kmol/m.^2-s\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.11,Page number:129"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "\n",
+ "# a-water b-air\n",
+ "D = 25.4*10**-3 \t\t\t\t# [diameter of wetted wall tower, m]\n",
+ "Gy = 10 \t\t\t\t\t# [mass velocity, kg/square m.s]\n",
+ "T1 = 308 \t\t\t\t\t# [K]\n",
+ "P = 101.3 \t\t\t\t\t# [kPa]\n",
+ "p_a1 = 1.95 \t\t\t\t\t# [partial pressure of water vapor, kPa]\n",
+ "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "M_a = 18 \t\t\t\t\t# [gram/mole]\n",
+ "Cpa = 1.88 \t\t\t\t\t# [kJ/kg.K]\n",
+ "\n",
+ "\n",
+ "# Properties of dry air at 308 K and 1 atm pressure are\n",
+ "u = 1.92*10**-5 \t\t\t\t# [kg/m.s]\n",
+ "row = 1.14 \t\t\t\t\t# [kg/cubic m]\n",
+ "D_ab = 0.242*10**-4 \t\t\t\t# [square m/s]\n",
+ "Sc = 0.696 \t\t\t\t\t# [Schmidt number]\n",
+ "Cp = 1.007 \t\t\t\t\t# [kJ/kg.K]\n",
+ "k = 0.027 \t\t\t\t\t# [W/m.K]\n",
+ "Pr = 0.7 \t\t\t\t\t# [Prandtl number]\n",
+ "\n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "import math\n",
+ "from scipy.optimize import fsolve\n",
+ "from numpy import *\n",
+ "\n",
+ "Re = D*Gy/u \t\t\t\t\t# [Renoylds number]\n",
+ "# From equation 2,74\n",
+ "Sh = 0.023*Re**0.83*Sc**0.44 \t\t\t#[Sherwood number]\n",
+ "# From Table 2.1\n",
+ "kg = Sh*D_ab/(R*T1*D)*1000 \t\t\t# [mole/square m.s.kPa]\n",
+ "\n",
+ "# To estimate the heat-transfer coefficient, we use the Dittus-Boelter equation for cooling, equation 2.80\n",
+ "Nu = 0.023*Re**0.8*Pr**0.3 \t\t\t# [Nusselt number]\n",
+ "# From Table 2.1\n",
+ "h = Nu*k/D \t\t\t\t\t# [W/square m.K]\n",
+ "\n",
+ "T =373.15 \t\t\t\t\t# [K]\n",
+ "lambda_a = 40.63 \t\t\t\t# [kJ/mole]\n",
+ "Tc = 647.1 \t\t\t\t\t# [K]\n",
+ "\n",
+ "# Solution of simultaneous equation 2.78 and 2.79\n",
+ "def F(e): \n",
+ " f1=kg*(p_a1 - math.exp(16.3872-(3885.7/(e[0]-42.98))))-e[1] \n",
+ " f2=e[1]*M_a*Cpa*(T1-e[0])/(1-exp(-e[1]*M_a*Cpa/h)) + 1000*e[1]*lambda_a*((1-(e[0]/Tc))/(1-(T/Tc)))**0.38 \n",
+ " return(f1,f2) \n",
+ "\n",
+ "\n",
+ "# Initial guess\n",
+ "e = [292,-0.003] \n",
+ "y = fsolve(F,e) \n",
+ "Ti = y[0] \n",
+ "Na = y[1] \n",
+ "\n",
+ "print\"The temperature of the liquid water and the rate of water evaporation is\",round(Ti),\"K and\",round(Na,3),\" mole/square m.s respectively\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The temperature of the liquid water and the rate of water evaporation is 295.0 K and -0.013 mole/square m.s respectively\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.12,Page number:131"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "\t# a-water b-dry air\n",
+ "D = 25.4*10**-3 \t\t\t# [Internal diameter of tower, m]\n",
+ "Z = 1.5 \t\t\t\t# [length of the wetted section, m]\n",
+ "Gy = 10 \t\t\t\t# [mass velocity of air, kg/square m.s]\n",
+ "Tair = 308 \t\t\t\t# [K]\n",
+ "Twater = 295 \t\t\t\t# [K]\n",
+ "P = 101.3 \t\t\t\t# [kPa]\n",
+ "M_a = 18.0 \t\t\t\t# [gram/mole]\n",
+ "M_b = 29.0 \t\t\t\t# [gram/mole]\n",
+ "R = 8.314 \t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "\n",
+ "#Calculations\n",
+ "\n",
+ "import math\n",
+ "\n",
+ "Pa = 2.64 # [kPa]\n",
+ "\n",
+ "Gm = Gy/M_b \t# [Assuming that gas phase is basically dry air, kmole/square m.s]\n",
+ "\t\t# The properties of dry air at 308 K and 1 atm are (from example 2.9)\n",
+ "row = 1.14 \t\t\t\t# [kg/cubic m]\n",
+ "u = 1.92*10**-5 \t\t\t# [kg/m.s]\n",
+ "D_ab = 0.242*10**-4 \t\t\t# [square m/s]\n",
+ "Sc = 0.692 \t\t\t\t# [Schmidt number]\n",
+ "\n",
+ "Re = Gy*D/u \t\t\t\t# [Renoylds number]\n",
+ "\n",
+ "if Re<35000 and Re>2000:\n",
+ " Sh = 0.023*Re**0.83*Sc**0.44 \t# [Sherwood number] \n",
+ " print\"Sherwood number is\",round(Sh,1) \n",
+ "else:\n",
+ " print\"We cannot use equation 2.74\"\n",
+ "\n",
+ "c = P/(R*Tair) \t\t\t# [kmole/cubic m]\n",
+ "\t# Now using equation 2.89\n",
+ "Pa_out = Pa*(1-math.exp((-4*Sh*Z*c*D_ab)/(Gm*D**2))) \t\t# [kPa]\n",
+ "\n",
+ "#Result\n",
+ "print\"The partial pressure of water in the air leaving the tower is\",round(Pa_out,2),\"kPa\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sherwood number is 51.6\n",
+ "The partial pressure of water in the air leaving the tower is 1.94 kPa\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.13,Page number:134"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#Variable declaration\n",
+ "\t# a-water b-dry air\n",
+ "Gy = 10.0 \t\t\t\t# [kg/square m.s]\n",
+ "dp = 3.5*10**-3 \t\t\t# [diameter of spherical glass beads, m]\n",
+ "D = 25.4*10**-3 \t\t\t# [Internal diameter of tower, m]\n",
+ "Tair = 308 \t\t\t\t# [K]\n",
+ "Twater = 295 \t\t\t\t# [K]\n",
+ "P = 101.3 \t\t\t\t# [kPa]\n",
+ "M_a = 18 \t\t\t\t# [gram/mole]\n",
+ "M_b = 29 \t\t\t\t# [gram/mole]\n",
+ "R = 8.314 \t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "\n",
+ "#Calculation\n",
+ "\n",
+ "import math\n",
+ "from scipy.optimize import fsolve\n",
+ "\t# The properties of dry air at 308 K and 1 atm are (from example 2.12)\n",
+ "row = 1.14 \t\t\t\t# [kg/cubic m]\n",
+ "u = 1.92*10**-5 \t\t\t# [kg/m.s]\n",
+ "D_ab = 0.242*10**-4 \t\t\t# [square m/s]\n",
+ "Sc = 0.692 \t\t\t\t# [Schmidt number]\n",
+ "c = 0.04 \t \t\t\t# [mole/cubic m]\n",
+ "Gm = 0.345 \t\t\t\t# [kmole/square m.s]\n",
+ "\n",
+ "Re = Gy*dp/u \t\t\t\t# [Renoylds number]\n",
+ "if Re<2500 and Re>10:\n",
+ " \t\t\t\t\t# Subsituting in equation 2.90\n",
+ " jd = 1.17*Re**-0.415 \n",
+ " print\"Renoylds number is \",Re\n",
+ "else:\n",
+ " print \" \"\n",
+ "Std = 0.052/(Sc**(2.0/3.0)) \n",
+ "\t\t# From Table 2.1 \n",
+ "Sh = Std*Re*Sc \t\t\t# [Sherwood number]\n",
+ "\t\t# From equation 2.94\n",
+ "e = 0.406+0.571*(dp/D) \t\t# [bed porosity]\n",
+ "e=round(e,3)\n",
+ "\t#Illustration 2.13(a) \n",
+ "\t# Solution(a)\n",
+ "\t# Now Paout = 0.99*Pa\n",
+ "\t# Using equation 2.93 to calculate 'Z'\n",
+ "def f12(Z):\n",
+ " return(0.99 - 1 + math.exp(-6*(1-e)*Sh*c*Z*D_ab/(Gm*dp**2))) \n",
+ "Z = fsolve(f12,0.06) \n",
+ "\n",
+ "#Result\n",
+ "Z=round(Z[0],3)\n",
+ "print\"The depth of packing required is\",Z,\"m=\",Z*100,\"cm\" \n",
+ "\n",
+ "\t#Illustration 2.13(b)\n",
+ "\t# Solution(b)\n",
+ "\t# From equation 2.95\n",
+ "deltaP = (150*(1-e)/Re + 1.75)*((1-e)*(Gy**2)*Z)/(dp*row*e**3) \t# [Pa]\n",
+ "\n",
+ "#Result\n",
+ "print\"The gas pressure drop through the bed is\",round(deltaP),\"Pa (Approx) \\nDUE TO LACK OF PRECISION IN CALCULATION IN BOOK.\\niF DONE MANUALLY,THIS ANSWER STANDS CORRECT\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Renoylds number is 1822.91666667\n",
+ "The depth of packing required is 0.078 m= 7.8 cm\n",
+ "The gas pressure drop through the bed is 15817.0 Pa (Approx) \n",
+ "DUE TO LACK OF PRECISION IN CALCULATION IN BOOK.\n",
+ "iF DONE MANUALLY,THIS ANSWER STANDS CORRECT\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.14,Page number:138"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "\t# a-oxygen b-water\n",
+ "\t# To design the deaerator, We will use commercially available microporous polypropylene \thollow fibers in a module\n",
+ "\n",
+ "m = 40000.0 \t\t\t\t\t# [kg/hr]\n",
+ "Twater = 298 \t\t\t\t\t# [K]\n",
+ "v = 0.1 \t\t\t\t\t# [superficial velocity, m/s]\n",
+ "P = 101.3 \t\t\t\t\t# [kPa]\n",
+ "V = 40*10**-3 \t\t\t\t\t# [Flow rate of nitrogen, cubic m/min]\n",
+ "d = 2.90*10**-4 \t\t\t\t# [Outside diameter of fibres, m]\n",
+ "pf = 0.4 \t\t\t\t\t# [Packing factor]\n",
+ "a = 46.84*100 \t\t\t\t\t# [surface area per unit volume, m**-1]\n",
+ "R = 8.314 \t\t\t\t\t# [cubic m.Pa/mole.K]\n",
+ "\n",
+ "#Calculation\n",
+ "\n",
+ "import math\n",
+ "\n",
+ "dw = 1000 \t\t\t\t\t# [density of water, kg/cubic m]\n",
+ "Ql = m/(3600*1000) \t\t\t\t# [volumetric water flow rate, cubic m/s]\n",
+ "\t# Shell diameter\n",
+ "D = (4*Ql/(math.pi*v))**0.5 \t\t\t# [Shell diameter, m]\n",
+ "\n",
+ "\t# the properties of dilute mixtures of oxygen in water at 298 K\n",
+ "u = 0.9 \t\t\t\t\t# [cP]\n",
+ "\t# Diffusivity from equation 1.53\n",
+ "D_ab = 1.93*10**-9 \t\t\t\t# [square m/s]\n",
+ "Sc = 467 \t\t\t\t\t# [Schmidt number]\n",
+ "\n",
+ "Re = d*v*dw/(u*10**-3) \t\t\t# [Renoylds number]\n",
+ "\n",
+ "\t# Substituting in equation (2-97) gives\n",
+ "Sh = 0.53*(1-1.1*pf)*((1-pf)/pf)**-0.47*(Re**0.53*Sc**0.33) \n",
+ "\n",
+ "kl = Sh*D_ab/d \t\t\t\t# [mass-transfer coefficient on the shell side, \t\t\t\t\t\tm/s]\n",
+ "\n",
+ "\t# From the specified BFW flow rate\n",
+ "L = m/(3600*18) \t\t\t\t# [kmole/s]\n",
+ "\t# From ideal gas law\n",
+ "V1 = V*P/(Twater*R*60) \t\t\t# [kmole/s]\n",
+ "\t# From the solubility of oxygen in water at 298 K,\n",
+ "M = 4.5*10**4 \n",
+ "A = L/(M*V1) \t\t\t\t\t# [Absorption factor]\n",
+ "\n",
+ "#Result\n",
+ "\n",
+ "print\"Absorption factor is\",round(A,3) \n",
+ "\n",
+ "#Calculation\n",
+ "\n",
+ "\t# For 99% removal of the dissolved oxygen\n",
+ "\t# x_in/x_out = b = 100\n",
+ "b = 100 \n",
+ "c = 55.5 \t\t\t\t\t# [molar density, kmole/cubic m]\n",
+ "\t# Substituting in equation 2.99 yields\n",
+ "V_T = (L*math.log(b*(1-A)+A))/(kl*a*c*(1-A)) \t # [cubic m]\n",
+ "\n",
+ "\t# The module length, Z is\n",
+ "Z = V_T/(math.pi*D**2.0/4.0) \n",
+ "\n",
+ "#Result\n",
+ "print\"The shell diameter and module length is\",round(D,3),\"m and\",round(Z,2),\" m respectively\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Absorption factor is 0.503\n",
+ "The shell diameter and module length is 0.376 m and 2.15 m respectively\n"
+ ]
+ }
+ ],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Example 2.15,Page number:140"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "\n",
+ "#Variable declaration\n",
+ "d=2.21/100\t\t\t#[m]\n",
+ "mu=8.82*10**-6\t\t#[kg/m-s]\n",
+ "rho=2.81\t\t#[kg/m**3]\n",
+ "\n",
+ "c=34.14\t\t\t#[mol/m**3]\n",
+ "D=array([2.228/(10**6),2.065/(10**6),1.832/(10**6)]) #Velocities in [m**2/s]\n",
+ "\n",
+ "\n",
+ "#Calculation\n",
+ "#Gy=rho*v\t\t\n",
+ "#Re=Gy*d/mu\t\t#Reynolds number\n",
+ "Re=21750\n",
+ "print \"Reynolds number=\",Re\n",
+ "Sc=[]\n",
+ "Sh=[]\n",
+ "F=[]\n",
+ "for i in range(0, 3):\n",
+ " sc=mu/(rho*D[i]) #Schmidt number\n",
+ " Sc.append(sc)\n",
+ " sh=0.023*Re**(0.83)*sc**(0.44) #Sherwood number\n",
+ " Sh.append(sh)\n",
+ " f=sh*c*D[i]/d #Binary mass transfer coefficient in [mol/m^2-s]\n",
+ " F.append(f)\n",
+ "print \"Schmidt number are:\"\n",
+ "for i in range(0,3):\n",
+ " print round(Sc[i],3)\n",
+ "print \"Sherwood number number are:\"\n",
+ "for i in range(0,3):\n",
+ " print round(Sh[i],1)\n",
+ "print\"Binary mass transfer coefficients are:\"\n",
+ "for i in range(0,3):\n",
+ " print round(F[i],3)\n",
+ "#After modifying mathcad program of example 1.17,we have\n",
+ "N1=-0.0527 #[mol/m^2-s]\n",
+ "N2=0.0395 #[mol/m^2-s]\n",
+ "N3=0.0132 #[mol/m^2-s]\n",
+ "print\"The program yields the following results:\"\n",
+ "print \"N1=\",N1,\"mol/m**2-s\"\n",
+ "print \"N2=\",N2,\"mol/m**2-s\"\n",
+ "print \"N3=\",N3,\"mol/m**2-s\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Reynolds number= 21750\n",
+ "Schmidt number are:\n",
+ "1.409\n",
+ "1.52\n",
+ "1.713\n",
+ "Sherwood number number are:\n",
+ "106.5\n",
+ "110.1\n",
+ "116.1\n",
+ "Binary mass transfer coefficients are:\n",
+ "0.367\n",
+ "0.351\n",
+ "0.328\n",
+ "The program yields the following results:\n",
+ "N1= -0.0527 mol/m**2-s\n",
+ "N2= 0.0395 mol/m**2-s\n",
+ "N3= 0.0132 mol/m**2-s\n"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ }
+ ],
+ "metadata": {}
+ }
+ ]
} \ No newline at end of file
diff --git a/Practical_C_Programming/Chapter_3_4.ipynb b/Practical_C_Programming/Chapter_3_4.ipynb
index b0fd7f89..1a74741f 100644
--- a/Practical_C_Programming/Chapter_3_4.ipynb
+++ b/Practical_C_Programming/Chapter_3_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 3"
+ "name": "",
+ "signature": "sha256:0893bf0eb544a1587dd8191af7502dfb1634a7c77a30f208ad67851517e5431c"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,36 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 3: Style"
+ "source": [
+ "Chapter 3: Style"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 3.1, Page number: 57"
+ "source": [
+ "Example 3.1, Page number: 57"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 3.1.py\n# To print 'Hello World'\n\n\n# Result\nprint ('Hello World')",
+ "input": [
+ "\n",
+ "\n",
+ "# Result\n",
+ "print ('Hello World')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Hello World\n"
+ "text": [
+ "Hello World\n"
+ ]
}
],
"prompt_number": 1
diff --git a/Practical_C_Programming/Chapter_4_4.ipynb b/Practical_C_Programming/Chapter_4_4.ipynb
index bb8fdf51..8602f989 100644
--- a/Practical_C_Programming/Chapter_4_4.ipynb
+++ b/Practical_C_Programming/Chapter_4_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 4"
+ "name": "",
+ "signature": "sha256:785237ffc865ddc47952e10309b1bb53ee1543217e833069c02c2afb50bff287"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,38 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 4: Basic declarations and expressions"
+ "source": [
+ "Chapter 4: Basic declarations and expressions"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.1, Page number: 71"
+ "source": [
+ "Example 4.1, Page number: 71"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.1.py\n# To perform a simple calculation\n\n\n# Variable declaration\nx = (1 + 2) * 4\n\n# Result\nprint ('1 plus 2 multiplied by 4 gives %d' % x)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "x = (1 + 2) * 4\n",
+ "\n",
+ "# Result\n",
+ "print ('1 plus 2 multiplied by 4 gives %d' % x)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1 plus 2 multiplied by 4 gives 12\n"
+ "text": [
+ "1 plus 2 multiplied by 4 gives 12\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +52,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.2, Page number: 75"
+ "source": [
+ "Example 4.2, Page number: 75"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.2.py\n# To calculate the twice and thrice of 15\n\n\n# Variable declaration\nterm = 3 * 5\nterm_2 = 2 * term\nterm_3 = 3 * term\n\n# Result\nprint ('Twice %d is %d' % (term, term_2))\nprint ('Three times %d is %d' % (term, term_3))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "term = 3 * 5\n",
+ "term_2 = 2 * term\n",
+ "term_3 = 3 * term\n",
+ "\n",
+ "# Result\n",
+ "print ('Twice %d is %d' % (term, term_2))\n",
+ "print ('Three times %d is %d' % (term, term_3))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Twice 15 is 30\nThree times 15 is 45\n"
+ "text": [
+ "Twice 15 is 30\n",
+ "Three times 15 is 45\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +88,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.3, Page number: 77"
+ "source": [
+ "Example 4.3, Page number: 77"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.3.py\n# To calculate the twice and thrice of 15\n\n\n# Variable declaration\nterm = 3 * 5\nterm_2 = 2 * term\nterm_3 = 3 * term\n\n# Result\nprint ('Twice %d is %d' % (term, term_2))\nprint ('Three times %d is %d' % (term, term_3))",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "term = 3 * 5\n",
+ "term_2 = 2 * term\n",
+ "term_3 = 3 * term\n",
+ "\n",
+ "# Result\n",
+ "print ('Twice %d is %d' % (term, term_2))\n",
+ "print ('Three times %d is %d' % (term, term_3))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Twice 15 is 30\nThree times 15 is 45\n"
+ "text": [
+ "Twice 15 is 30\n",
+ "Three times 15 is 45\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +125,30 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.4, Page number: 79"
+ "source": [
+ "Example 4.4, Page number: 79"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.4.py\n# To determine the value of 1/3\n\n\n# Variable declaration\nanswer = 1/3\n\n# Result\nprint ('The value of 1/3 is %f' % answer)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "answer = 1/3\n",
+ "\n",
+ "# Result\n",
+ "print ('The value of 1/3 is %f' % answer)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The value of 1/3 is 0.000000\n"
+ "text": [
+ "The value of 1/3 is 0.000000\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +157,29 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.5, Page number: 80"
+ "source": [
+ "Example 4.5, Page number: 80"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.5.py\n# To determine the value of 2 + 2\n\n\n# Variable declaration\nanswer = 2 + 2\n\n# Result\nprint ('The answer is %d' % answer)",
+ "input": [
+ "\n",
+ "answer = 2 + 2\n",
+ "\n",
+ "# Result\n",
+ "print ('The answer is %d' % answer)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The answer is 4\n"
+ "text": [
+ "The answer is 4\n"
+ ]
}
],
"prompt_number": 5
@@ -122,19 +188,31 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.6, Page number: 80"
+ "source": [
+ "Example 4.6, Page number: 80"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.6.py\n# To determine the integer equivalent of 7.0/22.0 \n\n\n# Variable declaration\nresult = 7.0/22.0\n\n# Result\nprint ('The result is %d' % result)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "result = 7.0/22.0\n",
+ "\n",
+ "# Result\n",
+ "print ('The result is %d' % result)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The result is 0\n"
+ "text": [
+ "The result is 0\n"
+ ]
}
],
"prompt_number": 6
@@ -143,19 +221,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 4.7, Page number: 82"
+ "source": [
+ "Example 4.7, Page number: 82"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 4.7.py\n# To determine the reverse of 3 characters\n\n\n# Variable declaration\nchar1 = 'A'\nchar2 = 'B'\nchar3 = 'C'\n\n# Result\nprint ('%c%c%c reversed is %c%c%c' % (char1, char2, char3, char3, char2, char1))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "char1 = 'A'\n",
+ "char2 = 'B'\n",
+ "char3 = 'C'\n",
+ "\n",
+ "# Result\n",
+ "print ('%c%c%c reversed is %c%c%c' % (char1, char2, char3, char3, char2, char1))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "ABC reversed is CBA\n"
+ "text": [
+ "ABC reversed is CBA\n"
+ ]
}
],
"prompt_number": 7
diff --git a/Practical_C_Programming/Chapter_5_4.ipynb b/Practical_C_Programming/Chapter_5_4.ipynb
index 37c16908..2d2abf16 100644
--- a/Practical_C_Programming/Chapter_5_4.ipynb
+++ b/Practical_C_Programming/Chapter_5_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 5"
+ "name": "",
+ "signature": "sha256:8925a816cb9ea05a6a48e6ec88b1e1778772a9e1e429803a5c922fb000f7f30d"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,42 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 5: Arrays, qualifiers, and reading numbers"
+ "source": [
+ "Chapter 5: Arrays, qualifiers, and reading numbers"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.1, Page number: 84"
+ "source": [
+ "Example 5.1, Page number: 84"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.1.py\n# To print the total and average of five data items\n\n\n# Variable declaration\ndata = [ 34.0, 27.0, 45.0, 82.0, 22.0 ]\n\n# Calculation\ntotal = data[0] + data[1] + data[2] + data[3] + data[4]\naverage = total/5.0\n\n# Result\nprint ('Total %f Average %f' % (total, average))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "data = [ 34.0, 27.0, 45.0, 82.0, 22.0 ]\n",
+ "\n",
+ "# Calculation\n",
+ "total = data[0] + data[1] + data[2] + data[3] + data[4]\n",
+ "average = total/5.0\n",
+ "\n",
+ "# Result\n",
+ "print ('Total %f Average %f' % (total, average))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Total 210.000000 Average 42.000000\n"
+ "text": [
+ "Total 210.000000 Average 42.000000\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +56,31 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.2, Page number: 87"
+ "source": [
+ "Example 5.2, Page number: 87"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.2.py\n# To print the name 'Sam'\n\n\n# Variable declaration\nname = 'Sam'\n\n# Result\nprint ('The name is %s' % name)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "name = 'Sam'\n",
+ "\n",
+ "# Result\n",
+ "print ('The name is %s' % name)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The name is Sam\n"
+ "text": [
+ "The name is Sam\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +89,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.3, Page number: 88"
+ "source": [
+ "Example 5.3, Page number: 88"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.3.py\n# To print the full name of a person\n\n\n# Variable declaration\nfirst = 'Steve'\nlast = 'Oualline'\n\n# Calculation\nfull_name = first + ' ' + last\n\n# Result\nprint ('The full name is %s' % full_name)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "first = 'Steve'\n",
+ "last = 'Oualline'\n",
+ "\n",
+ "# Calculation\n",
+ "full_name = first + ' ' + last\n",
+ "\n",
+ "# Result\n",
+ "print ('The full name is %s' % full_name)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The full name is Steve Oualline\n"
+ "text": [
+ "The full name is Steve Oualline\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +126,30 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.4, Page number: 89"
+ "source": [
+ "Example 5.4, Page number: 89"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.4.py\n# To calculate the length of a line\n\n\n# Variable declaration\nline = 'hello world'\n\n# Result\nprint ('The length of the line is %d' % len(line))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "line = 'hello world'\n",
+ "\n",
+ "# Result\n",
+ "print ('The length of the line is %d' % len(line))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The length of the line is 11\n"
+ "text": [
+ "The length of the line is 11\n"
+ ]
}
],
"prompt_number": 6
@@ -101,19 +158,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.5, Page number: 90"
+ "source": [
+ "Example 5.5, Page number: 90"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.5.py\n# To display the first and last names of a person\n\n\n# Variable declaration\nfirst = 'Steve'\nlast = 'Oualline'\n\n# Calculation\nfull = first + ' ' + last\n\n# Result\nprint ('The name is %s' % full)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "first = 'Steve'\n",
+ "last = 'Oualline'\n",
+ "\n",
+ "# Calculation\n",
+ "full = first + ' ' + last\n",
+ "\n",
+ "# Result\n",
+ "print ('The name is %s' % full)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The name is Steve Oualline\n"
+ "text": [
+ "The name is Steve Oualline\n"
+ ]
}
],
"prompt_number": 7
@@ -122,19 +195,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.6, Page number: 91"
+ "source": [
+ "Example 5.6, Page number: 91"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.6.py\n# To display the first and last names of a person\n\n\n# Variable declaration\nfirst = 'Steve'\nlast = 'Oualline'\n\n# Calculation\nfull = first + ' ' + last\n\n# Result\nprint ('The name is %s' % full)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "first = 'Steve'\n",
+ "last = 'Oualline'\n",
+ "\n",
+ "# Calculation\n",
+ "full = first + ' ' + last\n",
+ "\n",
+ "# Result\n",
+ "print ('The name is %s' % full)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The name is Steve Oualline\n"
+ "text": [
+ "The name is Steve Oualline\n"
+ ]
}
],
"prompt_number": 8
@@ -143,19 +231,64 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.7, Page number: 93"
+ "source": [
+ "Example 5.7, Page number: 93"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.7.py\n# To display the values of the elements of a 2-D array\n\n\n# Variable declaration\narray = [[0 for x in range(5)] for x in range(5)]\narray[0][0] = 0 * 10 + 0\narray[0][1] = 0 * 10 + 1\narray[1][0] = 1 * 10 + 0\narray[1][1] = 1 * 10 + 1\narray[2][0] = 2 * 10 + 0\narray[2][1] = 2 * 10 + 1\n\n# Result\nprint ('array[0]')\nprint (array[0][0])\nprint (array[0][1])\nprint ('\\n')\n\nprint ('array[1]')\nprint (array[1][0])\nprint (array[1][1])\nprint ('\\n')\n\nprint ('array[2]')\nprint (array[2][0])\nprint (array[2][1])\nprint ('\\n')",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "array = [[0 for x in range(5)] for x in range(5)]\n",
+ "array[0][0] = 0 * 10 + 0\n",
+ "array[0][1] = 0 * 10 + 1\n",
+ "array[1][0] = 1 * 10 + 0\n",
+ "array[1][1] = 1 * 10 + 1\n",
+ "array[2][0] = 2 * 10 + 0\n",
+ "array[2][1] = 2 * 10 + 1\n",
+ "\n",
+ "# Result\n",
+ "print ('array[0]')\n",
+ "print (array[0][0])\n",
+ "print (array[0][1])\n",
+ "print ('\\n')\n",
+ "\n",
+ "print ('array[1]')\n",
+ "print (array[1][0])\n",
+ "print (array[1][1])\n",
+ "print ('\\n')\n",
+ "\n",
+ "print ('array[2]')\n",
+ "print (array[2][0])\n",
+ "print (array[2][1])\n",
+ "print ('\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "array[0]\n0\n1\n\n\narray[1]\n10\n11\n\n\narray[2]\n20\n21\n\n\n"
+ "text": [
+ "array[0]\n",
+ "0\n",
+ "1\n",
+ "\n",
+ "\n",
+ "array[1]\n",
+ "10\n",
+ "11\n",
+ "\n",
+ "\n",
+ "array[2]\n",
+ "20\n",
+ "21\n",
+ "\n",
+ "\n"
+ ]
}
],
"prompt_number": 9
@@ -164,19 +297,30 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.8, Page number: 94"
+ "source": [
+ "Example 5.8, Page number: 94"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.8.py\n# To print the twice of a value\n\n\n# Variable declaration\nline = 4\n\n# Result\nprint ('Twice %d is %d' % (line, line * 2))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "line = 4\n",
+ "\n",
+ "# Result\n",
+ "print ('Twice %d is %d' % (line, line * 2))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Twice 4 is 8\n"
+ "text": [
+ "Twice 4 is 8\n"
+ ]
}
],
"prompt_number": 10
@@ -185,19 +329,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.9, Page number: 95"
+ "source": [
+ "Example 5.9, Page number: 95"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.9.py\n# To calculate the area of a triangle\n\n\n# Variable declaration\nwidth = 12\nheight = 10\n\n# Calculation\narea = (width * height) / 2\n\n# Result\nprint ('The area is %d' % area)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "width = 12\n",
+ "height = 10\n",
+ "\n",
+ "# Calculation\n",
+ "area = (width * height) / 2\n",
+ "\n",
+ "# Result\n",
+ "print ('The area is %d' % area)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The area is 60\n"
+ "text": [
+ "The area is 60\n"
+ ]
}
],
"prompt_number": 12
@@ -206,19 +365,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 5.10, Page number: 107"
+ "source": [
+ "Example 5.10, Page number: 107"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 5.10.py\n# To calculate the area of a triangle\n\n\n# Variable declaration\nwidth = 4\nheight = 6\n\n# Calculation\narea = (width * height) / 2\n\n# Result\nprint ('The area is %d' % area)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "width = 4\n",
+ "height = 6\n",
+ "\n",
+ "# Calculation\n",
+ "area = (width * height) / 2\n",
+ "\n",
+ "# Result\n",
+ "print ('The area is %d' % area)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The area is 12\n"
+ "text": [
+ "The area is 12\n"
+ ]
}
],
"prompt_number": 13
diff --git a/Practical_C_Programming/Chapter_6_4.ipynb b/Practical_C_Programming/Chapter_6_4.ipynb
index 22907862..2769b901 100644
--- a/Practical_C_Programming/Chapter_6_4.ipynb
+++ b/Practical_C_Programming/Chapter_6_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 6"
+ "name": "",
+ "signature": "sha256:72c49f1aed7f4d43ac1084e67245cfba7b011cb43f84b1ab3ef13b081aec557f"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,56 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 6: Decision and control statements"
+ "source": [
+ "Chapter 6: Decision and control statements"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 6.1, Page number: 114"
+ "source": [
+ "Example 6.1, Page number: 114"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 6.1.py\n# To print the Fibonacci series upto 100\n\n\n# Variable declaration\nold_number = 1\ncurrent_number = 1\n\nprint ('1')\n\n# Calculation and result\nwhile (current_number < 100) :\n print (current_number)\n next_number = current_number + old_number\n\n old_number = current_number\n current_number = next_number",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "old_number = 1\n",
+ "current_number = 1\n",
+ "\n",
+ "print ('1')\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (current_number < 100) :\n",
+ " print (current_number)\n",
+ " next_number = current_number + old_number\n",
+ "\n",
+ " old_number = current_number\n",
+ " current_number = next_number"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n"
+ "text": [
+ "1\n",
+ "1\n",
+ "2\n",
+ "3\n",
+ "5\n",
+ "8\n",
+ "13\n",
+ "21\n",
+ "34\n",
+ "55\n",
+ "89\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +70,52 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 6.2, Page number: 115"
+ "source": [
+ "Example 6.2, Page number: 115"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 6.2.py\n# To calculate the number of items entered\n\n\n# Variable declaration\ntotal = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n\n if item == 0 :\n break\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation\n",
+ "while (i < 10) :\n",
+ " item = 1\n",
+ "\n",
+ " if item == 0 :\n",
+ " break\n",
+ "\n",
+ " total += item\n",
+ " print ('Total: %d' % total)\n",
+ " i = i + 1\n",
+ " \n",
+ "# Result\n",
+ "print ('Final total: %d' % total)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\n"
+ "text": [
+ "Total: 1\n",
+ "Total: 2\n",
+ "Total: 3\n",
+ "Total: 4\n",
+ "Total: 5\n",
+ "Total: 6\n",
+ "Total: 7\n",
+ "Total: 8\n",
+ "Total: 9\n",
+ "Total: 10\n",
+ "Final total: 10\n"
+ ]
}
],
"prompt_number": 1
@@ -59,19 +124,59 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 6.3, Page number: 116"
+ "source": [
+ "Example 6.3, Page number: 116"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 6.3.py\n# To calculate the total number of items entered and omit the negative items\n\n\n# Variable declaration\ntotal = 0\nminus_items = 0\ni = 0\n\n# Calculation\nwhile (i < 10) :\n item = 1\n \n if item == 0 :\n break\n\n if item < 0 :\n minus_items += 1\n continue\n\n total += item\n print ('Total: %d' % total)\n i = i + 1\n \n# Result\nprint ('Final total: %d' % total)\nprint ('with %d negative items omitted' % minus_items)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "minus_items = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation\n",
+ "while (i < 10) :\n",
+ " item = 1\n",
+ " \n",
+ " if item == 0 :\n",
+ " break\n",
+ "\n",
+ " if item < 0 :\n",
+ " minus_items += 1\n",
+ " continue\n",
+ "\n",
+ " total += item\n",
+ " print ('Total: %d' % total)\n",
+ " i = i + 1\n",
+ " \n",
+ "# Result\n",
+ "print ('Final total: %d' % total)\n",
+ "print ('with %d negative items omitted' % minus_items)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Total: 1\nTotal: 2\nTotal: 3\nTotal: 4\nTotal: 5\nTotal: 6\nTotal: 7\nTotal: 8\nTotal: 9\nTotal: 10\nFinal total: 10\nwith 0 negative items omitted\n"
+ "text": [
+ "Total: 1\n",
+ "Total: 2\n",
+ "Total: 3\n",
+ "Total: 4\n",
+ "Total: 5\n",
+ "Total: 6\n",
+ "Total: 7\n",
+ "Total: 8\n",
+ "Total: 9\n",
+ "Total: 10\n",
+ "Final total: 10\n",
+ "with 0 negative items omitted\n"
+ ]
}
],
"prompt_number": 2
@@ -80,19 +185,34 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 6.4, Page number: 118"
+ "source": [
+ "Example 6.4, Page number: 118"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 6.4.py\n# To calculate the number of dollars owed\n\n\n# Variable declaration\nbalance_owed = 100\n\n# Calculation and result\nif balance_owed == 0 :\n print ('You owe nothing.')\nelse :\n print ('You owe %d dollars.' % balance_owed)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "balance_owed = 100\n",
+ "\n",
+ "# Calculation and result\n",
+ "if balance_owed == 0 :\n",
+ " print ('You owe nothing.')\n",
+ "else :\n",
+ " print ('You owe %d dollars.' % balance_owed)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "You owe 100 dollars.\n"
+ "text": [
+ "You owe 100 dollars.\n"
+ ]
}
],
"prompt_number": 3
diff --git a/Practical_C_Programming/Chapter_7_4.ipynb b/Practical_C_Programming/Chapter_7_4.ipynb
index 51b18ca7..ad153ba6 100644
--- a/Practical_C_Programming/Chapter_7_4.ipynb
+++ b/Practical_C_Programming/Chapter_7_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 7"
+ "name": "",
+ "signature": "sha256:05d2b6828dd178069989841e00a87dd5090f48e199f3c803503e5017b744fb21"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,51 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 7: Programming process"
+ "source": [
+ "Chapter 7: Programming process"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 7.1, Page number: 126"
+ "source": [
+ "Example 7.1, Page number: 126"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 7.1.py\n# To perform an operation based on operator input by the user\n\n\n# Variable declaration\nresult = 0\ni = 0\n\n# Calculation and result\nwhile (i < 3) :\n print ('Result: %d' % result)\n operator = '+'\n value = 10\n\n if operator == '+' :\n result += value\n else :\n print ('Unknown operator %c' % operator)\n i = i + 1 ",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "result = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (i < 3) :\n",
+ " print ('Result: %d' % result)\n",
+ " operator = '+'\n",
+ " value = 10\n",
+ "\n",
+ " if operator == '+' :\n",
+ " result += value\n",
+ " else :\n",
+ " print ('Unknown operator %c' % operator)\n",
+ " i = i + 1 "
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Result: 0\nResult: 10\nResult: 20\n"
+ "text": [
+ "Result: 0\n",
+ "Result: 10\n",
+ "Result: 20\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +65,57 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 7.2, Page number: 133"
+ "source": [
+ "Example 7.2, Page number: 133"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 7.2.py\n# To perform various arithmetic operations based on operator input by the user\n\n\n# Variable declaration\nresult = 0\ni = 0\n\n# Calculation and result\nwhile (i < 3) :\n print ('Result: %d' % result)\n operator = '+'\n value = 5\n\n if operator == 'q' or operator == 'Q' :\n break\n\n if operator == '+' :\n result += value\n\n if operator == '-' :\n result -= value\n\n if operator == '*' :\n result *= value\n\n if operator == '/' :\n if value == 0 :\n print ('Error: Divide by zero')\n print ('operation ignored') \n else :\n result /= value\n\n i = i + 1",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "result = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (i < 3) :\n",
+ " print ('Result: %d' % result)\n",
+ " operator = '+'\n",
+ " value = 5\n",
+ "\n",
+ " if operator == 'q' or operator == 'Q' :\n",
+ " break\n",
+ "\n",
+ " if operator == '+' :\n",
+ " result += value\n",
+ "\n",
+ " if operator == '-' :\n",
+ " result -= value\n",
+ "\n",
+ " if operator == '*' :\n",
+ " result *= value\n",
+ "\n",
+ " if operator == '/' :\n",
+ " if value == 0 :\n",
+ " print ('Error: Divide by zero')\n",
+ " print ('operation ignored') \n",
+ " else :\n",
+ " result /= value\n",
+ "\n",
+ " i = i + 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Result: 0\nResult: 5\nResult: 10\n"
+ "text": [
+ "Result: 0\n",
+ "Result: 5\n",
+ "Result: 10\n"
+ ]
}
],
"prompt_number": 4
@@ -59,12 +124,56 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 7.3, Page number: 140"
+ "source": [
+ "Example 7.3, Page number: 140"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 7.3.py\n# Guess - A simple guessing game\n# A random number is chosen between 1 and 100. \n# The player is given a set of bounds and \n# must choose a number between them. \n# If the player chooses the correct number, he wins.\n# Otherwise, the bounds are adjusted to reflect\n# the player's guess and the game continues.\n\n# Variable declaration, calculation and result\nimport sys\nimport random\nwhile (1) :\n\n # random number to be guessed\n number_to_guess = random.randrange (1, 101, 1)\n\n # current lower limit of player's range\n low_limit = 0\n\n # current upper limit of player's range\n high_limit = 100\n \n # number of times player guessed\n guess_count = 0\n\n while (1) :\n \n # tell user what the bounds are and get his guess\n print ('Bounds %d - %d\\n' % (low_limit, high_limit))\n print ('Value[%d]?' % guess_count)\n \n guess_count += 1\n\n # number gotten from the player\n player_number = 50\n\n # did he guess right?\n if (player_number == number_to_guess) :\n break\n\n # adjust bounds for next guess\n if (player_number < number_to_guess) :\n low_limit = player_number\n \n else :\n high_limit = player_number\n\n print ('Bingo\\n')",
+ "input": [
+ "\n",
+ "# Variable declaration, calculation and result\n",
+ "import sys\n",
+ "import random\n",
+ "while (1) :\n",
+ "\n",
+ " # random number to be guessed\n",
+ " number_to_guess = random.randrange (1, 101, 1)\n",
+ "\n",
+ " # current lower limit of player's range\n",
+ " low_limit = 0\n",
+ "\n",
+ " # current upper limit of player's range\n",
+ " high_limit = 100\n",
+ " \n",
+ " # number of times player guessed\n",
+ " guess_count = 0\n",
+ "\n",
+ " while (1) :\n",
+ " \n",
+ " # tell user what the bounds are and get his guess\n",
+ " print ('Bounds %d - %d\\n' % (low_limit, high_limit))\n",
+ " print ('Value[%d]?' % guess_count)\n",
+ " \n",
+ " guess_count += 1\n",
+ "\n",
+ " # number gotten from the player\n",
+ " player_number = 50\n",
+ "\n",
+ " # did he guess right?\n",
+ " if (player_number == number_to_guess) :\n",
+ " break\n",
+ "\n",
+ " # adjust bounds for next guess\n",
+ " if (player_number < number_to_guess) :\n",
+ " low_limit = player_number\n",
+ " \n",
+ " else :\n",
+ " high_limit = player_number\n",
+ "\n",
+ " print ('Bingo\\n')"
+ ],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Practical_C_Programming/Chapter_8_4.ipynb b/Practical_C_Programming/Chapter_8_4.ipynb
index f7a42df6..24580c55 100644
--- a/Practical_C_Programming/Chapter_8_4.ipynb
+++ b/Practical_C_Programming/Chapter_8_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 8"
+ "name": "",
+ "signature": "sha256:10a432341f6033fcf678cc580d953b0c94ad7350aa1431ba3e0517904d3e90c8"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,46 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 8: More control statements"
+ "source": [
+ "Chapter 8: More control statements"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.1, Page number: 144"
+ "source": [
+ "Example 8.1, Page number: 144"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.1.py\n# To calculate the sum of numbers entered by the user\n\n\n# Variable declaration\ntotal = 0\ncounter = 0\n\n# Calculation\nwhile (counter < 5) :\n current = 3\n total += current\n counter += 1\n\n# Result\nprint ('The grand total is %d' % total)",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "counter = 0\n",
+ "\n",
+ "# Calculation\n",
+ "while (counter < 5) :\n",
+ " current = 3\n",
+ " total += current\n",
+ " counter += 1\n",
+ "\n",
+ "# Result\n",
+ "print ('The grand total is %d' % total)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The grand total is 15\n"
+ "text": [
+ "The grand total is 15\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +60,35 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.2, Page number: 145"
+ "source": [
+ "Example 8.2, Page number: 145"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.2.py\n# To calculate the sum of numbers entered by the user using for loop\n\n\n# Variable declaration\ntotal = 0\n\n# Calculation\nfor counter in range (0, 5) :\n current = 5\n total += current\n\n# Result\nprint ('The grand total is %d' % total)",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "total = 0\n",
+ "\n",
+ "# Calculation\n",
+ "for counter in range (0, 5) :\n",
+ " current = 5\n",
+ " total += current\n",
+ "\n",
+ "# Result\n",
+ "print ('The grand total is %d' % total)"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "The grand total is 25\n"
+ "text": [
+ "The grand total is 25\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +97,129 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.3, Page number: 146"
+ "source": [
+ "Example 8.3, Page number: 146"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.3.py\n# To produce a Celsius to Fahrenheit conversion chart for the numbers 0 to 100\n\n\n# Variable declaration, calculation and result\nfor celsius in range (0, 101) :\n print ('Celsius: %d Fahrenheit: %d' % (celsius, (celsius * 9) / 5 +32))",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration, calculation and result\n",
+ "for celsius in range (0, 101) :\n",
+ " print ('Celsius: %d Fahrenheit: %d' % (celsius, (celsius * 9) / 5 +32))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Celsius: 0 Fahrenheit: 32\nCelsius: 1 Fahrenheit: 33\nCelsius: 2 Fahrenheit: 35\nCelsius: 3 Fahrenheit: 37\nCelsius: 4 Fahrenheit: 39\nCelsius: 5 Fahrenheit: 41\nCelsius: 6 Fahrenheit: 42\nCelsius: 7 Fahrenheit: 44\nCelsius: 8 Fahrenheit: 46\nCelsius: 9 Fahrenheit: 48\nCelsius: 10 Fahrenheit: 50\nCelsius: 11 Fahrenheit: 51\nCelsius: 12 Fahrenheit: 53\nCelsius: 13 Fahrenheit: 55\nCelsius: 14 Fahrenheit: 57\nCelsius: 15 Fahrenheit: 59\nCelsius: 16 Fahrenheit: 60\nCelsius: 17 Fahrenheit: 62\nCelsius: 18 Fahrenheit: 64\nCelsius: 19 Fahrenheit: 66\nCelsius: 20 Fahrenheit: 68\nCelsius: 21 Fahrenheit: 69\nCelsius: 22 Fahrenheit: 71\nCelsius: 23 Fahrenheit: 73\nCelsius: 24 Fahrenheit: 75\nCelsius: 25 Fahrenheit: 77\nCelsius: 26 Fahrenheit: 78\nCelsius: 27 Fahrenheit: 80\nCelsius: 28 Fahrenheit: 82\nCelsius: 29 Fahrenheit: 84\nCelsius: 30 Fahrenheit: 86\nCelsius: 31 Fahrenheit: 87\nCelsius: 32 Fahrenheit: 89\nCelsius: 33 Fahrenheit: 91\nCelsius: 34 Fahrenheit: 93\nCelsius: 35 Fahrenheit: 95\nCelsius: 36 Fahrenheit: 96\nCelsius: 37 Fahrenheit: 98\nCelsius: 38 Fahrenheit: 100\nCelsius: 39 Fahrenheit: 102\nCelsius: 40 Fahrenheit: 104\nCelsius: 41 Fahrenheit: 105\nCelsius: 42 Fahrenheit: 107\nCelsius: 43 Fahrenheit: 109\nCelsius: 44 Fahrenheit: 111\nCelsius: 45 Fahrenheit: 113\nCelsius: 46 Fahrenheit: 114\nCelsius: 47 Fahrenheit: 116\nCelsius: 48 Fahrenheit: 118\nCelsius: 49 Fahrenheit: 120\nCelsius: 50 Fahrenheit: 122\nCelsius: 51 Fahrenheit: 123\nCelsius: 52 Fahrenheit: 125\nCelsius: 53 Fahrenheit: 127\nCelsius: 54 Fahrenheit: 129\nCelsius: 55 Fahrenheit: 131\nCelsius: 56 Fahrenheit: 132\nCelsius: 57 Fahrenheit: 134\nCelsius: 58 Fahrenheit: 136\nCelsius: 59 Fahrenheit: 138\nCelsius: 60 Fahrenheit: 140\nCelsius: 61 Fahrenheit: 141\nCelsius: 62 Fahrenheit: 143\nCelsius: 63 Fahrenheit: 145\nCelsius: 64 Fahrenheit: 147\nCelsius: 65 Fahrenheit: 149\nCelsius: 66 Fahrenheit: 150\nCelsius: 67 Fahrenheit: 152\nCelsius: 68 Fahrenheit: 154\nCelsius: 69 Fahrenheit: 156\nCelsius: 70 Fahrenheit: 158\nCelsius: 71 Fahrenheit: 159\nCelsius: 72 Fahrenheit: 161\nCelsius: 73 Fahrenheit: 163\nCelsius: 74 Fahrenheit: 165\nCelsius: 75 Fahrenheit: 167\nCelsius: 76 Fahrenheit: 168\nCelsius: 77 Fahrenheit: 170\nCelsius: 78 Fahrenheit: 172\nCelsius: 79 Fahrenheit: 174\nCelsius: 80 Fahrenheit: 176\nCelsius: 81 Fahrenheit: 177\nCelsius: 82 Fahrenheit: 179\nCelsius: 83 Fahrenheit: 181\nCelsius: 84 Fahrenheit: 183\nCelsius: 85 Fahrenheit: 185\nCelsius: 86 Fahrenheit: 186\nCelsius: 87 Fahrenheit: 188\nCelsius: 88 Fahrenheit: 190\nCelsius: 89 Fahrenheit: 192\nCelsius: 90 Fahrenheit: 194\nCelsius: 91 Fahrenheit: 195\nCelsius: 92 Fahrenheit: 197\nCelsius: 93 Fahrenheit: 199\nCelsius: 94 Fahrenheit: 201\nCelsius: 95 Fahrenheit: 203\nCelsius: 96 Fahrenheit: 204\nCelsius: 97 Fahrenheit: 206\nCelsius: 98 Fahrenheit: 208\nCelsius: 99 Fahrenheit: 210\nCelsius: 100 Fahrenheit: 212\n"
+ "text": [
+ "Celsius: 0 Fahrenheit: 32\n",
+ "Celsius: 1 Fahrenheit: 33\n",
+ "Celsius: 2 Fahrenheit: 35\n",
+ "Celsius: 3 Fahrenheit: 37\n",
+ "Celsius: 4 Fahrenheit: 39\n",
+ "Celsius: 5 Fahrenheit: 41\n",
+ "Celsius: 6 Fahrenheit: 42\n",
+ "Celsius: 7 Fahrenheit: 44\n",
+ "Celsius: 8 Fahrenheit: 46\n",
+ "Celsius: 9 Fahrenheit: 48\n",
+ "Celsius: 10 Fahrenheit: 50\n",
+ "Celsius: 11 Fahrenheit: 51\n",
+ "Celsius: 12 Fahrenheit: 53\n",
+ "Celsius: 13 Fahrenheit: 55\n",
+ "Celsius: 14 Fahrenheit: 57\n",
+ "Celsius: 15 Fahrenheit: 59\n",
+ "Celsius: 16 Fahrenheit: 60\n",
+ "Celsius: 17 Fahrenheit: 62\n",
+ "Celsius: 18 Fahrenheit: 64\n",
+ "Celsius: 19 Fahrenheit: 66\n",
+ "Celsius: 20 Fahrenheit: 68\n",
+ "Celsius: 21 Fahrenheit: 69\n",
+ "Celsius: 22 Fahrenheit: 71\n",
+ "Celsius: 23 Fahrenheit: 73\n",
+ "Celsius: 24 Fahrenheit: 75\n",
+ "Celsius: 25 Fahrenheit: 77\n",
+ "Celsius: 26 Fahrenheit: 78\n",
+ "Celsius: 27 Fahrenheit: 80\n",
+ "Celsius: 28 Fahrenheit: 82\n",
+ "Celsius: 29 Fahrenheit: 84\n",
+ "Celsius: 30 Fahrenheit: 86\n",
+ "Celsius: 31 Fahrenheit: 87\n",
+ "Celsius: 32 Fahrenheit: 89\n",
+ "Celsius: 33 Fahrenheit: 91\n",
+ "Celsius: 34 Fahrenheit: 93\n",
+ "Celsius: 35 Fahrenheit: 95\n",
+ "Celsius: 36 Fahrenheit: 96\n",
+ "Celsius: 37 Fahrenheit: 98\n",
+ "Celsius: 38 Fahrenheit: 100\n",
+ "Celsius: 39 Fahrenheit: 102\n",
+ "Celsius: 40 Fahrenheit: 104\n",
+ "Celsius: 41 Fahrenheit: 105\n",
+ "Celsius: 42 Fahrenheit: 107\n",
+ "Celsius: 43 Fahrenheit: 109\n",
+ "Celsius: 44 Fahrenheit: 111\n",
+ "Celsius: 45 Fahrenheit: 113\n",
+ "Celsius: 46 Fahrenheit: 114\n",
+ "Celsius: 47 Fahrenheit: 116\n",
+ "Celsius: 48 Fahrenheit: 118\n",
+ "Celsius: 49 Fahrenheit: 120\n",
+ "Celsius: 50 Fahrenheit: 122\n",
+ "Celsius: 51 Fahrenheit: 123\n",
+ "Celsius: 52 Fahrenheit: 125\n",
+ "Celsius: 53 Fahrenheit: 127\n",
+ "Celsius: 54 Fahrenheit: 129\n",
+ "Celsius: 55 Fahrenheit: 131\n",
+ "Celsius: 56 Fahrenheit: 132\n",
+ "Celsius: 57 Fahrenheit: 134\n",
+ "Celsius: 58 Fahrenheit: 136\n",
+ "Celsius: 59 Fahrenheit: 138\n",
+ "Celsius: 60 Fahrenheit: 140\n",
+ "Celsius: 61 Fahrenheit: 141\n",
+ "Celsius: 62 Fahrenheit: 143\n",
+ "Celsius: 63 Fahrenheit: 145\n",
+ "Celsius: 64 Fahrenheit: 147\n",
+ "Celsius: 65 Fahrenheit: 149\n",
+ "Celsius: 66 Fahrenheit: 150\n",
+ "Celsius: 67 Fahrenheit: 152\n",
+ "Celsius: 68 Fahrenheit: 154\n",
+ "Celsius: 69 Fahrenheit: 156\n",
+ "Celsius: 70 Fahrenheit: 158\n",
+ "Celsius: 71 Fahrenheit: 159\n",
+ "Celsius: 72 Fahrenheit: 161\n",
+ "Celsius: 73 Fahrenheit: 163\n",
+ "Celsius: 74 Fahrenheit: 165\n",
+ "Celsius: 75 Fahrenheit: 167\n",
+ "Celsius: 76 Fahrenheit: 168\n",
+ "Celsius: 77 Fahrenheit: 170\n",
+ "Celsius: 78 Fahrenheit: 172\n",
+ "Celsius: 79 Fahrenheit: 174\n",
+ "Celsius: 80 Fahrenheit: 176\n",
+ "Celsius: 81 Fahrenheit: 177\n",
+ "Celsius: 82 Fahrenheit: 179\n",
+ "Celsius: 83 Fahrenheit: 181\n",
+ "Celsius: 84 Fahrenheit: 183\n",
+ "Celsius: 85 Fahrenheit: 185\n",
+ "Celsius: 86 Fahrenheit: 186\n",
+ "Celsius: 87 Fahrenheit: 188\n",
+ "Celsius: 88 Fahrenheit: 190\n",
+ "Celsius: 89 Fahrenheit: 192\n",
+ "Celsius: 90 Fahrenheit: 194\n",
+ "Celsius: 91 Fahrenheit: 195\n",
+ "Celsius: 92 Fahrenheit: 197\n",
+ "Celsius: 93 Fahrenheit: 199\n",
+ "Celsius: 94 Fahrenheit: 201\n",
+ "Celsius: 95 Fahrenheit: 203\n",
+ "Celsius: 96 Fahrenheit: 204\n",
+ "Celsius: 97 Fahrenheit: 206\n",
+ "Celsius: 98 Fahrenheit: 208\n",
+ "Celsius: 99 Fahrenheit: 210\n",
+ "Celsius: 100 Fahrenheit: 212\n"
+ ]
}
],
"prompt_number": 3
@@ -80,19 +228,46 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.4, Page number: 147"
+ "source": [
+ "Example 8.4, Page number: 147"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.4.py\n# To count the number of 3's and 7's in an array\n\n\n# Variable declaration\nseven_count = 0\nthree_count = 0\ndata = []\n\n# Calculation\nfor i in range (0, 5) :\n x = 7\n data.append(int(x))\nprint (data)\n\nfor index in range (0, 5) :\n if data[index] == 3 :\n three_count += 1\n\n if data[index] == 7 :\n seven_count += 1\n\n# Result\nprint ('Threes %d Sevens %d' % (three_count, seven_count))",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "seven_count = 0\n",
+ "three_count = 0\n",
+ "data = []\n",
+ "\n",
+ "# Calculation\n",
+ "for i in range (0, 5) :\n",
+ " x = 7\n",
+ " data.append(int(x))\n",
+ "print (data)\n",
+ "\n",
+ "for index in range (0, 5) :\n",
+ " if data[index] == 3 :\n",
+ " three_count += 1\n",
+ "\n",
+ " if data[index] == 7 :\n",
+ " seven_count += 1\n",
+ "\n",
+ "# Result\n",
+ "print ('Threes %d Sevens %d' % (three_count, seven_count))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "[7, 7, 7, 7, 7]\nThrees 0 Sevens 5\n"
+ "text": [
+ "[7, 7, 7, 7, 7]\n",
+ "Threes 0 Sevens 5\n"
+ ]
}
],
"prompt_number": 4
@@ -101,19 +276,60 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 8.6, Page number: 149"
+ "source": [
+ "Example 8.6, Page number: 149"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 8.6.py\n# To perform various arithmetic operations based on operator input by the user\n\n\n# Variable declaration\nresult = 0\ni = 0\n\n# Calculation and result\nwhile (i < 3) :\n print ('Result: %d' % result)\n operator = '-'\n value = 10\n\n if operator == 'q' or operator == 'Q' :\n break\n\n elif operator == '+' :\n result += value\n\n elif operator == '-' :\n result -= value\n\n elif operator == '*' :\n result *= value\n\n elif operator == '/' :\n if value == 0 :\n print ('Error: Divide by zero')\n print ('operation ignored') \n else :\n result /= value\n\n else :\n print ('Unknown operator %c' % operator)\n i = i + 1",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "result = 0\n",
+ "i = 0\n",
+ "\n",
+ "# Calculation and result\n",
+ "while (i < 3) :\n",
+ " print ('Result: %d' % result)\n",
+ " operator = '-'\n",
+ " value = 10\n",
+ "\n",
+ " if operator == 'q' or operator == 'Q' :\n",
+ " break\n",
+ "\n",
+ " elif operator == '+' :\n",
+ " result += value\n",
+ "\n",
+ " elif operator == '-' :\n",
+ " result -= value\n",
+ "\n",
+ " elif operator == '*' :\n",
+ " result *= value\n",
+ "\n",
+ " elif operator == '/' :\n",
+ " if value == 0 :\n",
+ " print ('Error: Divide by zero')\n",
+ " print ('operation ignored') \n",
+ " else :\n",
+ " result /= value\n",
+ "\n",
+ " else :\n",
+ " print ('Unknown operator %c' % operator)\n",
+ " i = i + 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Result: 0\nResult: -10\nResult: -20\n"
+ "text": [
+ "Result: 0\n",
+ "Result: -10\n",
+ "Result: -20\n"
+ ]
}
],
"prompt_number": 5
@@ -121,7 +337,7 @@
{
"cell_type": "code",
"collapsed": false,
- "input": "",
+ "input": [],
"language": "python",
"metadata": {},
"outputs": []
diff --git a/Practical_C_Programming/Chapter_9_4.ipynb b/Practical_C_Programming/Chapter_9_4.ipynb
index 28d47037..8d18a77b 100644
--- a/Practical_C_Programming/Chapter_9_4.ipynb
+++ b/Practical_C_Programming/Chapter_9_4.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 9"
+ "name": "",
+ "signature": "sha256:312f9e5b3211fd50ff3dd7c27b81824ed2a8d6172aff8fb27752ffb6ee205caa"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,25 +12,47 @@
"cell_type": "heading",
"level": 1,
"metadata": {},
- "source": "Chapter 9: Variable scope and functions"
+ "source": [
+ "Chapter 9: Variable scope and functions"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.1, Page number: 160"
+ "source": [
+ "Example 9.1, Page number: 160"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 9.1.py\n# To illustrate the difference between temporary and permanent variables\n\n\n# Function declaration, calculation and result\ndef func() :\n if not hasattr(func, \"permanent\") :\n func.permanent = 1\n result = func.permanent\n func.permanent += 1\n return result\n\nfor counter in range (0, 3) :\n temporary = 1\n print ('Temporary %d Permanent %d' % (temporary, func()))\n temporary += 1",
+ "input": [
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def func() :\n",
+ " if not hasattr(func, \"permanent\") :\n",
+ " func.permanent = 1\n",
+ " result = func.permanent\n",
+ " func.permanent += 1\n",
+ " return result\n",
+ "\n",
+ "for counter in range (0, 3) :\n",
+ " temporary = 1\n",
+ " print ('Temporary %d Permanent %d' % (temporary, func()))\n",
+ " temporary += 1"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Temporary 1 Permanent 1\nTemporary 1 Permanent 2\nTemporary 1 Permanent 3\n"
+ "text": [
+ "Temporary 1 Permanent 1\n",
+ "Temporary 1 Permanent 2\n",
+ "Temporary 1 Permanent 3\n"
+ ]
}
],
"prompt_number": 1
@@ -38,19 +61,36 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.3, Page number: 164"
+ "source": [
+ "Example 9.3, Page number: 164"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 9.3.py\n# To compute the area of a triangle\n\n\n# Function declaration, calculation and result\ndef triangle (width, height) :\n area = width * height / 2.0\n return area\n\nprint ('Triangle #1 %f' % (triangle (1.3, 8.3)))\nprint ('Triangle #2 %f' % (triangle (4.8, 9.8)))\nprint ('Triangle #3 %f' % (triangle (1.2, 2.0)))",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def triangle (width, height) :\n",
+ " area = width * height / 2.0\n",
+ " return area\n",
+ "\n",
+ "print ('Triangle #1 %f' % (triangle (1.3, 8.3)))\n",
+ "print ('Triangle #2 %f' % (triangle (4.8, 9.8)))\n",
+ "print ('Triangle #3 %f' % (triangle (1.2, 2.0)))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Triangle #1 5.395000\nTriangle #2 23.520000\nTriangle #3 1.200000\n"
+ "text": [
+ "Triangle #1 5.395000\n",
+ "Triangle #2 23.520000\n",
+ "Triangle #3 1.200000\n"
+ ]
}
],
"prompt_number": 2
@@ -59,19 +99,32 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.4, Page number: 166"
+ "source": [
+ "Example 9.4, Page number: 166"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 9.4.py\n# To compute the length of a string\n\n\n# Function declaration, calculation and result\ndef length (string) :\n return len(string)\n\nline = 'hello world' \n\nprint ('Length is: %d' % length(line))",
+ "input": [
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def length (string) :\n",
+ " return len(string)\n",
+ "\n",
+ "line = 'hello world' \n",
+ "\n",
+ "print ('Length is: %d' % length(line))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Length is: 11\n"
+ "text": [
+ "Length is: 11\n"
+ ]
}
],
"prompt_number": 4
@@ -80,19 +133,33 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 9.6, Page number: 170"
+ "source": [
+ "Example 9.6, Page number: 170"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 9.6.py\n# To compute the length of a string\n\n\n# Function declaration, calculation and result\ndef length (string) :\n return len(string)\n\nline = 'Steve Oualline'\n\nprint ('Length is: %d' % length(line))",
+ "input": [
+ "\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def length (string) :\n",
+ " return len(string)\n",
+ "\n",
+ "line = 'Steve Oualline'\n",
+ "\n",
+ "print ('Length is: %d' % length(line))"
+ ],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
- "text": "Length is: 14\n"
+ "text": [
+ "Length is: 14\n"
+ ]
}
],
"prompt_number": 5