C++ 二维数组:计算行的和和列的乘积

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41740502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:27:55  来源:igfitidea点击:

Two-dimensional array: calculate sum of rows and product of columns

c++arraysalgorithmmultidimensional-array

提问by Ava Barbilla

I am currently working on a task which says the following:

我目前正在执行一项任务,内容如下:

Input a two-dimensional array A (m,n) [m < 10, n < 20]. In the n + 1 column calculate the sum of the rows, and in the m + 1 row the product of the columns. Print out the resulting matrix.

输入一个二维数组 A (m,n) [m < 10, n < 20]。在 n + 1 列中计算行的总和,在 m + 1 行中计算列的乘积。打印出结果矩阵。

According to my understanding of this task, at the end of each column must be the sum of according rows (so on the right hand side), and the product of the column (at the end/bottom?).

根据我对这项任务的理解,每列的末尾必须是相应行的总和(因此在右侧)和列的乘积(在末尾/底部?)。

This task is so confusing I do not know where to start. I found some code that covers the idea but does not include the product and it does not display these values as the task asks me to:

这个任务太混乱了,我不知道从哪里开始。我发现了一些涵盖该想法但不包含产品的代码,并且它不显示这些值,因为任务要求我:

#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int a[3][3];
    int i, j, s = 0, sum = 0;

    cout << "Enter 9 elements of 3*3 Matrix \n";
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            cin >> a[i][j];

    cout << "Matrix Entered By you is \n";
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            cout << a[i][j] << " ";
        cout << endl;
    }

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            s = s + a[i][j];
        cout << "sum of" << i + 1 << " Row is" << s;
        s = 0;
        cout << endl;
    }
    cout << endl;
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            s = s + a[j][i];
        cout << "sum of" << i + 1 << " Column is" << s;
        s = 0;
        cout << endl;
    }

    cout << endl;

    for (i = 0; i < 3; i++)
        sum = sum + a[i][i];
    cout << "Sum of Diagnols Elements is \n" << sum;

    getch();
}

回答by dasblinkenlight

Start with the declaration: make sure that your program works with m×n matrix, not simply a 3×3 matrix. Since m and n have limits of 10 and 20, and because you must add an extra row and a column to the result, the declaration should be

从声明开始:确保您的程序使用 m×n 矩阵,而不是简单的 3×3 矩阵。由于 m 和 n 的限制为 10 和 20,并且您必须向结果中添加额外的行和列,因此声明应为

int a[11][21];

You also need to declare mand n, have end-user enter them, and validate them to be within their acceptable ranges:

您还需要声明mand n,让最终用户输入它们,并验证它们在可接受的范围内:

int m, n;
cin >> m >> n;
... // Do the validation

Now you can rewrite your loops in terms of mand n, rather than using 3throughout your code.

现在您可以根据mand重写循环n,而不是3在整个代码中使用。

With these declarations in place, you can total the numbers in place, i.e. for each row ryou would write

有了这些声明,您就可以合计到位的数字,即对于r您要写的每一行

for (int i = 0 ; i != n ; i++) {
    a[r][n+1] += a[r][i];
}

Similarly, you would compute the product (don't forget to start it with the initial value of 1, not 0).

类似地,您将计算乘积(不要忘记以 的初始值开始它1,而不是0)。

At the end you would print an (m+1)×(n+1) matrix to complete the task.

最后,您将打印一个 (m+1)×(n+1) 矩阵来完成任务。

回答by Vlad from Moscow

We beginners should help each other.

我们初学者应该互相帮助。

Here you are

这个给你

#include <iostream>
#include <iomanip>

int main()
{
    const size_t M = 10;
    const size_t N = 20;
    int a[M][N] = {};

    std::cout << "Enter number of rows: (less than " << M << "): ";

    size_t m;

    std::cin >> m;

    if (!(m < M) || (m == 0)) m = M - 1;

    std::cout << "Enter number of columns: (less than " << N << "): ";

    size_t n;

    std::cin >> n;

    if (!(n < N) || (n == 0)) n = N - 1;

    std::cout << std::endl;

    for (size_t i = 0; i < m; i++)
    {
        std::cout << "Enter " << n
            << " numbers for the row " << i << ": ";
        for (size_t j = 0; j < n; j++) std::cin >> a[i][j];
    }

    for (size_t j = 0; j < n; j++) a[m][j] = 1;

    for (size_t i = 0; i < m; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            a[i][n] += a[i][j];
            a[m][j] *= a[i][j];
        }
    }

    std::cout << std::endl;

    for (size_t i = 0; i < m + 1; i++)
    {
        for (size_t j = 0; j < n + 1; j++)
        {
            std::cout << std::setw(3) << a[i][j] << ' ';
        }
        std::cout << '\n';
    }

    std::cout << std::endl;
}

The program output might look like

程序输出可能看起来像

Enter number of rows: (less than 10): 3
Enter number of columns: (less than 20): 3

Enter 3 numbers for the row 0: 1 2 3
Enter 3 numbers for the row 1: 4 5 6
Enter 3 numbers for the row 2: 7 8 9

  1   2   3   6
  4   5   6  15
  7   8   9  24
 28  80 162   0

So you have to declare an array with 10 rows and 20 columns. The user should enter the dimensions of the array that are correspondingly less than 10 and 20. One row and one column are reserved for sums and products.

所以你必须声明一个有 10 行和 20 列的数组。用户应输入相应小于 10 和 20 的数组维数。一行和一列保留用于总和和乘积。

It is desirable that the array would be initially initialized by zeroes.

数组最初由零初始化是可取的。

int a[M][N] = {};

In this case you need not to set the last column with zeroes as you have to do with last row initializing it with ones.

在这种情况下,您不需要将最后一列设置为零,因为您必须用最后一行对其进行初始化。

That is all.:)

就这些。:)

回答by Vlad from Moscow

Solution : use this kind of functions for your array after making it global.

解决方案:在将数组设为全局后,将此类函数用于数组。

    void Adder(int row, int colum)
{
    for (int i = 0; i < row - 1; i++)
    {
        int temp = 0;
        for (int j = 0; j < colum - 1; j++)
        {
            temp += a[i][j]; // added all other than last one
        }
        a[i][j] = temp; // assigned to last one in row
    }

}

void Mul(int row, int colum)
{
    for (int i = 0; i < colum- 1; i++)
    {
        int temp = 1;
        for (int j = 0; j < row - 1; j++)
        {
            temp *= a[j][i]; // multiplied all element other than last one
        }
        a[j][i] = temp; // assigned to last one in column
    }

}