【问题标题】:C++ For loops and multidimensional arraysC++ For 循环和多维数组
【发布时间】:2024-04-26 04:55:01
【问题描述】:

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

现在,我并不想无礼,但我的老师不是很好,我们基本上在一个问题上花费了 2 个小时,而她没有做太多解释。除此之外,我是从 C++ Primer and Programming: Principles and Practice Using 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;
    }

【问题讨论】:

  • 逗号不会像你想象的那样工作
  • 用特定值填充数组是一种更简单的方法。 int sumRows[row_num] = { 0 };
  • 你的教师循环更容易阅读和更正,你的不是(即使错误没有出现,但你的中断条件是错误的)
  • @tobi303 在这里一针见血。在可读性和良好/优雅的方法之间经常需要权衡。

标签: c++ for-loop multidimensional-array


【解决方案1】:

1) 您注释掉的循环总和 所有 值,而不仅仅是沿对角线的值。

2) 它与您的方法不同,它会遍历矩阵中的每个值,但只有在检测到它位于适当的单元格之一时才会添加到总数中。您的解决方案只迭代适当的单元格,而不必评估任何ifs,因此它会更有效。但是,您需要将循环条件更改为i &lt; row_num &amp;&amp; j &gt;= 0。在此处使用逗号将丢弃其中一项检查的结果。

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

【讨论】:

  • 请注意,将此处的循环与他所要求的内容结合起来很好
  • 是的,你是对的,我错过了老师循环中的if。我将编辑我的答案。
  • 我不认为注释掉的循环计算出正确的值
  • 感谢您的回复,他们帮助了!我现在有一个“我是个白痴的时刻”,因为我意识到我使用了 ,而不是 &&。
【解决方案2】:

1) 这个

    for(i=0; i<row_num; i++)
        for (j = 2; j >= 0; j--)
        {
            antidiagonal += A[i][j];
        }

错了,因为对于i=0,您遍历所有j 值,即

  i=0 , j=2,1,0  then i=1

2) 您的教师循环更易于阅读和纠正。您的中断条件错误:

 for (i = 0, j = 2; i < row_num, j >= 0; i++, j--)
                               ^------------------------ WRONG

阅读逗号运算符以了解此处实际发生的情况。您只是没有意识到问题所在,因为这两个条件是偶然等效的。

3) 你不必使用它,它只是一种新奇的初始化数组的方法。

【讨论】:

  • 感谢您指出我的错误!使用 , 而不是 && 真的很愚蠢。