Tweet |
Custom Search
|
|
Sample Problem Using Nested Loops and a Matrix Suppose you wanted to write a program to compute your career sea pay based on your paygrade and years of sea duty. You would need a two-dimensional array (matrix) to store the data. Figure 5-1 shows the table of values needed to determine sea pay.Examine the following program and see how the matrix is constructed. The program contains nested loops (lines 20-60) which are used to read the values into the matrix. The outer loop sets up the row portion, G, which represents paygrade. The inner loop sets up the column portion, S, which represents the years of sea duty.Figure 5-1.Sea-pay table. Example: As seen in the output from this program, an E-8 with 7 years sea duty would receive $285.00 sea pay. Try the program and see what your sea pay would be.The paygrade (4-9) is entered (line 100). Before it can be used as a subscript to determine row number, we subtract 3 (line 110). This makes it correspond to row number 1-6. Next, years of sea duty (1-12) are entered to be used as the subscript for column number. Then the PRINT statement (line 140) prints the corresponding value of the coordinates G and S from the matrix named P. |
||