Skip to content

Commit bf922d3

Browse files
committed
added data block for datafile
1 parent 297ccda commit bf922d3

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void main() {
528528
</p>
529529

530530
<listing xml:id="python-histogram">
531-
<program interactive="activecode" language="python">
531+
<program interactive="activecode" language="python" add-files="datafile-test">
532532
<code>
533533
def main():
534534
count = [0]*10
@@ -545,9 +545,11 @@ main()
545545
</listing>
546546

547547
<p>
548-
Test running the program. It will read this data:
548+
Test running the program. It will read <xref ref="datafile-test" text="type-global"/>.
549549
</p>
550-
550+
551+
<data xml:id="datafile-test">
552+
<title>Data file for testing the histogram program</title>
551553
<datafile label="datafile-test" filename="test.dat" editable="no" cols="12" rows="6">
552554
<pre>
553555
1
@@ -557,6 +559,7 @@ main()
557559
1
558560
</pre>
559561
</datafile>
562+
</data>
560563

561564
<p>
562565
Lets review what is happening in this little program. First, we create a list and initialize the first 10 positions in the list to be 0. Next we open the data file called &#x2018;test.dat&#x2019;. Third, we have a loop that reads each line of the file. As we read each line we convert it to an integer and increment the counter at the position in the list indicated by the number on the line we just read. Finally we iterate over each element in the list, printing out both the position in the list and the total value stored in that position.

0 commit comments

Comments
 (0)