gpt4 book ai didi

c++ - 如何保持二维数组的总计

转载 作者:行者123 更新时间:2023-11-28 07:22:04 24 4
gpt4 key购买 nike

这是我的一些代码。我正在尝试保留一个二维数组的总计。我有一个随机数生成器来生成二维数组中的 x 和 y 位置。该位置将 2 添加到 x 和 y 位置,而正下方、上方、右侧和左侧的位置将在此处添加 1。这可能会发生多次。我需要将输入到数组中的所有值相加。

我无法运行总计。我不确定如何将输入的值添加到二维数组中。有人知道怎么做吗?

int paintSplatterLoop(int ary [ROWS][COLS])
{
double bagCount,
simCount,
totalCupCount = 0.0;//accumulator, init with 0

double totalRowCount = 0, totalColCount=0;

double simAvgCount = 0;
double cupAvgCount;

for (simCount = 1; simCount <= 1; simCount++)
{
for (bagCount = 1; bagCount <= 2; bagCount++)
{
for (int count = 1; count <= bagCount; count++);
{
int rRow = (rand()%8)+1;
int rCol = (rand()%6)+1;
ary[rRow][rCol]+=2;
ary[rRow-1][rCol]+=1;
ary[rRow+1][rCol]+=1;
ary[rRow][rCol-1]+=1;
ary[rRow][rCol+1]+=1;
}
totalRowCount += ary [rRow][rCol];
totalColCount += rCol;
}

}
totalCupCount = totalRowCount + totalColCount;
cout<<"total cups of paint "<<totalCupCount<<"\n"<<endl;

return totalCupCount;
}

最佳答案

这就是我对二维数组的内容求和的方式:

int sum_array(int array[ROWS][COLS])
{
int sum = 0;

for (int i = 0; i < ROWS; ++i)
{
for (int j = 0; j < COLS; ++j)
{
sum += array[i][j];
}
}

return sum;
}

关于c++ - 如何保持二维数组的总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com