需要1-100随机数插入一个三维阵列 [英] Need to insert random numbers of 1-100 into a three dimensional array

查看:129
本文介绍了需要1-100随机数插入一个三维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建,其中1到100之间的随机数被放置在一个三维阵列的每一维的程序。数组是不同大小的,因此到目前为止只遇到过在执行时崩溃。试着与一维数组规模较小,并得到它的工作的罚款。似乎无法对较大规模的翻译。我的code到目前为止...

 诠释常量S​​TOCK_AMOUNT = 1000,DAY_AMOUNT = 366,TIME_AMOUNT = 480;
INT randomGenerator();
无效randomInsert(中间体ARRAY0 [DAY_AMOUNT] [TIME_AMOUNT] [STOCK_AMOUNT]);诠释的main()
{
    INT立方体[DAY_AMOUNT] [TIME_AMOUNT] [STOCK_AMOUNT];
    函数srand((无符号)时间(0));
    randomInsert(立方体);
    返回0;
}无效randomInsert(INT ARRAY0 [DAY_AMOUNT] [TIME_AMOUNT] [STOCK_AMOUNT])
{
    对于(INT COUNT1 = 0;&COUNT1 LT; D​​AY_AMOUNT; COUNT1 ++)
    {
        对于(INT COUNT2 = 0;&COUNT2 LT; TIME_AMOUNT; COUNT2 ++)
        {
            对于(INT共3个记录= 0;&共3个记录下; STOCK_AMOUNT,共3个记录++)
            {
                INT randomGenerator();
                ARRAY0 [COUNT1] [COUNT2] [共3个记录] = randomGenerator();
                COUT<< ENDL;
            }
        }
    }
}INT randomGenerator()
{
    INT randNum;
    INT LOWERLIMIT = 1;
    INT UPPERLIMIT = 100;
    randNum =(RAND()%UPPERLIMIT)+ LOWERLIMIT;
    返回randNum;
}


解决方案

您似乎超过堆栈大小。阵列,堆栈上创建的,持有约175M整数,即,左右的内存700MB。你需要设置编译选项增加栈的大小。

编辑:此外,请注意,把如此巨大的数组在栈上通常被认为是不好的做法。理想情况下,使用STL的载体,那就是对付阵列现代的方式。

I need to create a program in which random numbers between 1 and 100 are placed in each dimension of a 3D array. The arrays are of varying sizes, and thus far have only encountered crashes upon execution. Tried on a smaller scale with a 1D array and got it to work fine. Cant seem to translate on larger scale. My code so far...

int const STOCK_AMOUNT = 1000, DAY_AMOUNT = 366, TIME_AMOUNT = 480;
int randomGenerator();
void randomInsert(int array0[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT]);

int main()
{
    int cube[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT];
    srand((unsigned)time(0));
    randomInsert(cube);
    return 0;
}

void randomInsert(int array0[DAY_AMOUNT][TIME_AMOUNT][STOCK_AMOUNT])
{
    for (int count1 = 0; count1 < DAY_AMOUNT; count1++)
    {
        for (int count2 = 0; count2 < TIME_AMOUNT; count2++)
        {
            for (int count3 = 0; count3 < STOCK_AMOUNT; count3++)
            {
                int randomGenerator();
                array0[count1][count2][count3] = randomGenerator();
                cout << endl;
            }
        }
    }   
}

int randomGenerator()
{
    int randNum;
    int lowerLimit = 1;
    int upperLimit = 100;
    randNum = (rand() % upperLimit) + lowerLimit;
    return randNum;
}

解决方案

You seem to be exceeding stack size. your array, created on the stack, holds about 175M integers, that is, about 700MB of memory. You need to setup compilation options to increase the stack size.

EDIT: moreover, please be aware that putting such huge arrays on the stack is generally considered bad practice. Ideally, use STL vectors, that is the modern way to deal with arrays.

这篇关于需要1-100随机数插入一个三维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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