📄 page_715.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_715</title> <link rel="stylesheet" href="reset.css" type="text/css" media="all"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <table summary="top nav" border="0" width="100%"> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_714.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_715</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_716.html">next page ></a></td> </tr> <tr> <td align="left" colspan="3" style="background: #ffffff; padding: 20px;"> <table border="0" width="100%" cellpadding="0"><tr><td align="center"> <table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="left"></td> <td align="right"></td> </tr></table></td></tr><tr><td align="left"><p></p><table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td align="right"><font face="Times New Roman, Times, Serif" size="2" color="#FF0000">Page 715</font></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">for聽(row聽=聽2;聽row聽<聽4;聽row++)<br />{<br />聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽//聽聽聽聽聽Rows聽2聽through聽row1聽have聽been聽summed聽and聽printed<br />聽聽聽聽聽聽聽聽//聽&&聽2聽<=聽row聽<=聽4<br /><br />聽聽聽聽total聽=聽0;<br />聽聽聽聽for聽(col聽=聽0;聽col聽<聽NUM_COLS;聽col++)<br /><br />聽聽聽聽聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽聽聽聽聽total聽==聽table[row][0]聽+聽聽+聽table[row][col-1]<br />聽聽聽聽聽聽聽聽聽聽聽聽//聽&&聽0聽<=聽col聽<=聽NUM_COLS<br /><br />聽聽聽聽聽聽聽聽total聽=聽total聽+聽table[row][col];<br />聽聽聽聽cout聽<<聽Row聽sum:聽聽<<聽total聽<<聽endl;<br />}</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">The second approach is shorter, but its real advantage is that we can easily modify it to process any range of rows.</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">The outer loop controls the rows, and the inner loop controls the columns. For each value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">row</font><font face="Times New Roman, Times, Serif" size="3">, every column is processed; then the outer loop moves to the next row. In the first iteration of the outer loop, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">row</font><font face="Times New Roman, Times, Serif" size="3"> is held at 2 and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">col</font><font face="Times New Roman, Times, Serif" size="3"> goes from 0 through </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">NUM_COLS-1</font><font face="Times New Roman, Times, Serif" size="3">. Therefore, the array is accessed in the following order:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">table[2][0]聽聽[2][1]聽聽[2][2]聽聽[2][3]聽聽聽聽[2][NUM_COLS-1]</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">In the second iteration of the outer loop, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">row</font><font face="Times New Roman, Times, Serif" size="3"> is incremented to 3, and the array is accessed as follows:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">table[3][0]聽聽[3][1]聽聽[3][2]聽聽[3][3]聽聽聽聽[3][NUM_COLS-1]</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Times New Roman, Times, Serif" size="3">We can generalize this row processing to run through every row of the table by having the outer loop run from 0 through </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">NUM_ROWS-1</font><font face="Times New Roman, Times, Serif" size="3">. However, if we want to access only part of the array (subarray processing), we write the code fragment as follows:</font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table><table border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="5"></td> <td colspan="3" height="12"></td> <td rowspan="5"></td></tr><tr><td colspan="3"></td></tr><tr><td></td> <td><font face="Courier New, Courier, Mono New, Courier, Mono" size="2">for聽(row聽=聽0;聽row聽<聽rowLength;聽row++)<br />{<br />聽聽聽聽聽聽聽聽//聽Invariant聽(prior聽to聽test):<br />聽聽聽聽聽聽聽聽//聽聽聽聽聽Rows聽0聽through聽row1聽have聽been聽summed聽and聽printed<br />聽聽聽聽聽聽聽聽//聽聽&&聽0聽<=聽row聽<=聽rowLength<br /></font></td><td></td></tr><tr><td colspan="3"></td></tr><tr><td colspan="3" height="1"></td></tr></table></td></tr></table><p><font size="0"></font></p>聽 </td> </tr> <tr> <td align="left" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_714.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_715</strong></td> <td align="right" width="30%" style="background: #EEF3E2"><a style="color: blue; font-size: 120%; font-weight: bold; text-decoration: none; font-family: verdana;" href="page_716.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -