创建和返回锯齿状数组时出现问题(错误std :: bad_array_new_length)

时间:2019-05-08 00:55:49

标签: c++ pointers jagged-arrays

对于此作业问题,我们需要使用教授提供的代码创建一个新的锯齿状数组,打印该数组,并计算该数组内容的最大值,最小值和总和。我们只允许编辑createAndReturnJaggedArray()printAndThenFindMaxMinSum(int**,int*,int*,int*)函数,因为为我们提供了其余代码,因此我们可以检查是否获得正确的输出。

我能够运行该程序,但是在打印了初始字符串后,它将终止该程序,并给我一个错误terminate called after throwing an instance of 'std::bad_array_new_length' what(): std::bad_array_new_length。我相信问题出在我创建锯齿状数组和为数组的列部分分配内存的过程中,但是我使用了我们作为参考的注释,却不知道问题出在哪里。整个程序在下面提供。感谢您的帮助!

编辑/注意:我们尚未学习矢量,因此我们不允许使用它们。

#include <iostream>
#include <climits>
using namespace std;

class JaggedArray {

public:

int numRows;
int *numColumnsInEachRow;
JaggedArray() {
    numRows = 11;
    numColumnsInEachRow = new int[numRows];
    for (int i = 0; i < numRows; i++) {
        if (i <= numRows / 2) {
            numColumnsInEachRow[i] = i + 1;
        } else {
            numColumnsInEachRow[i] = numRows - i;
        }
    }
    readComputeWrite();
}

int **createAndReturnJaggedArray() { // COMPLETE THIS FUNCTION
    int **A = new int*[numRows];
    for(int i=0;i<numRows;i++){ //allocate columns in each row
        A[i] = new int[numColumnsInEachRow[i]];
        for(int j=0;j<numColumnsInEachRow[i];j++){
            if(i <= numRows/2)
                A[i][j] = (i + j);
            else
                A[i][j] = -1 * (i+j);
        }
    }
    return A;
}

void printAndThenFindMinMaxSum(int **A, int *maxPtr, int *minPtr, int *sumPtr) { // COMPLETE THIS FUNCTION
    maxPtr = new int[INT_MIN];
    minPtr = new int[INT_MAX];
    sumPtr = 0;

    for(int i=0;i<numRows;i++){
       for(int j=0;j<numColumnsInEachRow[i];j++){
           //1. print array
            if (j == (numColumnsInEachRow[i]-1))
                cout << A[i][j] << endl;
            else
                cout << A[i][j] << " ";

           //2. compute max, min, and sum
           sumPtr += A[i][j];
           if (A[i][j] > *maxPtr)
                maxPtr = new int[A[i][j]];
           if (A[i][j] < *minPtr)
                minPtr = new int[A[i][j]];
        }
    }
}

void print(int max, int min, int sum) {
    cout << endl;
    cout << "Max is " << max << "\n";
    cout << "Min is " << min << "\n";
    cout << "Sum is " << sum << "\n";
}

void readComputeWrite() {
    int max, min, sum;
    int **A = createAndReturnJaggedArray();
    cout << "*** Jagged Array ***" << endl;
    printAndThenFindMinMaxSum(A, &max, &min, &sum);
    print(max, min, sum);
}
};

int main() {
    JaggedArray jaf;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

正如@ user4581301所暗示的,您的问题出在printAndThenFindMinMaxSum中。只需将其更改为以下即可解决您的问题:

void printAndThenFindMinMaxSum(int **A, int &maxPtr, int &minPtr, int &sumPtr) { // COMPLETE THIS FUNCTION
    maxPtr = INT_MIN;
    minPtr = INT_MAX;
    sumPtr = 0;

    .
    .
    .
            sumPtr += A[i][j];
            if (A[i][j] > maxPtr)
                maxPtr = A[i][j];
            if (A[i][j] < minPtr)
                minPtr = A[i][j];
        }
    }
}

我们还需要将readComputeWrite更改为:

void readComputeWrite() {
    int max, min, sum;
    int **A = createAndReturnJaggedArray();
    cout << "*** Jagged Array ***" << endl;
    printAndThenFindMinMaxSum(A, max, min, sum);
    print(max, min, sum);
}

我还建议将名称minPtrmaxPtrsumPtr更改为更合适的名称,因为它们在此时还不是指针,而是表示原始值。

您会注意到,我更改了指向引用的指针,因为这是对这种类型的操作的更自然的适应。本质上,通过引用传递使用户可以直接方式对传递的值进行操作,而无需进行繁琐的任务来确保您在适当的时间取消对事物的引用。它还允许人们以一种不太容易出错的方式进行操作。

同样,如@ user4581301精明指出的那样,此分配的目的可能是处理指针。因此,如果OP无法使用引用,则需要更改一些内容。观察:

void printAndThenFindMinMaxSum(int **A, int *maxPtr, int *minPtr, int *sumPtr) { // COMPLETE THIS FUNCTION
    *maxPtr = INT_MIN;  // Make sure to deference before assigning
    *minPtr = INT_MAX;  // Make sure to deference before assigning
    *sumPtr = 0;  // Make sure to deference before assigning

    for(int i=0;i<numRows;i++){
        for(int j=0;j<numColumnsInEachRow[i];j++){
            //1. print array
            if (j == (numColumnsInEachRow[i]-1))
                cout << A[i][j] << endl;
            else
                cout << A[i][j] << " ";

            //2. compute max, min, and sum
            *sumPtr += A[i][j];   // Make sure to deference before assigning
            if (A[i][j] > *maxPtr)  // Make sure to deference before comparing
                *maxPtr = A[i][j];  // Make sure to deference before assigning
            if (A[i][j] < *minPtr)  // Make sure to deference before comparing
                *minPtr = A[i][j];  // Make sure to deference before assigning
        }
    }
}

readComputeWrite可以保持OP最初的尝试不变。

在OP的代码中,他们主要是在分配/比较之前忘记遵循。

相关问题