2015-11-04 31 views
0

我需要创建一个程序,其中1到100之间的随机数放置在3D数组的每个维中。这些数组的大小各不相同,因此在执行时只遇到崩溃。用一维数组进行小规模尝试,并使其正常工作。似乎不能大规模翻译。我的代码到目前为止...需要在三维数组中插入1-100的随机数

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; 
} 
+0

你的内部循环可能不需要cout语句。 (366,000 * 480换行!)而且,当你在这里时,请描述你认为运行程序的输出应该是什么? –

+0

同样在内部循环中,您不需要转发声明randomGenerator()函数,该函数已在代码顶部完成 –

+0

,因此在阅读某些注释和进一步询问professor.stock_amount = 100后数组太大了day_amount = 50 time_amount = 8。基本上是一个olap立方体[Stock_amount] [day_amount] [time_amount] – SCSUjer

回答

3

您似乎超过堆栈大小。你的数组,在堆栈上创建,拥有大约175M整数,即大约700MB的内存。您需要设置编译选项以增加堆栈大小。编辑:另外,请注意,将如此巨大的数组放在堆栈上通常被认为是不好的做法。理想情况下,使用STL向量,这是处理数组的现代方法。

+1

增加堆栈大小通常不是一个好的解决方案。 – bames53

+0

@ bames53是真的,但这是另一场辩论。当然,STL会是一个更“现代”的方法,它会将数组移到堆中,但这个答案是对所提供代码的特定评估。我将这句话添加到了考虑你的评论的答案中。 :) –

1

下面是使用STL的一个更复杂的版本:

#include <cassert> 
#include <cstddef> 
#include <cstdint> 
#include <ctime> 
#include <functional> 
#include <iostream> 
#include <random> 

using std::cout; 
using std::endl; 

// The size of a type that can hold values from 1 to 100. Used for storage. (Copy to int for calculation.) 
using elem_t = int_least8_t; 
// Lower and upper bounds: 
constexpr int lb = 1; 
constexpr int ub = 100; 

constexpr size_t stocks = 100, days = 300, times = 400; 
using pricearray_t = elem_t[days][times][stocks]; 

pricearray_t& init_prices() 
/* Returns a heap-allocated array that must be deleted with delete[]. */ 
{ 
    // This ugly little cast is brought to us by the C++ rules for array types. 
    pricearray_t &to_return = *(pricearray_t*) new pricearray_t; 
    const std::default_random_engine::result_type seed = std::time(NULL) * CLOCKS_PER_SEC + std::clock(); 
    std::default_random_engine generator(seed); 
    std::uniform_int_distribution<int_fast8_t> distribution(lb, ub); 

    auto x = std::bind(distribution, generator); 

    for (size_t i = 0; i < days; ++i) 
    for (size_t j = 0; j < times; ++j) 
     for (size_t k = 0; k < stocks; ++k) 
     to_return[i][j][k] = static_cast<elem_t>(x()); 

    return to_return; 
} 

int main(void) 
{ 
    const pricearray_t &prices = init_prices(); 

    long long int sum = 0; 
    for (size_t i = 0; i < days; ++i) 
    for (size_t j = 0; j < times; ++j) 
     for (size_t k = 0; k < stocks; ++k) { 
     const int x = prices[i][j][k]; 
     assert(x >= lb); 
     assert(x <= ub); 
     sum += x; 
     } 

    cout << "The mean is " << static_cast<double>(sum)/days/times/stocks << "." << endl; 

    delete[] &prices; 
    return EXIT_SUCCESS; 
} 

下面是一个使用智能指针来自动管理存储一个版本:

#include <cassert> 
#include <cstddef> 
#include <cstdint> 
#include <ctime> 
#include <functional> 
#include <iostream> 
#include <memory> 
#include <random> 

using std::cout; 
using std::endl; 

