19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

You start by initializing the sum variable (which holds the running<br />

sum) <strong>to</strong> 0 (line 2). You then use a nested loop <strong>to</strong> iterate over all the rows<br />

and columns (lines 3–4). For each iteration, you add the number s<strong>to</strong>red in<br />

sales[r][c] <strong>to</strong> sum (line 5). When the outer loop ends, you display the result<br />

followed by K for thousands (line 8).<br />

Add this subroutine <strong>to</strong> the program, and then add a statement <strong>to</strong> call it.<br />

Here’s what you should see when you call the TotalSales() subroutine:<br />

Total Sales: $1340 K<br />

Step 2: Find the Sum of Each Column<br />

Donald also wants <strong>to</strong> see the <strong>to</strong>tal sales for each Duckberg Industries product.<br />

He needs <strong>to</strong> compare these numbers <strong>with</strong> those from his competi<strong>to</strong>rs<br />

<strong>to</strong> assess his company’s market share.<br />

To give Donald this information, you’ll use the ColumnSum() subroutine<br />

in Listing 17-6 <strong>to</strong> compute the sum of each column in the sales matrix.<br />

1 Sub ColumnSum<br />

2 For c = 1 To COLS ' For each column<br />

3 sum = 0 ' Initializes the sum for column c<br />

4 For r = 1 To ROWS ' Iterates over the rows<br />

5 sum = sum + sales[r][c] ' Adds number at row r, column c<br />

6 EndFor<br />

7 colName = product[c] + " Sales: $" ' Name <strong>to</strong> display<br />

8 TextWindow.WriteLine(colName + sum + " K")<br />

9 EndFor<br />

10 EndSub<br />

Listing 17-6: The ColumnSum() subroutine<br />

You start the outer loop <strong>to</strong> iterate over the five columns (line 2). For<br />

each column (each value of c), you initialize the column’s sum <strong>to</strong> 0 (line 3)<br />

and then start a For loop <strong>to</strong> add the numbers from all the rows in that column<br />

<strong>to</strong> sum (lines 4–6). When the inner loop completes, you get the name<br />

of the current product (from product[c]), append "Sales: $" <strong>to</strong> it, and save<br />

the resulting string in colName (line 7). In line 8, you display that string followed<br />

by the sum you just computed. The outer loop then restarts <strong>to</strong> find<br />

and display the sum for the next column.<br />

Add this subroutine <strong>to</strong> the program, and then add a statement <strong>to</strong> call it.<br />

Here’s what you should see when you call the ColumnSum() subroutine:<br />

eShoes Sales: $190 K<br />

iShirt Sales: $200 K<br />

Shampoop Sales: $310 K<br />

dWater Sales: $330 K<br />

iHat Sales: $310 K<br />

Expanding <strong>to</strong> Higher-Dimension Arrays 251

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!