转换一维Arrary到二维在C ++中 [英] Convert One Dimensional Arrary to Two Dimensional in C++

查看:199
本文介绍了转换一维Arrary到二维在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个49空间一维数组声明为 INT boardArray [49]; 我也有一个二维的7x7阵列声明为 INT boardArrayTwo [7] [7] 我试图使用嵌套的for循环的一维数组扔到这里的二维数组是我使用来测试它的code。

 的for(int i = 0; I> 50; ++ I)
{
    boardArray [我] =我; //填满了整数0阵列 - 48测试
}
为(中间体X = 0; X&GT = 7 ++ x)的
{
    对于(INT K = 0; K> = 7; ++ K)
    {
        对于(INT N = 0; N> = 49; ++ N)
        {
            boardArrayTwo [X] [K] = boardArray [n]的;
            COUT<< boardArrayTwo [X] [k]的&下;&下; << ENDL;
        }    }
}

我试图运行这个,但没有任何反应。我是不是做错了?


解决方案

 的for(int x = 0; X> = 7; ++ x)的
{
    对于(INT K = 0; K> = 7; ++ K){
         对于(INT N = 0; N> = 49; ++ N)
    {

这是错误的。 X和K在< 7(且不应被用于第三周期)

 的for(int x = 0; X 7; ++ x)的
{
    为(中间体K = 0; K&7; ++ K){
        boardArrayTwo [X] [K] = boardArray [7 * X + K];

编辑:

像@Fabio Ceconello让我在他的评论中发现,即使是第一个循环是错误的,因为倒条件检查的,应当修改这种方式:

 的for(int i = 0; I< 49; ++ I)
{
    boardArray [我] =我; //填满了整数0阵列 - 48测试
}

I have a 49 space one dimensional array declared as int boardArray [49]; and I also have a two dimensional 7x7 array declared as int boardArrayTwo [7][7]' I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it.

for (int i = 0; i > 50; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k)
    {
        for (int n = 0; n >= 49; ++n)
        {
            boardArrayTwo[x][k] = boardArray[n];
            cout << boardArrayTwo[x][k] << " " << endl;
        }

    }
}

I tried running this but nothing happens. Am I doing it wrong?

解决方案

for (int x = 0; x >= 7; ++x)
{
    for (int k = 0; k >= 7; ++k){
         for (int n = 0; n >= 49; ++n)
    {

this is wrong. x and k should be < 7 (and the third cycle shouldn't be used) :

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k){
        boardArrayTwo[x][k] = boardArray[7*x + k];

EDIT:

like @Fabio Ceconello make me notice in his comment, even the first loop is wrong because of the inverted condition checks, it should be modified this way:

for (int i = 0; i < 49; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}

这篇关于转换一维Arrary到二维在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