Data-Driven Programming Data driven progamming is a programming model where the data itself controls the flow of the program and not the program logic. It is a model where you control the flow by offering different data sets to the program where the program logic is some generic form of flow or of state-changes. set1: DOWN - STOP - START - STOP - UP - STOP set2: UP - DOWN - UP - DOWN For example if you have program that has four states: UP - DOWN - STOP - START You can control this program by offering input (data) that represents the states: The program code stays the same but data set (which is not of a dynamic input type but statically given to the computer) controls the flow. Although there are more than a few ideas as to what data driven programming is, allow me to give an example using a data structure and a function. Non data driven example: data_lloyd = {'name': 'Lloyd', 'lives': 'Alcoy } data_jason = {'name': 'Jason', 'lives':...
Mảng 1 chiều
Định nghĩa mảng
một biến có thể chưa một giá trị. Mảng là tập hợp các biến có cùng kiểu dữ liệu, các phần tử cùng tên với nhau nhưng phân biệt bởi chỉ số hay còn gọi là chỉ số mảng.
Đối với một số ngôn ngữ lập trình thì mảng cũng có thể là tập hợp các biến nhưng không có cùng kiểu dữ liệu, ví dụ như trong ngôn ngư JavaScript, Ruby,...
Mảng 1 chiều
Ở đâu chúng ta sẽ lấy ví dụ về mảng 1 chiều trong ngôn ngữ lập trình C++:
- Khởi tạo mảng 1 chiều, chúng ta có thể khởi tạo theo 3 cách cơ bản như sau
int arr[5];
///
int arr[5] = {4, 12, 7, 15, 9};
///
int arr[5];
arr[0] = 4;
arr[1] = 12;
- Mẫu khai báo hoàn chỉnh trong ngôn ngữ C++
#include <stdio.h>
int main(){
int arr[5] = {4, 12, 7, 15, 9};
return 0;
}
Các thao tác trên mảng 1 chiều
- Nhập Mảng:
void NhapMang(int a[], int n){
for(int i = 0;i < n; ++i){
printf("\nNhap phan tu a[%d] = ", i);
scanf("%d", &a[i]);
}
}
- Xuất Mảng:
void XuatMang(int a[], int n){
for(int i = 0;i < n; ++i){
printf("\nPhan tu a[%d] = %d", i, a[i]);
}
}
- Tìm kiếm:
int TimKiem(int a[], int n, int v){
for(int i = 0;i < n; ++i){
if(a[i] == v){
return i;
}
}
return -1;
}
kết luận
Mảng là một cấu trúc quan trọng trong việc lập trình.