The main function to initialize a two-dimensional array represents a matrix array, and each element is output, and then call sub-
function was calculated and the elements of each row, and will be directly stored in the first element of each row , After returning to the main function, the sum
of the elements in each row is output.
#include <iostream>
using namespace std;
void rowSum(int a[][4], int nRow)
{
for (int i = 0; i < nRow; i++)
{
for(int j = 1; j < 4; j++)
a[i][0] += a[i][j];
}
}
int main()
{
//定义并初始化数组
int table[3][4] = {
{
1, 2, 3, 4}, {
2, 3, 4, 5}, {
3, 4, 5, 6}};
//输出数组元素
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
cout << table[i][j] << " ";
cout << endl;
}
rowSum(table, 3); //调用子函数,计算各行和
//输出计算结果
for (int i = 0; i < 3; i++)
cout << "Sum of row " << i << " is " << table[i][0] << endl;
return 0;
}