TAILIEUCHUNG - THEORY AND PROBLEMS OF PROGRAMMING WITH Second Edition phần 5

Bây giờ chúng ta giới thiệu các nhà điều hành dấu phẩy (,) được sử dụng chủ yếu kết hợp với tuyên bố cho. Toán tử này cho phép hai biểu thức khác nhau để xuất hiện trong tình huống mà chỉ biểu hiện thông thường sẽ được sử dụng. Ví dụ, nó có thể viết (biểu hiện 7a, 7b biểu hiện, biểu thức 2; biểu 3) tuyên bố | 212 PROGRAM STRUCTURE CHAP. 8 Fig. xl xr b formerly xr Fig. Í------ Sep -H-------------- a xl xr b formerly xl Fig. Let us consider a program outline for the general case where a and b are input quantities but Sep has a fixed value of . 1. Assign a value of sep . 2. Read in the values of a and b. 3. Repeat the following until either yl becomes equal to yr the desired maximum will be at the midpoint or the most recent value of b - a becomes less than or equal to 3 sep а Generate the two interior points xl and xr. б Calculate the corresponding values of yl and yr and determine which is larger. c Reduce the search interval by eliminating that portion that does not contain the larger value of y. 4. Evaluate xmax and ymax. 5. Display the values of xmax and ymax and stop. To translate this outline into a program we first create a programmer-defined function to evaluate the mathematical function y X cos x . Let US call this function curve. This function can easily be written as follows. evaluate the function y X cos x double curve double x return x cos x Note that cos X is a call to a c library function. CHAP. 8 PROGRAM STRUCTURE 213 Now consider step 3 in the above outline which carries out the interval reduction. This step can also be programmed as a function which we will call reduce. Notice however that the values represented by the variables a b xl xr yl and yr which change through the course of the computation must be transferred back and forth between this function and main. Therefore let these variables be external variables whose scope includes both reduce and main. Function reduce can be written as interval reduction routine void reduce void xl a b a - CNST xr xl CNST yl curve xl yr curve xr if yl yr retain left interval b xr return if yl yr retain right interval a xl return Notice that the parameter that we have referred to earlier as Sep is now represented as the character constant CNST. Also notice that this function does not .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.