zoukankan      html  css  js  c++  java
  • 动态二维数组实现

    二维动态数组和静态数组的存储方式如下:

     二维动态数组代码实现如下:

    #include <stdio.h>
    #define DEBUG
    int matrixMultiplication(int n, int **A, int **B, int s, int t){
        int result = 0;
        for (int i = 0; i < n; i++){
            result += A[s][i] * B[i][t];
    #ifdef DEBUG
            printf("result:%d, A[s][i]:%d, b[i][t]:%d
    ", result, A[s][i], B[i][t]);
    #endif
        }
        return result;
    }
    
    int main(){
        int n = 5;
        int **A = new int*[n];
        int k = 0;
        int **B = new int*[n];
        for (int i = 0; i < n; i++){
            A[i] = new int[n];
            B[i] = new int[n];
        }
        for (int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                B[i][j] = -k;
                A[i][j] = k++;
            }
        }
    #ifdef DEBUG
        printf("A:
    ");
        for (int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                printf("%d ", A[i][j]);
            }
            printf("
    ");
        }
        printf("B:
    ");
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                printf("%d ", B[i][j]);
            }
            printf("
    ");
        }
    #endif
        for (int i = 0; i < n; i++){
            delete[] A[i];
            delete[] B[i];
        }
        delete[] A;
        delete[] B;
        int result = matrixMultiplication(n, A, B, 2, 3);
        printf("%d
    ", result);
    }
  • 相关阅读:
    数据库管理
    CodeSmith的运用正在完善中,待续
    各搜索引擎搜索代码
    浏览器本地存储
    QQ在线代码生成
    6组Alpha冲刺4/6
    结对编程作业
    camera相关
    数电、模电相关
    usb协议学习
  • 原文地址:https://www.cnblogs.com/feng-ying/p/13125378.html
Copyright © 2011-2022 走看看