Danh mục

Giáo trình Tiếng Anh chuyên ngành Khoa học máy tính: Phần 2 - KS. Châu Văn Trung

Số trang: 270      Loại file: pdf      Dung lượng: 12.43 MB      Lượt xem: 15      Lượt tải: 0    
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Giáo trình Tiếng Anh chuyên ngành Khoa học máy tính do KS. Châu Văn Trung và cộng tác TS. Nguyễn Phi Khứ - Quang Hùng biên soạn nhằm đáp ứng nhu cầu giảng dạy và học tập của các sinh viên chuyên ngành Công nghệ thông tin và kỹ thuật máy tính. Sách gồm 2 phần, mời các bạn cùng tham khảo phần 2 sau đây với nội dung các chương: Mảng và chuỗi, các file dữ liệu, lập trình hướng đối tượng và các cấu trúc dữ liệu.


Nội dung trích xuất từ tài liệu:
Giáo trình Tiếng Anh chuyên ngành Khoa học máy tính: Phần 2 - KS. Châu Văn Trung Chương 6: Mảng và Chuỗi 3 65 CHAPTER * Arrays and Strings 6 M ảng và ch u ỗ i MỤC ĐÍCH YÊU CẨU Sau khi học xong chương này, các Dạn sẽ nắm vững các khái n iệm về mảng và chuỗi, các phương pháp xử lý m ảng và ''huỗi trong các ngôn ngữ lập trìn h C/C++, V isual B asic và Java, với các nội dung cụ th ể sau đây: ♦ Introduction to arrays ♦ Giới thiệu sơ lược về m ảng ♦ Arrays in Visual Basic + Các mảng trong Visual basic ♦ Arrays in C/C++ and Java + Các mảng trong C/C ++ và Java ♦ Searching + Tìm kiêm ♦ Sorting + Sắp xếp thứ tự Ngoài ra, ở cuối chương còn zỏ phần bài tập có lời g iải, bài tập bổ sung và đáp án Ìihằm giúp các bọn thực h àn h và áp dụng m ột cách hiệu quả vào công việc thực tế. / 366 Chapter 6: Arrays anơ strings CHỦ DIỂM 6.1 INTRODUCTION TO ARRAYS G i ó i thiệu sơ lược về mảnq An a r r a y is a group of memory locations all of the same type that have the same name. Previously, in a program to calculate employee pay, the user would type in the employee number, the pay rate, and the number of hours worked. There would be one variable to hold each piece of informa­ tion as shown in Fig. 6-1, i-nd then the gross pay would be calculated. employeeNum hourly Rate hours Worked grossPay 101 6.25 40 Fig. 6-1 Individual variables for payroll. If th e re w ere m ore th a n one em ployee, th e p ro g ram could have a loop for th e u ser to type in th e in form ation in to th e sam e variables for th e second em ployee and calculate th a t p e rso n ’s gross pay, then th e th ird , and so forth. However, a problem would a ris e if the em­ ployer w anted to have a re p o rt co n tain in g a lis t of th e w eekly pay for all th e em ployees, th e average pay for th e w eek, an d th e n a list of all th e em ployees who received above th e average pay. T he average could not be calculated until all th e em ployees’ in fo rm atio n was submitted. In o r d e i'to com pare each p erso n ’s pay to th e av erag e, all th e infor­ m ation would need to be e n tered a second tim e. Oane solution to this problem would be to have a different variable for each employee, as shown in Fig. 6-2. Each person’s pay could be compared to the average. This would be possible if there were only two or three employees, but completely im practical to declare separate variables for twenty or a hundred or a thousand employees. The solution to this problem is to use an array. Arrays allow the storing and m anipulating of large amounts of data. Three arrays are declared, one to hold all the employee numbers, another for all the hourly rates and a third for all the hours worked, as shown in Fig. 6-3. The information is entered in such a way th a t the employee whose num ber is in box 2 receives the rate in box 2 and worked the number of hours in box 2. Each person’s gross pay is calculated and the average is found. Each person’s data is still in memory and can quickly be examined to produce the list of people with pay above the average. Chương 6: Mảng và Chuỗi 367 employeeNuml hourlyRatel hoursWorkedl grossPayl 1 101 16251 1401 □ employeeNum2 hourlyRate2 hoursWorked2 grossPay2 11021 1725I 1381 11 employecNum3 hourlyRate3 hoursWorked3 grossPay3 11031 ran FI 11 Fig- 6-2 Individual variables íor three em ployees. employeeNums hourlyRates hoursWorked gross Pay [01 101 [0] 6.25 [0] 40 [0] [1] 102 [1] 6.55 [1] 38 [1] [2] 103 [2] 7.25 [2] 42 [2] [3] 104 [3] 7.15 [3] 40 [3] [41 105 [4] 6.25 [4] 37 [4] [n- 1 ] [n-11 In-1] ln-11 Fig. 6-3 Array variables for any number (n) of employees. 6.1.1 M a n ip u la tin g A rra y s - x ử lý các mảng The entire array is declared once and known by one name. W hen it is declared, the exact num ber of memory locations to be set aside is speci­ fied. All the locations m ust contain data of the same type. Each e lem en t, or individual memory location in the array, is accessible through the use of its s u b s c r i p t or in d e x n u m b e r. F o r e x a m p le , in F ig . 6-3 em ployeeN um s[3] r e fe rs to th e e le m e n t in box n u m b e r 3 of th e employeeNums array w hich contains “104,” or hourlyRates[4] would ac­ cess the elem ent in box 4 of the hourlyRates array which contains “6.25.” The subscripts usually begin with 0. Input and output for an array is accom plished through the use of loops. Each tim e through the loop a different box in the array is filled or printed. 368 Chapter 6: Arrays and Strings EXAM PLE 6.1 Pseudocode for a loop to f i l l or p rin t an array of n items would look like this: loop from lev = 0 to lev = n -1 where n is the total number of items in the array input or output arrayllcv] end loop ...

Tài liệu được xem nhiều:

Tài liệu liên quan: