输出 2D 数组及其元素的总和
Output a 2D array and the sum of its elements
在这个程序中,我尝试添加二维数组的元素,打印整个数组,并使用函数打印其总和。我知道我非常接近,但是当我尝试打印总和时,它不起作用。这是我的代码。
void DisplayB(int b[][4], int col, int row, int total);
int main()
{
int total = 0;
int b[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
DisplayB(b, 3, 4, total);
}
void DisplayB(int b[][4], int col, int row, int total)
{
total = 0;
cout << "Array c using the display function: " << endl;
for (int i = 0; i<col; i++)
{
for (int j = 0; j<row; j++)
{
cout << b[i][j] << " ";
total+=b[i][j];
}
cout<<total;
cout << endl;
}
}
当我在编译器中运行它时,我最终得到这个:
1 2 3 4 10
5 6 7 8 36
9 10 11 12 78
看起来正在发生的事情是它找到每行的总和并将其添加到前一行的总和中。有什么想法吗?
你的函数非常混乱。
void DisplayB(int b[][4], int col, int row, int total)
{
total = 0;
cout << "Array c using the display function: " << endl;
for (int i = 0; i<col; i++)
{
for (int j = 0; j<row; j++)
{
cout << b[i][j] << " ";
total+=b[i][j];
}
cout<<total;
cout << endl;
}
}
例如,原始数组有三行,但您命名的函数的相应参数如 col。反之亦然,您命名的列数就像函数声明中的行一样。
同样在外部循环中,您输出数组的部分总和。例如
1 2 3 4 10
5 6 7 8 36
9 10 11 12 78
10 是 1、2、3、4 的总和。36 是 1、2、3、4 和 5、6、7、8 的总和等等。
我认为您想在解散数组后输出总和。考虑到参数总计是多余的。您可以简单地在函数中声明一个名称为 total 的局部变量。
我将函数分为两个函数。一个用于输出数组,另一个用于计算数组所有元素的总和。
在这种情况下,程序看起来像
#include <iostream>
#include <iomanip>
const size_t N = 4;
void DisplayB( const int b[][N], size_t rows )
{
std::cout << "Array b using the display function:n" << std::endl;
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
std::cout << std::setw( 2 ) << b[i][j] << ' ';
}
std::cout << std::endl;
}
}
long long int Accumulate( const int b[][N], size_t rows )
{
long long int total = 0;
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
total += b[i][j];
}
}
return total;
}
int main()
{
int b[3][N] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
DisplayB( b, 3 );
std::cout << "nSum of elements of the array is "
<< Accumulate( b, 3 )
<< std::endl;
}
程序输出为
Array b using the display function:
1 2 3 4
5 6 7 8
9 10 11 12
Sum of elements of the array is 78
void DisplayB(int b[][4], int col, int row, int total)
{
total = 0;
cout << "Array c using the display function: " << endl;
for (int i = 0; i<col; i++)
{
int sub_total = 0;
for (int j = 0; j<row; j++)
{
cout << b[i][j] << " ";
sub_total+=b[i][j];
}
total += sub_total;
cout<<"Sub total "<<sub_total;// Optional
cout << endl;
}
cout<<"Over all total "<<total;
}
#include <iostream>
using std::cout;
using std::endl;
void DisplayB(int b[][4], int row, int col, int total);
int main()
{
int total = 0;
int b[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
DisplayB(b, 3, 4, total);
}
void DisplayB(int b[][4], int row, int col, int total)
{
total = 0;
cout << "Array c using the display function: " << endl;
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
cout << b[i][j] << " ";
total+=b[i][j];
}
//This is for row-wise addition
//cout<< "nTotal of row " << i << " is : " << total << endl;
//total = 0;
cout << endl;
}
cout<<"nTotal = " << total << endl;
}
相关文章:
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 交换函数不是在 C++ 中交换 2D 数组的元素
- 如何将所有元素初始化为 -1 的 2D 数组?
- 如何在 2d 矢量 c++ 中复制元素并将其放在原始元素旁边
- 如何使用指针C++将元素从2D数组复制到1D数组
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 如何计算大小为 1000 x 1000 的 2D 数组中 2 个元素之间的步幅?C++
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- 查找 2D 向量的位置,该向量是类C++的一个元素
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- 创建 2D 数组类:如何访问 2D 数组类(如 array[x][y))中的元素
- 返回C 中2D阵列的对角线元素总和
- 如何使用 cpp 消除 2d 数组中的边框元素
- 如何在 2D 字符数组中打印 [x][y] 元素的地址?(C++)
- 排列 2D 数组中的元素
- 从2D矢量中找出最小尺寸的向量元素的更好方法
- 静态2D数组,添加元素
- 从2D数组C 中删除单个元素
- 分割故障:从2D矢量中随机删除元素