Posts

Medians and Order Statistics

Image
Phần này có 1 vài vấn đề cần quan tâm như: - Median - i-th order statistic  - Selection problem  ------- 1, i-th order statistic i-th order statistic là phần tử có thứ tự i trong dãy đã xếp hay còn gọi là phần tử nhỏ thứ i trong dãy. Median( theo môn MAS có thể dịch là trung vị) là phần tử halfway. Nếu n lẻ, median nằm ở giữa. Nếu n chẵn, sẽ có median upper và median lower: Phát biểu bài toán: Input: Cho một dãy n phần tử phân biệt và số i (1<=i <= n) Output:  Đưa ra phần tử lớn hơn chính xác i-1 phần tử khác trong dãy. (Nói cách khác đưa ra phần tử nhỏ thứ i trong dãy). Xét bài toàn tìm minimum và maximum trong một dãy chưa được sort, ta mất (n-1) phép so sánh cho từng loại. Vậy ta phải mất 2n-2 phép so sánh để tìm cả max và min. Liệu có cách nào hiệu quả hơn? Ta có thể chỉ mất 3*|n/2| phép so sánh bằng cách: - Chia thành các cặp phần tử - So sánh từng cặp với nhau - Với mỗi cặp lại so sánh với min, max hiện tại => Chỉ mất 3 phép so sán...

Reflection: Assignment 2

Image
Đề bài:  Phát triển bài Assignment 1 theo yêu cầu sau: Xây dựng cấu trúc dữ liệu struct để lưu thông tin khách hàng và khoản vay đối với ngân hàng gồm các thông tin: - Số hợp đồng - Họ tên khách hàng - Tổng số tiền vay - Thời hạn (Số tháng) - Lãi suất - Có tham gia bảo hiểm tín dụng không? Nếu có thì mức nào? - Có là gia đình có công không? - Làm việc trong lực lượng vũ trang? Sử dụng array để lưu trữ danh sách thông tin khách hàng và khoản vay đối với ngân hàng Xây dựng menu chương trình gồm các chức năng quản lý như sau: 1. Nhập danh sách các hợp đồng vay hoặc nạp dữ liệu từ file 2. Hiển thị danh sách các hợp đồng vay 3. Thêm vào cuối một hợp đồng vay 4. Tìm kiếm một khoản vay theo số hợp đồng: 5. Tìm kiếm hợp đồng vay theo tên hoặc họ tên khách hàng 6. Tìm kiếm hợp đồng vay theo tổng số tiền vay 7. Sửa thông tin một hợp đồng nào đó tìm được 8. Thống kê - Tổng số hợp đồng, tổng số tiền đã cho vay - Danh sách các khoản vay của từng khách hàng - Tổng dư nợ ...

Reflection: ArrayReview

Image
I, Summary Array types in C are traditionally of a fixed, static size specified at compile time.            dataType arrayIdentifier[ size ] = { value, ... , value }; The syntax of a function call that passes an array is             functionIdentifier ( arrayIdentifier, ... ) The syntax of a function header that receives an array address is           dataType functionIdentifier ( dataType arrayIdentifier [ ], ... ) II, Applied to solve problems I. Re-code again the example in the clip This is my result:  link code: II. Implement functions 1. Nhập vào kích thước và các phần tử của một mảng một chiều void nhap(int *n, int mang[]) 2. Copy một mảng sang một mảng khác void  copy(int nguon[], int dich[], int n) 3. tìm kiếm k trong mảng, không có trả về -1, có trả về chi so đầu tiên int tim(int mang[], int n, int k) 4. In danh sách vị trí của các phần tử...

Reflection: ArrayAsParam

Image
I, Summary Arguments  The syntax of a function call that passes an array is             functionIdentifier ( arrayIdentifier, ... ) Where functionIdentifier is the name of the function and arrayIdentifier is the array name without brackets. Parameters The syntax of a function header that receives an array address is           dataType functionIdentifier ( dataType arrayIdentifier [ ], ... ) The brackets following arrayIdentifier inform the compiler that the parameter holds the address of a one-dimensional array. - Arrays are automatically passed by reference. Do not use &. - If the function modifies the array, it is also modified in the calling environment II, Applied to solve problems I. Re-code again the example in the clip This is my code This is my result inputArray.cpp II. Implement functions to solve problems in reflection array (as in clip array review) arrayRev...

Reflection: Assignment1_bank

Image
Đề bài: Viết chương trình tính tiền khách hàng phải trả vào gốc và lãi cho ngân hàng theo nghiệp vụ được mô tả như sau: Với mỗi khoản vay của ngân hàng. Khách hàng đều phải cam kết trả nợ trong một khoảng thời gian nhất định tính bằng tháng. Theo khoảng thời gian này, hàng tháng khách hàng phải trả khoản gốc cho khoản vay bằng Tổng dư nợ : số tháng. Ví dụ khách hàng vay 1,2 tỷ trong vòng 5 năm (60 tháng) thì mỗi tháng khách hàng phải trả gốc là 1,2 tỷ : 60 tháng = 20 triệu/tháng Khoản tiền lãi sẽ được tính theo dư nợ hiện thời với lãi suất theo hợp đồng tín dụng. Ví dụ khách hàng vay 1.2 tỷ với lãi suất 12%/năm. Sau 10 tháng trả nợ, dư nợ của khách hàng còn 1 tỷ. Vậy khoản lãi của khách hàng cho tháng 11 sẽ là: 1 tỷ * 12/100/12 = 10 triệu Với mỗi khoản vay, hàng tháng khách hàng bắt buộc phải trả một khoản bảo hiểm cho tài sản thuế chấp bằng 1.2% /năm cho tổng số tiền vay. Ví dụ với khoản vay 1,2 tỷ. Hàng tháng khách hàng phải trả tiền bảo hiểm tài sản là: 1.2 tỷ * 1.2/100/12...

Reflection: Array

Image
I, Summary A data structure is a collection of data types designed to store information in some optimal way. The simplest example of a data structure is an array. The fundamental unit of an array is an element. Each element holds has an index and holds one data value.  Index numbering starts at 0 and extends to one less than the size of the array. To refer to an element, we use the array name followed by bracket notation around the element index.  dataType arrayIdentifier[ size ] = { value, ... , value }; II, Applied to solve problems A,Write program 1. Declare an double array with max 100 elements 2. Input an positive integer n that indicated the number of element of array 3. Input n elements of array 4. Display array has just been inputted 5. Calculate sum of all elements, sum of odd element, average of even elements, average of element at odd index Source and demo: file source:  array.cpp B, Pro...

Reflection: Pointer

Image
I, Summary What is pointer? Pointers are the container/variable who holds address of other variables. C uses pointers in three different ways:      - C uses pointers to create dynamic data structures -- data structures built up from blocks of memory allocated from the heap at run-time.      - C uses pointers to handle variable parameters passed to functions.      - Pointers in C provide an alternative way to access information stored in arrays. Pointer techniques are especially valuable when you work with strings. There is an intimate link between arrays and pointers in C. The declaration of pointers follows this format: type * name;  II, Applied to solve problems 1. void input(int*n, int*m); – input and check n>0, m>0 2. int fun2(int n, int m); Function returns n! + m n if n > 0 and m > 0, otherwise return 0 3. double fun6(int n); Function returns value calculated by formula 4. i...