0

我的代码如下。当我尝试运行 addArray() 函数时会出现问题。我对 C++ 完全陌生,所以我不知道分段错误是什么意思。

我也知道可能有更好的方法来初始化和返回二维数组,但我正在慢慢弄清楚。

我现在的主要问题是分段错误。我猜这与我如何访问变量有关?

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;





int c, q, w, row, coll, quit, qq, opt;
int** arr1;
int** arr2;
int** ans;


//Method Prototypes
int menu();
inline int** getArray(int opt);
inline void printArray(int** arr, int height, int width);
void addArray();
void subtractArray();
void multiplyArrays();

void determArray();
void transposeArray();
void inverseArray();

//Prints out the menu for choosing which option to go with

int menu() {

    cout << "Press 1 for Addition\n";
    cout << "Press 2 for Subtraction\n";
    cout << "Press 3 for Multiplication\n";
    cout << "Press 4 for Determinant\n";
    cout << "Press 5 for Transpose\n";
    cout << "Press 6 for Inverse\n";
    cout << "Press 0 to quit\n\n";

    cin >> c;
    return c;

}

//Main method

int main(void) {
    cout << "C++ 2d Matrix Operations Menu\n";
    c = menu();

    while (c != 0) {

        if (c == 1) {
            addArray();
        } else if (c == 2) {
            subtractArray();
        } else if (c == 3) {
            void multiplyArrays();
        } else if (c == 4) {
            void determArray();
        } else if (c == 5) {
            void transposeArray();
        } else if (c == 6) {
        }


        c = menu();
    }

    cout << "Press Enter to Quit. GOOD BYE";

    cin >> quit;

    return 0;
}

/*
Prints out the specified array. 
It's arguments are the actual array and the height/weight



 */
inline void printArray(int** arr, int height, int width) {

    for (int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            std::cout << arr[i][j] << ' ';
        }
        std::cout << std::endl;
    }


}

//Returns an array.

inline int** getArray(int opt) {

    if (opt == 0) {
        cout << "How many rows and columns should be in the array?\n";
        cin >> q >> w;
    } else {
        q = 3;
        w = 3;
    }

    int** ary = new int*[q];
    for (int i = 0; i < q; ++i) {
        ary[i] = new int[w];
    }

    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            cout << "What should the value be for item" << row << "," << coll << "\n";
            cin >> ary[row][coll];
        }
    }
    return ary;
}



//Adds arrays

void addArray() {

    arr1 = getArray(0);
    int h1 = q;
    int w1 = w;

    arr2 = getArray(0);
    int h2 = q;
    int w2 = w;

    if ((h1 != h2) || (w1 != w2)) {
        cout << "Both arrays must be the same size.";
        return;
    }



    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr1[row][coll] + arr2[row][coll];
        }
    }
    printArray(ans, q, w);

}

//Subtracts Arrays

void subtractArray() {

    arr1 = getArray(0);

    int h1 = q;
    int w1 = w;

    arr2 = getArray(0);

    int h2 = q;
    int w2 = w;

    if ((h1 != h2) || (w1 != w2)) {
        cout << "Both arrays must be the same size.";
        return;
    }



    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr2[row][coll] - arr1[row][coll];
        }
    }
    printArray(ans, q, w);

}

//Calculate the determinate of an array.

void determArray() {

    arr1 = getArray(1);
    printArray(arr1, q, w);

    //There must be a better/more efficient way to do this using loops.
    int determinant = arr1[0][0]*((arr1[1][1] * arr1[2][2]) - (arr1[2][1] * arr1[1][2])) - arr1[0][1]*(arr1[1][0] * arr1[2][2] - arr1[2][0] * arr1[1][2]) + arr1[0][2]*(arr1[1][0] * arr1[2][1] - arr1[2][0] * arr1[1][1]);

    printf("\nDeterminant of vector using method 1 is: %d\n", determinant);



}


//Transpose an array.

void transposeArray() {
    cout << "IN TRANS";
    arr1 = getArray(0);
    printArray(arr1, 3, 3);

    //Flip the values
    for (row = 0; row < q; row++) {
        for (coll = 0; coll < w; coll++) {
            ans[row][coll] = arr1[coll][row];
        }
    }

    cout << "----------" << endl << "The new vector looks like: \n";
    printArray(ans, q, w);

}

/*
        Multiply arrays. One option is to just multiply it by a number and the other is to multiply it by another array.



 */
void multiplyArrays() {


    arr1 = getArray(0);
    int h1 = q;
    int w1 = w;


    cout << "Do you wish to multiply the first vector by a number(Enter 1), or by a second vector(Enter 2)?";
    cin >> qq;

    int mu;
    //First Option is to multiply it by a single number
    if (qq == 1) {


        cout << "What number do you wish to multiply the vector by?";
        cin >> mu;



        for (row = 0; row < q; row++) {
            for (coll = 0; coll < w; coll++) {
                ans[row][coll] = arr1[row][coll] * mu;
            }
        }

        printArray(ans, h1, w1);

        //Multiply two arrays
    } else if (qq == 2) {

        arr2 = getArray(0);
        int h2 = q;
        int w2 = w;



        int n1 = h1;
        int n2 = w2;


        int nCommon = n1;
        if (n2 == nCommon) {
            cout << "Amount of columns for vector 1 must match amount of rows for vector 2";
            return;
        }
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                for (int k = 0; k < nCommon; k++) {
                    ans[i][j] += arr1[i][k] * arr2[k][j];
                }
            }
        }


        printArray(ans, n1, n2);

    }

}
4

2 回答 2

5

您永远不会为ans. 就像您需要在填充两个输入数组之前为它们分配存储一样,您需要为答案分配存储。

当您尝试写入您无权访问的内存时,会生成分段错误。在这种情况下,由于ans数组没有初始化,它指向随机内存。当你这样做时ans[row][coll] = arr2[row][coll] - arr1[row][coll];,你会得到一个段错误,因为ans[row][col]它指向你的程序空间之外的某个地方。

于 2013-09-30T14:15:32.113 回答
0

问题是您没有为ans数组分配内存,但是您正在以下代码中写入:

for (row = 0; row < q; row++) {
    for (coll = 0; coll < w; coll++) {
        ans[row][coll] = arr2[row][coll] - arr1[row][coll];
    }
}

这就是你有分段错误的原因。首先尝试添加一个块来分配内存ans

于 2013-09-30T14:16:54.103 回答