Ошибка сегментирования при заполнении двухмерного массива

При заполнении двухмерного массива возникает типичная ситуация.

введите сюда описание изображения

#include <iostream>
#include <iomanip>

using namespace std;

void ScanTable(int **table, int LEVEL, int PLAYER) {
    for (int row = 0; row < LEVEL; row++) {
        cout << "LEVEL " << row + 1 << ":\n";
        for (int col = 0; col < PLAYER; col++) {
            cout << "\tPlayer " << col + 1 << ": ";
            cin >> table[row][col];
        }
    }
}

void PrintTable(int **table, int LEVEL, int PLAYER) {
    cout << "\n\n";
    for (int i = 0; i < LEVEL; i++, cout << "\n\n") {
        for (int j = 0; j < PLAYER; j++) {
            cout << setw(5) << table[i][j];
        }
    }
}

void StatisticsOfLevels(int **table, int LEVEL, int PLAYER){

    float AverageOfTimeLevel[7];

    for (int i = 0; i < LEVEL; i++){
        int AllTimeOfLevel = 0;
        for (int j = 0; j < PLAYER; j++){
            AllTimeOfLevel += table[i][j];
        }
        AverageOfTimeLevel[i] = (float)AllTimeOfLevel/PLAYER;
    }
    for (int i = 0; i < 7; i++) {
        cout << "Average" << i + 1
        << ": " << AverageOfTimeLevel[i] << '\n';
    }

    float hard;
    float easy;
    hard = AverageOfTimeLevel[0];
    easy = AverageOfTimeLevel[0];
    for (int i = 1; i < LEVEL; i++){
        if (hard < AverageOfTimeLevel[i]){
            hard = AverageOfTimeLevel[i];
        }
        if (easy > AverageOfTimeLevel[i]){
            easy = AverageOfTimeLevel[i];
        }
    }
    cout << "E: " << easy;
    cout << "\nH: " << hard;

    for (int i = 0; i < LEVEL; i++){
        if (easy == AverageOfTimeLevel[i]){
            cout << "\nEASY LEVEL " << i + 1;
        }
        if (hard == AverageOfTimeLevel[i]){
            cout << "\nHARD LEVEL " << i + 1;
        }
   }
}
void StatisticsOfTime(int **table, int LEVEL, int PLAYER) {
    int BestTimeStatistics[7];
    int BadTimeStatistics[7];

    for (int i = 0; i < LEVEL; i++) {
        int BestTime = table[i][0];
        int BadTime = table[i][0];
        for (int j = 0; j < PLAYER; j++) {
            if (BestTime > table[i][j]) {
                BestTime = table[i][j];
            }
            if (BadTime < table[i][j]) {
                BadTime = table[i][j];
            }
        }
        BestTimeStatistics[i] = BestTime;
        BadTimeStatistics[i] = BadTime;
    }
    for (int i = 0; i < 7; i++) {
        cout << "BEST TIME Level " << i + 1 << ": "
        << BestTimeStatistics[i] << '\n';
    }
    for (int i = 0; i < 7; i++) {
        cout << "BAD TIME Level " << i + 1 
        << ": " << BadTimeStatistics[i] << '\n';
    }
}
void CheckLimitOfTime (int **table, int i, int j, const int LIMIT_OF_TIME){
    if (table[i][j] > LIMIT_OF_TIME){
        for (int col = 0; col <= j; col++){
            table[i][col] = 0;
        }
    }
}
int main() {
    const int LIMIT_OF_TIME = 3600;
    const int LEVEL = 7;

    int PLAYER;

    cout << "Enter N players: ";
    cin >> PLAYER;

    int **table = new int *[PLAYER];
    for (int i = 0; i < LEVEL; i++)
        table[i] = new int[LEVEL];


    ScanTable(table, LEVEL, PLAYER);

    for (int i = 0; i < LEVEL; i++){
        for (int j = 0; j < PLAYER; j++){
            CheckLimitOfTime(table, i, j, LIMIT_OF_TIME);
        }
    }
    PrintTable(table, LEVEL, PLAYER);

    StatisticsOfTime(table, LEVEL, PLAYER);
    StatisticsOfLevels(table, LEVEL, PLAYER);
}

Ответы (1 шт):

Автор решения: 0x00004

