dzungchaos

C++ "Cấp phát bộ nhớ"

Nov 6th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //void alloc_mem(int **mt, int m, int n){
  4. //  mt = new int*[m];
  5. //  for(int i = 0; i < m; i++)
  6. //      mt[i] = new int [n];
  7. //}
  8.  
  9. void free_mem(int **mt, int m, int n){
  10.     for(int i = 0; i < m; i++)
  11.         delete[] mt[i];
  12.     delete[] mt;
  13. }
  14.  
  15. void input(int **mt, int m, int n){
  16.     for(int i = 0; i < m; i++)
  17.         for(int j = 0; j < n; j++){
  18.             printf("mt[%d][%d] = ", i, j);
  19.             scanf("%d", &mt[i][j]);
  20.         }
  21. }
  22.  
  23. void output(int **mt, int m, int n){
  24.     for(int i = 0; i < m; i++){
  25.         for(int j = 0; j < n; j++)
  26.             printf("%d ", mt[i][j]);
  27.         printf("\n");
  28.     }      
  29. }
  30.  
  31. int xuly(int **mt, int m, int n){
  32.     int tong = 0;
  33.     for(int i = 0; i < m; i++)
  34.         for(int j = 0; j < n; j++)
  35.             tong += mt[i][j] % 2 == 0? mt[i][j]:0;
  36.     return tong;
  37. }
  38.  
  39. int main(){
  40.     int m, n, **mt;
  41.     printf("Nhap m, n = ");
  42.     scanf("%d%d", &m, &n);
  43.     //alloc_mem(mt, m, n);
  44.     mt = new int*[m];
  45.     for(int i = 0; i < m; i++)
  46.         mt[i] = new int [n];
  47.     input(mt, m, n);
  48.     output(mt, m, n);
  49.     printf("Tong cac phan tu chan la %d", xuly(mt, m, n));
  50.     free_mem(mt, m, n);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment