如何保持二维数组的运行总数
how to keep a running total of a 2d array
这是我的一些代码。我正在尝试保持 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;
}
相关文章:
- 在程序继续C++时保持进程运行
- 为什么我的代码在运行时保持循环
- 线程保持运行并执行命令
- 骰子滚轮随机循环每次运行时都保持相同的值
- 如何制作硬编码堆栈,而不是硬编码并保持程序正常运行
- 运行功能并保持循环运行C
- 如何保持服务器运行(cpprestsdk - casablanca)
- 如何将控制台输出的文本颜色更改为红色,但如果从Powershell或CMD运行,背景将保持相同的颜色
- 第一个进程保持运行而后续进程仅传递参数的最佳 IPC 方法是什么?
- 我想让程序保持运行,直到用户输入 n 退出程序
- 循环保持运行
- 如何在选择错误选项时保持程序运行而不退出
- 将一个大文件加载到内存中并在程序的所有运行时间内保持它是错误的
- 在不同的运行中保持对象状态
- 在Windows Phone 8.1中保持应用程序在锁屏下运行
- 如何保持二维数组的运行总数
- 只要应用程序运行,是否可以接受保持错误日志打开状态
- 如何保持进程运行
- 我怎样才能保持我的运行函数,直到我单击 c++ 形式的按钮
- 如何让窗口在C++代码运行后保持打开状态