Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 行和的结果是正确的,但列和的结果对于3 x 4数组是错误的_C++ - Fatal编程技术网

C++ 行和的结果是正确的,但列和的结果对于3 x 4数组是错误的

C++ 行和的结果是正确的,但列和的结果对于3 x 4数组是错误的,c++,C++,我试图制作一个程序,其中行和列的总和被单独计算,行结果很好,但列给出了奇怪的结果。正确的结果应该是这样的 (示例矩阵) 9124 2 2 8 0 3 3 3 3 第0行的和是16 第1行的和是12 第2行的和是12 第0列的和是14 第1列的和是6 第2列的和是13 第3列的和是7 但我的结果是 第0行的和为16 第1行的和是12 第2行的和是12 第0列的和为565359518 第1列的总和为32772 第2列的和是13 第3列的和是7 #包括 使用名称空间std; int main()

我试图制作一个程序,其中行和列的总和被单独计算,行结果很好,但列给出了奇怪的结果。正确的结果应该是这样的 (示例矩阵)

9124
2 2 8 0
3 3 3 3
第0行的和是16
第1行的和是12
第2行的和是12
第0列的和是14
第1列的和是6
第2列的和是13
第3列的和是7
但我的结果是

第0行的和为16
第1行的和是12
第2行的和是12
第0列的和为565359518
第1列的总和为32772
第2列的和是13
第3列的和是7
#包括
使用名称空间std;
int main()
{
整数值[3][4];
整数和=0;
int row,col;
int i,j;
对于(int i=0;i<3;i++){
对于(int j=0;j<4;j++){
cout值[i][j];
}
}

cout我会做一些我不应该做的事情,但是你已经为此付出了足够的努力,我想用一个有效的答案告诉你问题在哪里:

#include <iostream>

using namespace std;

// Use Constants to declare the size of your array to make it easier to iterate in loops.
// Also allows the compiler to know at compile-time the array sizes of functions.
const int ColSize = 4;
const int RowSize = 3;


// Forward declare functions here so that main() knows what they are.
void col_sum(int value[RowSize][ColSize]);
void row_sum(int value[RowSize][ColSize]);

int main()
{
    // Replace those nasty magic number 3 and 4s here with constants
    int value[RowSize][ColSize];
    // Notice I have removed all the unused vars here (i,j,sum, etc.)

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {
        for (int j = 0; j < ColSize; j++) {
            cout << "Enter a value : " << endl;
            cin >> value[i][j];
        }
    }

    cout << "Value of the array: " << endl;

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {
        for (int j = 0; j < ColSize; j++) {
            cout << value[i][j] << " ";
        }

        cout << endl;
    }

    // Actually call the functions...
    row_sum(value);
    col_sum(value);

    return 0;
}

// Make actual functions to be called using our constants.
void row_sum(int value[RowSize][ColSize])
{
    // Notice I have removed all the repeated vars here (i,j,sum, etc.)
    // They are all declared within the for loops

    // Replace magic numbers
    for (int i = 0; i < RowSize; i++) {

        int sum = 0;  // Moved the sum 0 here as we only need it once.
        for (int j = 0; j < ColSize; j++) {
            sum += value[i][j];
        }

        cout
            << "Sum of the row "
            << i << " is " << sum
            << endl;

    }
}

void col_sum(int value[RowSize][ColSize])
{
    // Notice I have removed all the repeated vars here (i,j,sum, etc.)
    // They are all declared within the for loops

    // Replace magic numbers
    for (int i = 0; i < ColSize; i++) {
        int sum = 0;  // Moved the sum 0 here as we only need it once.
    
        for (int j = 0; j < RowSize; j++) {
            sum = sum + value[j][i];
        }

        cout
            << "Sum of the column "
            << i << " is " << sum
            << endl;
    }
}
#包括
使用名称空间std;
//使用常量来声明数组的大小,以便更容易在循环中进行迭代。
//还允许编译器在编译时知道函数的数组大小。
常数int ColSize=4;
const int RowSize=3;
//在此转发声明函数,以便main()知道它们是什么。
void col_sum(int值[RowSize][ColSize]);
无效行和(int值[RowSize][ColSize]);
int main()
{
//用常量替换那些讨厌的魔法数字3和4s
int值[RowSize][ColSize];
//注意,我已经删除了这里所有未使用的变量(I、j、sum等)
//替换幻数
对于(int i=0;icout col sum正在计数
i
(或
j
)到4。这是不允许的。其中一个必须是到3,这取决于你从矩阵中读取的顺序。另外,如前所述,你在函数中嵌入了函数?这不是PASCAL,这是合法的。你传递到值和中的参数应该为你的2维数组声明为常量大小。我真的不同意e上面的代码编译。哦,函数名末尾的分号。哇。这会编译,但实际上没有函数。哇,以前从未见过。我猜这些函数名会变成前向声明…@MichaelOrganger我用了这个例子:@Blazer234是的,但你似乎从那个例子中遗漏了那些函数在
main
之外定义,然后从中调用(带参数)。