如何从函数返回矩阵(二维数组)? (C) [英] How to return matrix (2D array) from function? (C)

查看:115
本文介绍了如何从函数返回矩阵(二维数组)? (C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个生成宾果板的函数,我想返回宾果板。



我没有想到,它不起作用。



这里是函数:

$ p $ int ** generateBoard() {
int board [N] [M],i,j,fillNum;
布尔值存在=真;
// initilize seed
srand(time(NULL)); (i = 0; i for(j = 0; j exists = True;
while(exists){
fillNum = rand()%MAX_RANGE + 1; //限制为MAX_RANGE
if(beenAdded(board,fillNum)== Exist){
continue;
} else {
board [i] [j] = fillNum;
exists = False;
}
}
}
}
return board;

$ / code>

我在return board行有一个编译错误(红色的子行) 。

有没有办法使用structs \动态分配来返回2D数组



我正在使用Microsoft Visual C ++ Express 2010。

解决方案

有人必须拥有该板的内存> somewhere ,更重要的是,所有权必须延伸回该函数的调用者。如果没有动态分配,你唯一的另一种真正的选择是将它作为in / out参数发送给函数。

  void generateBoard size_t N,size_t M,int board [N] [M])
{
int i,j,fillNum;
布尔值存在=真;
// initilize seed
srand(time(NULL)); (i = 0; i for(j = 0; j exists = True;
while(exists){
fillNum = rand()%MAX_RANGE + 1; //限制为MAX_RANGE
if(beenAdded(board,fillNum)== Exist){
continue;
} else {
board [i] [j] = fillNum;
exists = False;
}
}
}
}
}

并从你的调用者这样调用:

  int main()
{
const size_t N = 10;
const size_t M = 10;
int board [N] [M];

generateBoard(N,M,board);

...
}

我也会考虑重新定位 srand()调用 main()中的启动代码。理想情况下,它绝不会处于某种可能重复调用的函数中,并且应该保证每个进程执行时只执行一次。 (注意:我真的不记得它是否是每个线程执行一次,但是在你的编码学习曲线的这一点上,我猜测多线程不在雷达之中)。 p>

最后,你的随机填充循环是不必要的重复。生成你显然想要做的事情有更好的选择:创建一组现有数字的随机排列。正如你所写的,你可以花一些时间试图填补最后几个时间段,这取决于比较 MAX_RANGE (N * M)


I created a function that generates a bingo board and I want to return the bingo board.

as I didn't expect for , it doesn't work.

here is the function:

int** generateBoard() {
    int board[N][M], i, j , fillNum;
    Boolean exists = True;
    // initilize seed
    srand(time(NULL));
    // fill up..
    for(i = 0; i < N; ++i) {
        for(j = 0; j < M; ++j) {
            exists = True;
            while(exists) {
                fillNum = rand()%MAX_RANGE + 1; // limit up to MAX_RANGE
                if(beenAdded(board, fillNum) == Exist) {
                    continue;
                } else {
                    board[i][j] = fillNum;
                    exists = False;
                }
            }
        }
    }
    return board;
}

I have a compilcation error (the red subline) at "return board" line.

is there way to return a 2D array without using structs \ dynamic allocations?

I'm using Microsoft Visual C++ Express 2010.

解决方案

Someone has to own the memory of that board somewhere, and more importantly, that ownership must extend back to the caller of this function. Without dynamic allocation, your only other real alternative is to send it into the function as in in/out parameter.

void generateBoard(size_t N, size_t M, int board[N][M])
{
    int i, j , fillNum;
    Boolean exists = True;
    // initilize seed
    srand(time(NULL));
    // fill up..
    for(i = 0; i < N; ++i) {
        for(j = 0; j < M; ++j) {
            exists = True;
            while(exists) {
                fillNum = rand()%MAX_RANGE + 1; // limit up to MAX_RANGE
                if(beenAdded(board, fillNum) == Exist) {
                    continue;
                } else {
                    board[i][j] = fillNum;
                    exists = False;
                }
            }
        }
    }
}

and invoke like this from your caller:

int main()
{
    const size_t N = 10;
    const size_t M = 10;
    int board[N][M];

    generateBoard(N,M,board);

    ...
}

I would also consider relocatting the srand() call to the startup code in main(). It should ideally never be in some potentially repeat-callable function, and should be guaranteed to only be executed once per process execution. (note: I honestly can't remember if it is once per thread execution, but at this point in your coding learning curve I'm guessing multi-threading is not on the radar yet).

Finally, your random-fill loop is needlessly repetitious. There are better alternatives generating what you're apparently trying to do: create a random permutation of an existing set of numbers. As written you could spin for some time trying to fill those last few slots, depending on how much larger MAX_RANGE is compared to (N*M).

这篇关于如何从函数返回矩阵(二维数组)? (C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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