如何保持二维数组的运行总数

how to keep a running total of a 2d array

本文关键字:运行 何保持 二维数组      更新时间:2023-10-16

这是我的一些代码。我正在尝试保持 2d 数组的运行总数。我有一个随机数生成器来生成 2d 数组中的 x 和 y 位置。该位置在 X 和 Y 位置上加了一个 2,正下方、上方、右侧和左侧的位置在那里加了一个 1。这可能会发生多次。我需要将所有输入到数组中的值相加。

我无法使运行总计正常工作。 我不确定如何将输入的值添加到 2D 数组中。 有谁知道如何做到这一点?

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