1、Lecture 3 前18分钟Lecture 3 后17分钟Lecture 4 前18分钟Lecture 4 后18分钟Lecture 5 前17分钟Lecture 5 后15分钟Lecture 6 前19分钟Lecture 6 后15分钟Lecture7Lecture 8 前15分钟Lecture 8第1530分钟Lecture 8第3048分钟Lecture 8第48完Lecture 2前17分钟 回首页This is computer programming 1 from the University of Washington. Welcome back! Today in lectu
2、re 2, well be going right into the technical content of this course as we talk about problems, algorithms and programs. As you may be aware, programming a computer involves a great amount of detail. Well have plenty of time later in this course to get too many of those details. But programming also
3、involves higher level concepts in problem solving skills. Id like to focus on that today, later on well see details which will help us carry out these higher level problem solving strategies. So well talk today about what problems might be solved by computers, what algorithms are and what programs a
4、re. Well about a general approach to solving problems with computers and discuss how are solutions can be turned into code that a CPU can actually execute, well focus today on these large ideas. To start with, computer programming is primarily a matter of using a computer to solve a problem that mig
5、ht be one definition of programming, problem solving through computers. Now not all problems that we refer to by that name in everyday use might be amenable to solution by computer. Problem of traffic jams during rush hour for example, probably cant be solved by writing a c program, and the same goe
6、s for problems like world peace or world hunger. Heart of this art programming is be able to recognize which problems are suitable for solutions. In general, a problem that a computer can solve has to be one for which you as a programmer can come up with an algorithm. An algorithm is a sequence of p
7、recise steps which will lead the solution of the problem. If you cant come up with the algorithm or sequence of steps, then you cant use a computer to solve the problem. These steps have to be quit precise and they have to be mechanical in the sense that they will be carried out by a machine, the co
8、mputer once the programming is done, rather than by human being. Notion of the algorithm is a fundamental one and it perhaps quite distinguishes computer science from other fields of science and engineering. Certainly algorithms are used in many other fields, but no worry exception computer science
9、is the algorithm itself of notion, nature natural characteristics keep abilities of algorithms of central area of study. If you can come up with an algorithm, then the next step is turn that algorithm into a computer program, a lot of this course will be about that process taking the algorithm and t
10、urn it into a program that can be executed. Now we express these algorithms using what we call a programming language, this is a special notation that we will be able to translate into a form that the CPU itself will be able to execute. Our course focuses on the C language, there are many other prog
11、ramming languages that you might heard several of them, C+, Java, both of which are quit closely related to C and derive(起源) tran it, others FORTRAN, Basic you may have experience with. Lets compare programming to another kind of algorithm-make activity that were familiar with from everyday life, na
12、mely cooking. So lets take a typical problem, you are hungry, you need to eat something, Fudge brownie sounds really good. So here is the problem: you want to make some Fudge brownie. OK, that course bounds(跳跃) to the statement of the problem, the goal: make Fudge brownie, whats the algorithm? Well,
13、 in cooking we call that a recipe, a recipe is something like open two bags of chocolate chips put them in the bowl, add two tablespoons of flour ,stir(搅拌) them together and bake them in the oven for 3 hours, or whatever the algorithm is, you may know more about cooking than I do. OK, thats the algo
14、rithm known, the step by step recipe that tells what things to do in what order. Now is that quit like a program? Well, not quit, when we write a program, we will be given(?) it to a particular computer to execute and we have to write it in a language and notations that the computer understands. Jus
15、t like a recipe has to be written in a language that the (should be an a here) cook understands. OK, so I may know these steps to go through any ingredients but Ive got to say that in English, in Russian (or lan 口误) or in whatever the language the cook is, youve got to use the right terminology, the
16、 right operations and do everything in exactly the right order but not end up with Fudge brownie. OK, and I still be hungry and I want to solve my problem.Here is its procedure that many people find useful as a general strategy for solving problems with a computer. The first step may be obvious but
17、its for stating, its to specify what the problem is. Now, although its obvious, its often hard to do. Problem that you are given to solve by client, customer, a teacher are sometimes vague(含糊不清的) and may take considerable(这里应该是个类似“额外的”意思的词) amount of the next step of process, analysis. Studying the
18、problem, understand what it really means, what really sort, learning more about it, as much as you can find out about it, eventually from that study, you hope you can design an algorithm step by step procedure for designing the problem(此句有误,后面是补充。), designing the solution to the problems. Once youve
19、 got the algorithm firmly in mind, you can then implement the algorithm, this means writing a program. Now its really only at this state you need knowledge of the programming language. So you can do the problem transactions, the analysis, the design without knowing whether you were going to use C or
20、 Java (?) or FORTRAN, and then at the implantation stage begin translating that analysis and algorithm into the programming language your computer will use. Are you done yet? Well, it would be nice if that was the last step, but its very easy to make mistakes somewhere in this process so the program
21、 that you write has to be tested, verified and in doing so you may find out it doesnt work exactly as you planned to. Where is the mistake? Well it might be in the programming but you may find out it was some overset design or maybe you have to go back even a further level, maybe the analysis was wr
22、ong or the problem was stated wrong or you misunderstood what the person who gives you the problem wanted. All of these things will require reanalysis, redesigning and fixing the problem or hopefully fixing the problem and program and try again. It will keep process involving all of these steps. Let
23、s take a simple problem and apply this procedure to it. OK, here is a problem: is a given number even or odd? Sounds simple? Yes, it is simple. Why is it such a simple problem? You may see not realistically simple to spend time on in a course. But actually, its not so far remove from real life probl
24、ems you might want to solve you have a payroll system and you print checks every other week you have to determine whether this is a payroll week or not. What about the problems determining whether a year is a leap year? That is a similar flavor to it. Beyond that, in a course like this, was slide th
25、ey are used to present information. It is difficult to completely solve and present complicated problems. As one of the reasons by the way, that we recommend you look into the text book for this course or another text book or another book on this subject so that you can see larger, more realistic an
26、d more interesting examples. But for now, this example will serve us well and allows us to go through all of the steps recommended for problem solving and when were done well come up with a complete solution to the problem including code. So what would I say the first step was? State the problem upo
27、n I have done that already. And so I can go on to the analysis, and here, you may start thinking about, uh, whats really meant? What numbers are allowed? Is a negative number what I need to get an answer for? What about a fractional number like 10.3. That even make sense to ask question for even or
28、odd you have to ask yourself this question or you perhaps ask the client or teacher who give you the problem. Where is this number come from? Is this number that fixes forever? You have to think about that before you can write an algorithm for the program. Then there is a more basic question about w
29、hat even or odd mean. With your mathematic concepts, en, most people remember this from their school mathematics but if you are not sure this would be a time to go back and look up and remember, remind yourself what those concepts are. I have had even college level students with 12 years mathematics
30、 behind them not be sure whether zero is an even or odd number. So this possible has doubt in your mind about these things and the analysis face is what it makes sense to figure out what it really meant. Finally, program says determining the answer but how do you report that answer. You want the com
31、puter to light up a billboard(广告信息) or speak a word. How was the answer to be reported? You have to sight on that too. This analysis might lead you to restate the problem in more precise terms so lets try to do that now. Let me restate the problem Im really going to solve as this: give an integer nu
32、mber. OK, so now Ive paid on the kind of number its gonna be, its gonna be an integer, a number without decimal points(小数点). Typed in from the keyboard so its not built in, it isnt sent from a satellite, ok or some other obscure method as it gonna be typed in, if it is an even, write “even” on the screen so now well specify how the answer is to be reported. And if its odd weare gonna write “odd” on the screen so now we have much more precise definition of what the problem is going to be and we can proceed to think about an algorithm. So here is the one, le