我试图打印一个伪多维数组(不要问为什么是XD),但是当我这样做的时候出于某种原因。
#include <iostream>
#define Row_sz 3
#define Col_sz 4
using namespace std;
int main()
{
    int row, col;
    int arr[Row_sz*Col_sz];
    cout<<"Printing a multi-dimensional array."<<endl;
    do{
        cout<<"Enter the number of rows: "<<endl;
        cin>>row;
    }while(row>Row_sz||row<0);
    do{
        cout<<"Enter the number of columns: "<<endl;
        cin>>col;
    }while(col>Col_sz||col<0);
    for (int x=0;x<row;x++){
        for(int y=0;y<col;y++){
            cout<<"Enter the value: "<<endl;
            cin>>arr[x*y];
        }
    }
    cout<<"The array matrix: "<<endl;
    for (int x=0;x<row;x++){
        for(int y=0;y<col;y++){
            cout<<arr[x*y]<<" ";
        }
        cout<<endl;
    }
}例如:5,4,3,2,1,6,7,8,9,11,12,13
                                                         9 9 9 9
                                                         9 6 11 8
                                                         9 11 12 13而不是:
                                                 5 4 3 2
                                                 1 6 7 8
                                                 9 11 12 13或者类似的东西。
发布于 2015-10-24 16:53:47
替换
 x*y使用
x*Col_sz+y*运算符是乘法。您的数组有12个元素长。你想要填充元素0,1,2,3,4,5,6…11。如果你看看x*y产生了什么,你会发现这不是你想要的。
https://stackoverflow.com/questions/33320389
复制相似问题