输出 2D 数组及其元素的总和

Output a 2D array and the sum of its elements

本文关键字:元素 2D 数组 输出      更新时间:2023-10-16

在这个程序中,我尝试添加二维数组的元素,打印整个数组,并使用函数打印其总和。我知道我非常接近,但是当我尝试打印总和时,它不起作用。这是我的代码。

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