Создаете массив указателей на размер PLAYER но потом вы пробегаетесь по table ссылаясь на размер переменной LEVEL? Это первая ошибка.

Вот правильный код:

 #include <iostream>
    #include <iomanip>

using namespace std;

void ScanTable(int **table, int LEVEL, int PLAYER) {
    for (int row = 0; row < LEVEL; row++) {
        cout << "LEVEL " << row + 1 << ":\n";
        for (int col = 0; col < PLAYER; col++) {
            cout << "\tPlayer " << col + 1 << ": ";
            cin >> table[row][col];
        }
    }
}

void PrintTable(int **table, int LEVEL, int PLAYER) {
    cout << "\n\n";
    for (int i = 0; i < LEVEL; i++, cout << "\n\n") {
        for (int j = 0; j < PLAYER; j++) {
            cout << setw(5) << table[i][j];
        }
    }
}

void StatisticsOfLevels(int **table, int LEVEL, int PLAYER) {

    float AverageOfTimeLevel[7];

    for (int i = 0; i < LEVEL; i++) {
        int AllTimeOfLevel = 0;
        for (int j = 0; j < PLAYER; j++) {
            AllTimeOfLevel += table[i][j];
        }
        AverageOfTimeLevel[i] = (float)AllTimeOfLevel / PLAYER;
    }
    for (int i = 0; i < 7; i++) {
        cout << "Average" << i + 1
            << ": " << AverageOfTimeLevel[i] << '\n';
    }

    float hard;
    float easy;
    hard = AverageOfTimeLevel[0];
    easy = AverageOfTimeLevel[0];
    for (int i = 1; i < LEVEL; i++) {
        if (hard < AverageOfTimeLevel[i]) {
            hard = AverageOfTimeLevel[i];
        }
        if (easy > AverageOfTimeLevel[i]) {
            easy = AverageOfTimeLevel[i];
        }
    }
    cout << "E: " << easy;
    cout << "\nH: " << hard;

    for (int i = 0; i < LEVEL; i++) {
        if (easy == AverageOfTimeLevel[i]) {
            cout << "\nEASY LEVEL " << i + 1;
        }
        if (hard == AverageOfTimeLevel[i]) {
            cout << "\nHARD LEVEL " << i + 1;
        }
    }
}
void StatisticsOfTime(int **table, int LEVEL, int PLAYER) {
    int BestTimeStatistics[7];
    int BadTimeStatistics[7];

    for (int i = 0; i < LEVEL; i++) {
        int BestTime = table[i][0];
        int BadTime = table[i][0];
        for (int j = 0; j < PLAYER; j++) {
            if (BestTime > table[i][j]) {
                BestTime = table[i][j];
            }
            if (BadTime < table[i][j]) {
                BadTime = table[i][j];
            }
        }
        BestTimeStatistics[i] = BestTime;
        BadTimeStatistics[i] = BadTime;
    }
    for (int i = 0; i < 7; i++) {
        cout << "BEST TIME Level " << i + 1 << ": "
            << BestTimeStatistics[i] << '\n';
    }
    for (int i = 0; i < 7; i++) {
        cout << "BAD TIME Level " << i + 1
            << ": " << BadTimeStatistics[i] << '\n';
    }
}
void CheckLimitOfTime(int **table, int i, int j, const int LIMIT_OF_TIME) {
    if (table[i][j] > LIMIT_OF_TIME) {
        for (int col = 0; col <= j; col++) {
            table[i][col] = 0;
        }
    }
}
int main() 
{
    const int LIMIT_OF_TIME = 3600;
    const int LEVEL = 7;

    int PLAYER;

    cout << "Enter N players: ";
    cin >> PLAYER;

    int **table = new int *[LEVEL];
    for (int i = 0; i < LEVEL; i++)
        table[i] = new int[PLAYER];


    ScanTable(table, LEVEL, PLAYER);

    for (int i = 0; i < LEVEL; i++) {
        for (int j = 0; j < PLAYER; j++) {
            CheckLimitOfTime(table, i, j, LIMIT_OF_TIME);
        }
    }
    PrintTable(table, LEVEL, PLAYER);

    StatisticsOfTime(table, LEVEL, PLAYER);
    StatisticsOfLevels(table, LEVEL, PLAYER);


}
→ Ссылка