diff options
Diffstat (limited to 'Practical_C_Programming/Chapter_5_4.ipynb')
-rw-r--r-- | Practical_C_Programming/Chapter_5_4.ipynb | 238 |
1 files changed, 206 insertions, 32 deletions
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 |