summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_10_1.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming/Chapter_10_1.ipynb')
-rw-r--r--Practical_C_Programming/Chapter_10_1.ipynb259
1 files changed, 227 insertions, 32 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