简体   繁体   English

二维数组及其元素的总和

[英]c++ - Output a 2D array and the sum of its elements

In this program I'm trying to add the elements of a 2d array, print the entire array, and print its sum using functions. 在此程序中,我试图添加2d数组的元素,打印整个数组,并使用函数打印其总和。 I know I am very close, but when I try to print the sum it isn't working. 我知道我已经很接近了,但是当我尝试打印总和时却无法正常工作。 Here is my code. 这是我的代码。

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;

    }

}

When I run this in the compiler I end up getting this: 当我在编译器中运行它时,我最终得到了这个:

1 2 3 4 10
5 6 7 8 36
9 10 11 12 78

It looks like what's happening is it finds the sum of the each row and adds it to the sum of the prior row. 看起来正在发生的事情是找到每行的总和并将其加到上一行的总和中。 Any ideas? 有任何想法吗?

Your function is very confusing. 您的功能非常混乱。

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;

    }

}

For example the original array has three rows but the corresponding parameter of the function you named like col. 例如,原始数组有三行,但是您命名的函数的相应参数像col。 And vice versa the number of columns you named like rows in the function declaration. 反之亦然,您在函数声明中像行一样命名的列数。

Also inside the outer loop you output partial sums of the array. 同样在外循环内,您输出数组的部分和。 For example 例如

1 2 3 4 10
5 6 7 8 36
9 10 11 12 78

10 is the sum of 1, 2, 3, 4. 36 is the sum of 1, 2, 3, 4 and 5, 6, 7, 8 and so on. 10是1、2、3、4的总和。36是1、2、3、4和5、6、7、8的总和。

I think you want to output the sum after displyaing the array. 我认为您希望在显示数组后输出总和。 Take into account that parameter total is redundant. 考虑到参数总数是多余的。 You could simply declare a local variable with name total within the function. 您可以在函数内简单地声明一个名称为total的局部变量。

I would separate the function into two functions. 我将功能分为两个功能。 One is designed to output the array and other is designed to calculate the sum of all elements of the array. 一种用于输出数组,另一种用于计算数组所有元素的总和。

In this case the program would look like 在这种情况下,程序看起来像

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

The program output is 程序输出为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM