0% found this document useful (0 votes)
5 views3 pages

DAP AN

The document contains code snippets for various programming tasks in C, including functions for creating and manipulating arrays, calculating sums, finding maximum values, and handling structures. It also includes user input for package details and file operations for saving data. The code demonstrates recursive functions for summing arrays and sorting elements.

Uploaded by

Đăng Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

DAP AN

The document contains code snippets for various programming tasks in C, including functions for creating and manipulating arrays, calculating sums, finding maximum values, and handling structures. It also includes user input for package details and file operations for saving data. The code demonstrates recursive functions for summing arrays and sorting elements.

Uploaded by

Đăng Quang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ĐÁP ÁN ĐỀ ÔN TẬP

Câu 1:
// Cau a
void tao_mang(int * a, int m, int n) {
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i * n + j] = (rand() % 50) + 1;
}

// Cau b
int sum_dong(int * a, int k, int n) {
int s = 0;
for (int i = 0; i < n; i++)
s += a[k * n + i];
return s;
}

// Cau c
float tim_max(int * a, int m, int n) {
int max = a[0];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (max < a[i * n + j])
max = a[i * n + j];
return max;
}

// Cau d
float tbc_cot(int * a, int m, int n, int l) {
int s= 0;
for (int i = 0; i < m; i++)
s += a[i * n + l];
return (float) s / m;
}

// Cau e
int main() {
int m, n, k, l;
printf("M, N: "); scanf("%d %d", &m, &n);
int * a = (int *) malloc(m * n * sizeof(int));
tao_mang(a, m, n);
printf("Dong ="); scanf("%d", &k);
printf("Tong dong %d: %d\n", k, sum_dong(a, k, n));
printf("Cot ="); scanf("%d", &l);
printf("TBC cot %d: %.2f\n", l, tbc_cot(a, m, n, l));
free(a);
}
Câu 2:
// Cau a
typedef struct pkg {
char name[17];
float weight;
int quartile;
} PACKAGE;

// Cau b
void nhap_package(PACKAGE & p) {
fflush(stdin);
printf("Ten kien hang: ");
fgets(p.name, 16, stdin);
p.name[strlen(p.name)-1] = 0;
printf("Trong luong: "); scanf ("%f", &p.weight);
printf("Quy (1-4): "); scanf ("%d", &p.quartile);
}

void nhap_mang(PACKAGE v[], int n) {


for (int i = 0; i < n; i++)
nhap_package(v[i]);
}

// Cau c
void xuat_mang(PACKAGE v[], int n) {
for (int i = 0; i < n; i++)
printf("%-16s %5.2f %d\n", v[i].name, v[i].weight, v[i].quartile);
}

// Cau d
void tim_max(PACKAGE v[], int n) {
PACKAGE p = v[0];
for (int i = 1; i < n; i++)
if (p.weight < v[i].weight)
p = v[i];
printf("Kien hang co khoi luong lon nhat la: %s (%.2f)\n", p.name, .weight);
}

// Cau e
void luu_mang(PACKAGE v[], int n) {
FILE * fp = fopen("packages.txt", "w");
for (int i = 0; i < n; i++)
fprintf(fp, "%s %.2f %d\n", v[i].name, v[i].weight, v[i].quartile);
fclose(fp);
}
Câu 3:
// Cau a
int sum_mang(int a[], int n) {
if (n == 1) return a[0];
return a[n-1] + sum_mang(a, n-1);
}

// Cau b
int vtmax(int a[], int start, int n) {
int max = a[start];
int vt = start;
for (int i = start + 1; i < n; i++)
if (max < a[i]) {
max = a[i];
vt = i;
}
return vt;
}

void swap(int & a, int & b) {


int t = a; a = b; b = t;
}

void sort(int a[], int start, int n) {


if (start < n) {
int vt = vtmax(a, start, n);
swap(a[start], a[vt]);
sort(a, start + 1, n);
}
}

You might also like