Advertisement
Guest User

Bai 2 - BG8 - ver2

a guest
Aug 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | None | 0 0
  1. // Nhận xét
  2. Bài 2. Chị thắc mắc không biết em tạo biến đếm làm gì?
  3. 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
  4. // Code
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7.  
  8. int** NhapMang(int **a, int *pnDong, int *pnCot)
  9. {
  10.     int i, j;
  11.  
  12.     printf("Nhap vao so dong: ");
  13.     scanf("%d", pnDong);
  14.  
  15.     printf("Nhap vao so cot: ");
  16.     scanf("%d", pnCot);
  17.  
  18.     a = (int**)malloc(*pnDong * sizeof(int*));
  19.  
  20.     for (i = 0; i < *pnDong; i++)
  21.         a[i] = (int*)malloc(*pnCot * sizeof(int));
  22.  
  23.     for (i = 0; i < *pnDong; i++)
  24.     {
  25.         for (j = 0; j < *pnCot; j++)
  26.         {
  27.             printf("a[%d][%d] = ", i, j);
  28.             scanf("%d", &a[i][j]);
  29.         }
  30.     }
  31.  
  32.     return a;
  33. }
  34.  
  35. void XuatMang(int **a, int nDong, int nCot)
  36. {
  37.     int i, j;
  38.  
  39.     for (i = 0; i < nDong; i++)
  40.     {
  41.         for (j = 0; j < nCot; j++)
  42.             printf("%5d", a[i][j]);
  43.  
  44.         printf("\n");
  45.     }
  46. }
  47.  
  48. void TimViTri(int **a, int nDong, int nCot)
  49. {
  50.     int i, j;
  51.     int Max = INT_MIN;
  52.  
  53.     for (i = 0; i < nDong; i++)
  54.     {
  55.         for (j = 0; j < nCot; j++)
  56.         {
  57.             if (Max < a[i][j])
  58.                 Max = a[i][j];
  59.         }
  60.     }
  61.  
  62.     for (i = 0; i < nDong; i++)
  63.     {
  64.         for (j = 0; j < nCot; j++)
  65.             if (a[i][j] == Max)
  66.                 printf("Max = %d xuat hien tai vi tri ---> dong %d cot %d \n", Max, i, j);
  67.     }
  68. }
  69.  
  70. int main()
  71. {
  72.     // khai bao
  73.     int **a = NULL;
  74.     int nDong = 0;
  75.     int nCot = 0;
  76.     int i;
  77.  
  78.     // nhap mang
  79.     a = NhapMang(a, &nDong, &nCot);
  80.  
  81.     // xuat mang
  82.     XuatMang(a, nDong, nCot);
  83.  
  84.     // tim vi tri Max xuat hien
  85.     TimViTri(a, nDong, nCot);
  86.  
  87.     // giai phong bo nho
  88.     for (i = 0; i < nDong; i++)
  89.         free(a[i]);
  90.  
  91.     free(a);
  92.     a = NULL;
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement