📄 page_293.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>page_293</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_292.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_293</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_294.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 293</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="Times New Roman, Times, Serif" size="3">Output:<br />The temperatures (echo print)<br />The day's high temperature<br />The day's low temperature</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">Discussion: This problem is easy to do by hand. We simply scan the list, looking for the highest and lowest values. How do we simulate this process in an algorithm? Well, let's look carefully at what we actually are doing.</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">To find the largest number in a list of numbers, we compare the first with the second and remember which one is larger. Then we compare that number with the third one, remembering the larger number. We repeat the process until we run out of numbers. The one we remember is the largest. We use the same process to find the smallest number, only we remember the smaller number instead of the larger one.</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">Now that we understand the process, we can design an algorithm for it:</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">1. <i>What is the condition that ends the loop?</i> Because there will be exactly 24 values in the list, we can use a counter to control the loop. When it exceeds 24, the loop exits.</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">2. <i>How should the condition be initialized?</i> The counter should be set to 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">3. <i>How should the condition be updated?</i> The counter should be incremented at the end of each iteration.</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">4. <i>What is the process being repeated?</i> The process reads a value, echoprints it, and checks to see if it should replace the current high or low value.</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">5. <i>How should the process be initialized?</i> In other words, what values should the first number be compared to? We have to give the variables high and low starting values that are to change immediately. So we set high to the smallest number possible (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MIN</font><font face="Times New Roman, Times, Serif" size="3">), and wesew low to the greatest number possible (</font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MAX</font><font face="Times New Roman, Times, Serif" size="3">). In this way, the first temperature read is less than low and greater than high and replaces the values in each. (If your C++ system doesn't supply the header file </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">limits.h</font><font face="Times New Roman, Times, Serif" size="3">, in which </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MIN</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">INT_MAX</font><font face="Times New Roman, Times, Serif" size="3"> are defined, you could use numbers like 30000 and -30000 instead.)</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">6. <i>How should the process be updated?</i> In each iteration, a new temperature is input and compared with high and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3">. If it exceeds </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">high</font><font face="Times New Roman, Times, Serif" size="3">, it replaces the old value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">high</font><font face="Times New Roman, Times, Serif" size="3">. If it is less than </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3">, it replaces the old value of </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3">. Otherwise, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">high</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3"> are unchanged. This tells us that the loop contains two If-Then structures, one each for comparing the input value against </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">high</font><font face="Times New Roman, Times, Serif" size="3"> and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3">.</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">7. <i>What is the state of the program on exiting the loop?</i> Twenty-four temperature values have been input and echo-printed. The loop control variable equals 25, </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">high</font><font face="Times New Roman, Times, Serif" size="3"> contains the largest of the input values, and </font><font face="Courier New, Courier, Mono New, Courier, Mono" size="3">low</font><font face="Times New Roman, Times, Serif" size="3"> contains the smallest.</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_292.html">< previous page</a></td> <td align="center" width="40%" style="background: #EEF3E2"><strong style="color: #2F4F4F; font-size: 120%;">page_293</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_294.html">next page ></a></td> </tr> </table> </body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -