Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nhận xét
- Bài 2. Chị thắc mắc không biết em tạo biến đếm làm gì?
- Bài làm không thực hiện tìm các vị trí max khi nhập vào mảng toàn số âm
- // Code
- #include<stdio.h>
- #include<stdlib.h>
- int** NhapMang(int **a, int *pnDong, int *pnCot)
- {
- int i, j;
- printf("Nhap vao so dong: ");
- scanf("%d", pnDong);
- printf("Nhap vao so cot: ");
- scanf("%d", pnCot);
- a = (int**)malloc(*pnDong * sizeof(int*));
- for (i = 0; i < *pnDong; i++)
- a[i] = (int*)malloc(*pnCot * sizeof(int));
- for (i = 0; i < *pnDong; i++)
- {
- for (j = 0; j < *pnCot; j++)
- {
- printf("a[%d][%d] = ", i, j);
- scanf("%d", &a[i][j]);
- }
- }
- return a;
- }
- void XuatMang(int **a, int nDong, int nCot)
- {
- int i, j;
- for (i = 0; i < nDong; i++)
- {
- for (j = 0; j < nCot; j++)
- printf("%5d", a[i][j]);
- printf("\n");
- }
- }
- void TimViTri(int **a, int nDong, int nCot)
- {
- int i, j;
- int Max = INT_MIN;
- for (i = 0; i < nDong; i++)
- {
- for (j = 0; j < nCot; j++)
- {
- if (Max < a[i][j])
- Max = a[i][j];
- }
- }
- for (i = 0; i < nDong; i++)
- {
- for (j = 0; j < nCot; j++)
- if (a[i][j] == Max)
- printf("Max = %d xuat hien tai vi tri ---> dong %d cot %d \n", Max, i, j);
- }
- }
- int main()
- {
- // khai bao
- int **a = NULL;
- int nDong = 0;
- int nCot = 0;
- int i;
- // nhap mang
- a = NhapMang(a, &nDong, &nCot);
- // xuat mang
- XuatMang(a, nDong, nCot);
- // tim vi tri Max xuat hien
- TimViTri(a, nDong, nCot);
- // giai phong bo nho
- for (i = 0; i < nDong; i++)
- free(a[i]);
- free(a);
- a = NULL;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement