正在加载图片...
8 Introduction programming expertise.you will become more efficient and productive,and you'll be able to apply your new skills to other problems. 1.6 Being as Clear as Possible About What Your Program Should Do If you decide that you need a r ram and that you should write it yourself,you will need to he as clea ssible abou should do This is r said than done Thinking through the workings of a p ects of program- ming.even harder in some cases than getting the syntax right. Return to the problem of finding the largest value in an array.It turns out that MATLAB provides a program (or more precisely,a fiunction).called max,that lets you find the maximum of a set of values (see Chapter 4).You can use this function to get the larg- est value in a matrix without having to reinvent the function yourself.Nevertheless,it is worth thinking through the way you would identify the largest value in an array.Work- ing through this example-however simple it may seem-will help you begin to"think programmatically. To think through what a program must do to find the largest value in an array of numbers imagine that you have a row of numbers like the one above,but you can only see one of the numbers at a time say,by sliding the hole in a card across the row.Under this circum- stance,you can determi e the larges valu value so far.I you wer actually d first place in the card over th Irst nu which is that 7 e you to revea rger than now you d te that 33 umb seen,and you' for the rest w time y value larger than la est value so far.afte list,Largest_Far will be the largest of all the values. Here is a flow chart for the procedure,along with some other items you'd need to get the job done.One of these other items is telling the program how many values there are in the list.We give the list the name V.There aren=9 values in V. Another thing that needs to be done is initializing Largest value so Far to an extremely small value,namely,minus infinity (which can be expressed in MATLAB as -inf).We do this because whenever a new number is tested,it must be compared to some prior value.Starting with-inf ensures that the first value will be called the largest pro- vided it is larger than-inf.It may stay that way if no larger value comes along. The third thing that needs to be done is providing an index,i.for each successively encountered value in V.An index for a value is the position of the value in the matrix For the first item,i-1,for the second item,i=2,and so forth.Initially,i is set to 0.Each time a new number is compared to Largest value so Far,the variable d by 1,until i is greater thar i-th va V enoted 1 is assigned to Largest value so Far if v(i)is larger than the current value o 8 Introduction programming expertise, you will become more efficient and productive, and youÊll be able to apply your new skills to other problems. 1.6 Being as Clear as Possible About What Your Program Should Do If you decide that you need a program and that you should write it yourself, you will need to be as clear as possible about what your program should do. This is easier said than done. Thinking through the workings of a program can be one of the hardest aspects of program￾ming, even harder in some cases than getting the syntax right. Return to the problem of finding the largest value in an array. It turns out that MATLAB provides a program (or more precisely, a function ), called max , that lets you find the maximum of a set of values (see Chapter 4). You can use this function to get the larg￾est value in a matrix without having to reinvent the function yourself. Nevertheless, it is worth thinking through the way you would identify the largest value in an array. Work￾ing through this example·however simple it may seem·will help you begin to „think programmatically.‰ To think through what a program must do to find the largest value in an array of numbers, imagine that you have a row of numbers like the one above, but you can only see one of the numbers at a time·say, by sliding the hole in a card across the row. Under this circum￾stance, you can determine the largest value by finding the largest value so far . If you were actually doing this, youÊd first place the hole in the card over the first number, which is 7. Then, youÊd remember that 7 is the largest value youÊve seen, and move the card to reveal the 33. Thirty-three is larger than 7, so now youÊd note that 33 is the largest number youÊve seen, and youÊd move the card again. After seeing 39, you would revise the largest number seen to that value. Continuing and not encountering any number larger than 39 for the rest of the series, that would be the number youÊd report. Now translate this algorithm into a program. Assign some very small value to a variable called, for instance, Largest_Value_So_Far . Then, proceeding from left to right, every time you encounter a value larger than Largest_Value_So_Far, reassign that new value to Largest_Value_So_Far . After you have evaluated the last item on the list, Largest_Value_So_Far will be the largest of all the values. Here is a flow chart for the procedure, along with some other items youÊd need to get the job done. One of these other items is telling the program how many values there are in the list. We give the list the name V . There are n = 9 values in V . Another thing that needs to be done is initializing Largest_Value_So_Far to an extremely small value, namely, minus infinity (which can be expressed in MATLAB as –inf ). We do this because whenever a new number is tested, it must be compared to some prior value. Starting with –inf ensures that the first value will be called the largest pro￾vided it is larger than –inf . It may stay that way if no larger value comes along. The third thing that needs to be done is providing an index, i , for each successively encountered value in V . An index for a value is the position of the value in the matrix. For the first item, i = 1 , for the second item, i = 2 , and so forth. Initially, i is set to 0 . Each time a new number is compared to Largest_Value_So_Far, the variable i is incremented by 1, until i is greater than n . The i -th value of V , denoted V(i) , is assigned to Largest_Value_So_Far if V(i) is larger than the current value of
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有