C++ 2D array to 1D array

后端 未结 4 1554
春和景丽
春和景丽 2020-12-17 00:23

I am attempting to convert a 2D array to 1D. I\'m extremely new to C/C++ but I think it\'s very important to learn how to convert a 2D array to 1D. So here I am stumbling u

4条回答
  •  别那么骄傲
    2020-12-17 01:03

    You are right with your supposition:

    The cycle should be like:

    for (q = 0; q < n; q++)
    {
        for (t = 0; t < m; t++)
        {
            b[q * m + t] = a[q][t];
        }
    }
    

    It is always easier to consider such conversions from the view point of the higher dimension array. Furthermore with your code you did not actually modify i or j in the b assigning cycle, so you should not expect different values to be assigned to the different array members of b.

提交回复
热议问题