C++ For 循环和多维数组

标签 c++ for-loop multidimensional-array

所以我的 C++ 类(class)有一个作业。基本上我们必须创建一个 3x3 多维数组,计算行总和、列总和、对角线值总和和反对角值总和,我通常只输入 1 2 3 4 5 6 7 8 9 作为起始值点。

现在,我并不是想无礼,但我的老师不是很好,我们基本上在一个问题上花 2 个小时,而她没有做太多解释。除此之外,我从 C++ 入门和编程:使用 C++ 的原理和实践开始,所以我相信我可以自己学到很多东西。

无论如何,我的问题可能很愚蠢,但如果有人想提供帮助,请看这里:

  1. 我的/**/对反对角线值循环的评论给了我一个错误的总和。我假设它与 for 循环的性质有关,或者我只是输入错误,但我还没有弄清楚。

2.老师计算反对角值的方法如下:

    for (i = 0; i < row_num; ++i)
    for (j = 0; j < col_num; ++j)
        if (i + j == row_num - 1)
            anti-diagonal += A[i][j];

它与我的方法有何不同?我相信我的更简单,效果更好。

3.行内:

int sumRows[row_num] = { 0 };

为什么必须使用 {}?我们的老师没有费心去解释。我尝试不使用 {},但出现错误。

这是我的版本的完整代码:

#include "../../std_lib_facilities.h"
#include <iostream>
using namespace std;

#define row_num 3 //no. of rows
#define col_num 3 //no. of columns

    int main()
    {
        int i = 0;
        int j = 0;
        int diagonal = 0;
        int antidiagonal = 0;

        int sumRows[row_num] = { 0 };
        int sumCol[col_num] = { 0 };

        int A[row_num][col_num];

        //Input to matrix
        for(i=0; i<row_num; i++)
            for (j = 0; j < col_num; j++)
            {
                cout << "A[" << i << "]" << "[" << j << "]: ";
                cin >> A[i][j];

                sumRows[i] += A[i][j];
                sumCol[j] += A[i][j];
            }

        cout << endl;

        //Print out the matrix
        for (i = 0; i < row_num; i++)
        {
            for (j = 0; j < col_num; j++)
                cout << A[i][j] << '\t';
                cout << endl;
        }

        //prints sum of rows
        for (i = 0; i < row_num; i++)
            cout << "Sum of row " << i + 1 << " "<< sumRows[i] << endl;

        //prints sum of columns
        for (j = 0; j < row_num; j++)
            cout << "Sum of column " << j + 1 << " " << sumCol[j] << endl;

        //Sum of diagonal values
        for (i = 0; i < row_num; i++)
            diagonal += A[i][i];

        //Sum of antidiagonal values
        for (i = 0, j = 2; i < row_num, j >= 0; i++, j--)
            antidiagonal += A[i][j];

        /*for(i=0; i<row_num; i++)
            for (j = 2; j >= 0; j--)
            {
                antidiagonal += A[i][j];
            }
        */
        cout << "\nSum of diagonal values: " << diagonal << endl;
        cout << "Sum of antdiagonal values: " << antidiagonal << endl;

        return 0;
    }

最佳答案

1) 你注释掉的循环求和所有值,而不仅仅是反对角线上的值。

2) 它与您的方法不同,因为它会遍历矩阵中的每个值,但是如果它检测到它位于适当的单元格之一,它只会添加到总数中。您的解决方案仅遍历适当的单元格,而不必评估任何 if s,这样效率会更高。但是,您需要将循环条件更改为 i < row_num && j >= 0 .此处使用逗号将丢弃其中一项检查的结果。

3) int sumRows[row_num] = { 0 };初始化整个 sumRows带 0 的数组。

关于C++ For 循环和多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246858/

相关文章:

c++ - 无法在 D3D9 中使用 SetTransform

c++ - Protobuf重复字段反序列化

java - for 循环类分配出错

ios - 动画 UILabel 值增量

c++ - 使用元编程的私有(private)成员存在性测试,GCC vs clang,哪个是对的?

c++ - 在 C++ 程序中间包含头文件

javascript - 奇数之和直到达到Javascript中的限制

javascript - 通过索引删除数组对象

php - 元素数量可变的数组,向上计数

multidimensional-array - ndarray是否存在assert_approx_eq?