0

该程序假设采用size一定数量的数字对,它们是二维布尔数组中的坐标。触发的每个坐标都会将值切换为TRUE。出于某种原因,我在最后一行以及最后一行的最后一个空格中都有错误。有任何想法吗?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main(){
    const int size = 10;
    int *x = new int [size];
    int *y = new int [size];
    bool table[size][size] =  {{false}};
    for(int i = 1 ; i <= size; i++){
        cin >> x[i] >> y[i];
        if(x[i] <= size && y[i] <= size){
            table[x[i]][y[i]] = true;
        } else{
            cout << "invalid input \n";
            i--;
        }
    }


    for(int a = 1; a <= size; a++){
        for(int b= 1; b <= size; b++){
            cout << table[a][b] << " ";
        }
        cout << "\n";
    }
    return 0;
}
4

1 回答 1

3

对于大小为 N 的数组,数组索引从 0 到 N-1。您正在写入超出数组的范围。你的循环应该是这样的

for(int i = 0; i < size; ++i) { ..... }
于 2013-01-15T19:55:31.337 回答