summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_13_1.ipynb
blob: 95603acb76c9b49df36a1e94575e866fa6b01288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
 "metadata": {
  "name": "Chapter 13"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": "Chapter 13: Simple pointers"
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Thing 2\nThing 3\n"
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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",
     "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"
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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]))",
     "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"
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Number of elements before zero 6\n\n"
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Number of elements before zero 6\n\n"
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n"
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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))",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "First: Steve Last: Oualline\n\n"
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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())",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Name: tmp1\n\n"
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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()",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SystemExit",
       "evalue": "1",
       "output_type": "pyerr",
       "traceback": [
        "An exception has occurred, use %tb to see the full traceback.\n",
        "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
       ]
      },
      {
       "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"
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": "To exit: use 'exit', 'quit', or Ctrl-D."
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "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)",
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": "Name1: tmp1\n\nName2: tmp2\n\n"
      }
     ],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}