diff options
Diffstat (limited to 'ANSI_C_Programming/chapter11.ipynb')
-rw-r--r-- | ANSI_C_Programming/chapter11.ipynb | 533 |
1 files changed, 533 insertions, 0 deletions
diff --git a/ANSI_C_Programming/chapter11.ipynb b/ANSI_C_Programming/chapter11.ipynb new file mode 100644 index 00000000..4d9a0fc1 --- /dev/null +++ b/ANSI_C_Programming/chapter11.ipynb @@ -0,0 +1,533 @@ +{
+ "metadata": {
+ "name": "chapter11.ipynb"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "CHAPTER 11: CONSOLE INPUT/OUTPUT"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:372"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program explaining the interpretation of contents of format string\n",
+ "avg=346\n",
+ "per=69.2\n",
+ "print \"Average=%d\\nPercentage=%f\\n\" % (avg,per)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average=346\n",
+ "Percentage=69.200000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:374"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program explaining field-width of format specifiers\n",
+ "weight=63\n",
+ "print \"weight is %d kg\\n\" % (weight)\n",
+ "print \"weight is %2d kg\\n\" % (weight)\n",
+ "print \"weight is %4d kg\\n\" % (weight)\n",
+ "print \"weight is %6d kg\\n\" % (weight)\n",
+ "print \"weight is %-6d kg\\n\" % (weight)\n",
+ "print \"weight is %1d kg\\n\" % (weight)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "weight is 63 kg\n",
+ "\n",
+ "weight is 63 kg\n",
+ "\n",
+ "weight is 63 kg\n",
+ "\n",
+ "weight is 63 kg\n",
+ "\n",
+ "weight is 63 kg\n",
+ "\n",
+ "weight is 63 kg\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:374-375"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program without using field-width\n",
+ "print \"%f%f%f\\n\" % (5.0,13.5,133.9)\n",
+ "print \"%f%f%f\\n\" % (305.0,1200.9,3005.3)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5.00000013.500000133.900000\n",
+ "\n",
+ "305.0000001200.9000003005.300000\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:375"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program using field-width to line-up properly\n",
+ "print \"%10.1f%10.1f%10.1f\\n\" % (5.0,13.5,133.9)\n",
+ "print \"%10.1f%10.1f%10.1f\\n\" % (305.0,1200.9,3005.3)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 5.0 13.5 133.9\n",
+ "\n",
+ " 305.0 1200.9 3005.3\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:375"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Formatting strings with printf()\n",
+ "firstname1=\"Sandy\"\n",
+ "surname1=\"Malya\"\n",
+ "firstname2=\"AjayKumar\"\n",
+ "surname2=\"Gurubaxani\"\n",
+ "print \"%20s%20s\\n\" % (firstname1,surname1)\n",
+ "print \"%20s%20s\\n\" % (firstname2,surname2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Sandy Malya\n",
+ "\n",
+ " AjayKumar Gurubaxani\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:376"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of escape sequences like tab and newlines\n",
+ "print \"You\\tmust\\tbe\\tcrazy\\nto\\thate\\tthis\\tbook\\n\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You\tmust\tbe\tcrazy\n",
+ "to\thate\tthis\tbook\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:377-378"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#using different format specifiers in print function\n",
+ "ch='z'\n",
+ "i=125\n",
+ "a=12.55\n",
+ "s=\"hello there!\"\n",
+ "print \"%c %d %f\\n\" % (ch,ord(ch),ord(ch))\n",
+ "print \"%s\\n\" % (s) #here conversation not possible\n",
+ "print \"%c %d %f\\n\" % (i,i,i)\n",
+ "print \"%f %d\\n\" % (a,a)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "z 122 122.000000\n",
+ "\n",
+ "hello there!\n",
+ "\n",
+ "} 125 125.000000\n",
+ "\n",
+ "12.550000 12\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#assigning elements to an array\n",
+ "i=10\n",
+ "ch='A'\n",
+ "a=3.14\n",
+ "print \"%d %c %f\\n\" % (i,ch,a)\n",
+ "str=[i,ch,a]\n",
+ "print \"%s\\n\" % (str)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "10 A 3.140000\n",
+ "\n",
+ "[10, 'A', 3.14]\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:380"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#use of input function\n",
+ "print \"Press any key to continue\"\n",
+ "raw_input() #will echo the character\n",
+ "print \"\\nType any character\"\n",
+ "ch=raw_input() #will echo the character typed\n",
+ "print \"\\nType any character\"\n",
+ "raw_input() #will echo character\n",
+ "print \"\\nContinue Y/N\"\n",
+ "raw_input() #will echo character"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Press any key to continue\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " \n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Type any character\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "B\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Type any character\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "W\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Continue Y/N\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Y\n"
+ ]
+ },
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 11,
+ "text": [
+ "'Y'"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:381"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#Use of print function\n",
+ "ch='A'\n",
+ "print ch,\n",
+ "print(ch),\n",
+ "print ch,\n",
+ "print 'Z',\n",
+ "print('Z'),\n",
+ "print'Z',"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A A A Z Z Z\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:381"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program to input a string of characters\n",
+ "print \"Enter name\"\n",
+ "name=raw_input()\n",
+ "print \"%s\\n\" % (name)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jonty Rhodes\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jonty Rhodes\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "EXAMPLE ON PAGE:382"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#program to input a string of characters\n",
+ "print \"Enter name\"\n",
+ "footballer=raw_input() #sends base address of array\n",
+ "print \"Happy footballing!\"\n",
+ "print footballer"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter name\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jonty Rhodes\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Happy footballing!\n",
+ "Jonty Rhodes\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |