数组在每次递归时都会更改其值

时间:2018-12-17 15:19:46

标签: c++ recursion matrix

这是我的代码。如您所见,我将数组s1,s2,..等用于结果。但是,当我递归地更改s1,s2等时,它在递归的所有步骤上都会更改。因此,第一个s1 =最后一个s1。如何解决?因此,在递归的每个步骤中,s1,s2等的值都应不同。 我知道问题是我按值将数组传递给函数,但是idk如何解决它。谢谢(对不起我的Eng:p)

    #include "pch.h"
    #include<iostream>
    #include<cstdio>
    #include<conio.h>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<clocale>
    #include <iomanip>
    using namespace std;

    void vivod(int **matrix, int n);
    int** Matrix_Add(int **a, int **b, int **c, int n, int x1, int y1, int x2, int y2);
    int** Matrix_Sub(int **a, int **b, int **c, int n, int x1, int y1, int x2, int y2);
    int** Matrix_Multiply(int **a, int **b, int **c, int x1, int y1, int x2, int y2, int n);
    int** strassen(int  **a, int **b, int **c, int **s1, int **s2, int **s3, int **s4, int **s5, int **s6, int **s7, int **s8, int **m1, int **m2, int **m3, int **m4, int **m5, int **m6, int **m7, int **t1, int **t2, int** c11, int** c12, int** c21, int** c22, int m, int n, int x1, int y1, int x2, int y2);
    int** Naive_Multiply(int  **a, int **b, int **c, int n);

    int main()
    {
        setlocale(LC_ALL, "Russian");
        int n;
        cout << "Enter the N:";
        cin >> n;
        int **A = new int*[n];
        int **B = new int*[n];
        int **C = new int*[n];
        int **k = new int*[n];
        int **s1 = new int*[n];
        int **s2 = new int*[n];
        int **s3 = new int*[n];
        int **s4 = new int*[n];
        int **s5 = new int*[n];
        int **s6 = new int*[n];
        int **s7 = new int*[n];
        int **s8 = new int*[n];
        int **m1 = new int*[n];
        int **m2 = new int*[n];
        int **m3 = new int*[n];
        int **m4 = new int*[n];
        int **m5 = new int*[n];
        int **m6 = new int*[n];
        int **m7 = new int*[n];
        int **t1 = new int*[n];
        int **t2 = new int*[n];
        int **c11 = new int*[n];
        int **c12 = new int*[n];
        int **c21 = new int*[n];
        int **c22 = new int*[n];
        int **buff = new int*[n];
        for (int i = 0; i < n; i++)
        {
            buff[i] = new int[n];
            A[i] = new int[n];
            B[i] = new int[n];
            C[i] = new int[n];
            k[i] = new int[n];
            s1[i] = new int[n];
            s2[i] = new int[n];
            s3[i] = new int[n];
            s4[i] = new int[n];
            s5[i] = new int[n];
            s6[i] = new int[n];
            s7[i] = new int[n];
            s8[i] = new int[n];
            m1[i] = new int[n];
            m2[i] = new int[n];
            m3[i] = new int[n];
            m4[i] = new int[n];
            m5[i] = new int[n];
            m6[i] = new int[n];
            m7[i] = new int[n];
            t1[i] = new int[n];
            t2[i] = new int[n];
            c11[i] = new int[n];
            c12[i] = new int[n];
            c21[i] = new int[n];
            c22[i] = new int[n];
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++) {
                A[i][j] = rand() % 10;
                B[i][j] = rand() % 10;
            }
        cout << "First Matrix:" << endl;
        vivod(A, n);
        cout << "Second Matrix:" << endl;
        vivod(B, n);
        int begin = clock();
        //for (int i =0; i < 100; i++)
        k = Naive_Multiply(A, B, k, n);
        int end = clock();
        cout << "Naive Multiply tacts: " << end - begin << endl;
        vivod(k, n);
        int begin2 = clock();
        //for (int i = 0; i < 100; i++)
        C = strassen(A, B, C, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, n, n, 0, 0, 0, 0);
        int end2 = clock();
        cout << "Shtrassen tacts: " << end2 - begin2 << endl;
        vivod(C, n);
        for (int i = 0; i < n; i++)
        {
            delete[] A[i];
            delete[] B[i];
            delete[] C[i];
            delete[] k[i];
            delete[] s1[i];
            delete[] s2[i];
            delete[] s3[i];
            delete[] s4[i];
            delete[] s5[i];
            delete[] s6[i];
            delete[] s7[i];
            delete[] s8[i];
            delete[] m1[i];
            delete[] m2[i];
            delete[] m3[i];
            delete[] m4[i];
            delete[] m5[i];
            delete[] m6[i];
            delete[] m7[i];
            delete[] t1[i];
            delete[] t2[i];
            delete[] c11[i];
            delete[] c12[i];
            delete[] c21[i];
            delete[] c22[i];
        }
        system("pause");
        return 0;
    }
    int** strassen(int  **a, int **b, int **c, int **s1, int **s2, int **s3, int **s4, int **s5, int **s6, int **s7, int **s8, int **m1, int **m2, int **m3, int **m4, int **m5, int **m6, int **m7, int **t1, int **t2, int** c11, int** c12, int** c21, int** c22, int m, int n, int x1, int y1, int x2, int y2) {
        m = n / 2;
        if (m != 1)
        {
            s1 = Matrix_Add(a, a, s1, m, x1 + m, y1, x1 + m, y1 + m);
            s2 = Matrix_Sub(s1, a, c, m, 0, 0, x1, y1);
            s3 = Matrix_Sub(a, a, s3, m, x1, y1, x1 + m, y1);
            s4 = Matrix_Sub(a, s2, s4, m, x1, y1 + m, 0, 0);
            s5 = Matrix_Sub(b, b, s5, m, x2, y2 + m, x2, y2);
            s6 = Matrix_Sub(b, s5, s6, m, x2 + m, y2 + m, 0, 0);
            s7 = Matrix_Sub(b, b, s7, m, x2 + m, y2 + m, x2, y2 + m);
            s8 = Matrix_Sub(s6, b, s8, m, 0, 0, x2 + m, y2);

            m1 = strassen(s2, s6, m1, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, 0, 0, 0, 0);
            m2 = strassen(a, b, m2, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, x1, y1, x2, y2);
            m3 = strassen(a, b, m3, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, x1, y1 + m, x2 + m, y2);
            m4 = strassen(s3, s7, m4, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, 0, 0, 0, 0);
            m5 = strassen(s1, s5, m5, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, 0, 0, 0, 0);
            m6 = strassen(s4, b, m6, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, 0, 0, x2 + m, y2 + m);
            m7 = strassen(a, b, m7, s1, s2, s3, s4, s5, s6, s7, s8, m1, m2, m3, m4, m5, m6, m7, t1, t2, c11, c12, c21, c22, m, m, x1 + m, y1 + m, 0, 0);

            t1 = Matrix_Add(m1, m2, t1, m, 0, 0, 0, 0);
            t2 = Matrix_Add(t1, m4, t2, m, 0, 0, 0, 0);

            c11 = Matrix_Add(m2, m3, c11, m, 0, 0, 0, 0);
            c21 = Matrix_Sub(t2, m7, c21, m, 0, 0, 0, 0);
            c12 = Matrix_Add(t1, m5, c12, m, 0, 0, 0, 0);
            c12 = Matrix_Add(c12, m6, c12, m, 0, 0, 0, 0);
            c22 = Matrix_Add(t2, m5, c22, m, 0, 0, 0, 0);
            for (int i = 0; i < n / 2; i++)
            {
                for (int j = 0; j < n / 2; j++)
                {
                    c[i + n - 2 * m][j + n - 2 * m] = c11[i][j];
                    c[i + n - 2 * m][j + n - m] = c12[i][j];
                    c[i + n - m][j + n - 2 * m] = c21[i][j];
                    c[i + n - m][j + n - m] = c22[i][j];
                }
            }
        }
        else
        {
            c = Matrix_Multiply(a, b, c, x1, y1, x2, y2, n);
        }
        return c;
    }
    void vivod(int **matrix, int n)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                cout << matrix[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    int** Matrix_Add(int **a, int **b, int **c, int n, int x1, int y1, int x2, int y2)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                c[i][j] = a[i + x1][j + y1] + b[i + x2][j + y2];
            }
        }
        return c;
    }

    int** Matrix_Sub(int **a, int **b, int **c, int n, int x1, int y1, int x2, int y2)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                c[i][j] = a[i + x1][j + y1] - b[i + x2][j + y2];
            }
        }
        return c;
    }
    int** Matrix_Multiply(int **a, int **b, int **c, int x1, int y1, int x2, int y2, int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                c[i][j] = 0;
                for (int t = 0; t < n; t++) {
                    c[i][j] = c[i][j] + a[x1 + i][y1 + t] * b[x2 + t][y2 + j];
                }
            }
        }
        return c;
    }
    int** Naive_Multiply(int **a, int **b, int **c, int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                c[i][j] = 0;
                for (int t = 0; t < n; t++) {
                    c[i][j] = c[i][j] + a[i][t] * b[t][j];
                }
            }
        }
        return c;
    }

0 个答案:

没有答案