// The size of a type that can hold values from 1 to 100. Used for storage. (Copy to int for calculation.) 
using elem_t = int_least8_t; 
// Lower and upper bounds: 
constexpr int lb = 1; 
constexpr int ub = 100; 

constexpr size_t stocks = 100, days = 300, times = 400; 
// The unique_ptr type doesn’t play nicely with arrays of known size. 
using pricearray_t = elem_t[][times][stocks]; 

std::unique_ptr<pricearray_t> init_prices() 
/* Returns a managed pointer to an array of uniformly-distributed values. 
*/ 
{ 
    // This smart pointer will use its move constructor to avoid copying the entire array. 
    std::unique_ptr<pricearray_t> to_return = std::make_unique<pricearray_t>(days); 
    const std::default_random_engine::result_type seed = std::time(NULL) * CLOCKS_PER_SEC + std::clock(); 
    std::default_random_engine generator(seed); 
    std::uniform_int_distribution<int_fast8_t> distribution(lb, ub); 
    auto x = std::bind(distribution, generator); 

    for (size_t i = 0; i < days; ++i) 
    for (size_t j = 0; j < times; ++j) 
     for (size_t k = 0; k < stocks; ++k) 
     to_return[i][j][k] = static_cast<elem_t>(x()); 

    return to_return; 
} 

int main(void) 
{ 
/* The contents of the smart pointer will be deleted automatically when it goes out of scope. 
    */ 
    const std::unique_ptr<pricearray_t> prices = init_prices(); 

    long long int sum = 0; 
    for (size_t i = 0; i < days; ++i) 
    for (size_t j = 0; j < times; ++j) 
     for (size_t k = 0; k < stocks; ++k) { 
     const int x = prices[i][j][k]; 
     assert(x >= lb); 
     assert(x <= ub); 
     sum += x; 
     } 

    cout << "The mean is " << static_cast<double>(sum)/days/times/stocks << "." << endl; 

    return EXIT_SUCCESS; 
} 
+0

这是如何有效地使用C++ 11及以后的一个很好的例子。 +1! – Cebtenzzre

+0

@ZirconiumHacker谢谢。我添加了一个使用智能指针的版本。 – Davislor

0

看来规格由提供的原始立方教授显然太大了......奇怪教授怎么会不明白这一点?缩减规格,现在有一些工作。主要股票应该为每只股票(100)找到股票价格的平均值(50)。

#include<iostream> 
#include<ctime> 
#include <cstdlib> 
#include <fstream> 
using namespace std; 
const int STOCK_AMOUNT = 100, DAY_AMOUNT = 50, TIME_AMOUNT = 8; 
int randomGenerator(); 
void priceGenerator(int [STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT]); 


int main() 
{ 
ofstream outputFile; 
int cube[STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT]; 
double total; 
srand((unsigned)time(0)); 
priceGenerator(cube); 
outputFile.open("Average_Day_Price"); 
for (int row = 0; row < STOCK_AMOUNT; row++) 
{ 
total=0; 
for (int col = 0; col < DAY_AMOUNT; col++) 
{  
for (int layer = 0; layer < TIME_AMOUNT; layer++) 
{   
total = cube[row][col][layer]; 
double average = (total/TIME_AMOUNT); 
outputFile << "STOCK Id:" << (row+1) << "--" << "--" << average <<endl:    
} 
} 
} 
outputFile.close(); 
return 0; 
} 
void priceGenerator(int array0[STOCK_AMOUNT][DAY_AMOUNT][TIME_AMOUNT]) 
{ 
int i,y, z; 
for (i = 0; i < STOCK_AMOUNT; i++) 
{ 
for (y = 0; y < DAY_AMOUNT; y++) 
{ 
for (z = 0; z < TIME_AMOUNT; z++)  
{ 
int randNum; 
int lowerLimit = 1; 
int upperLimit = 100; 
randNum = (rand() % upperLimit) + lowerLimit; 
array0[i][y][z] = randNum;   
} 
} 
} 
}