矩阵C ++对角线元素的总和

时间:2019-11-13 14:43:27

标签: c++ visual-studio loops for-loop vector

我想用此代码查找用户定义的矩阵对角线的总和,它适用于主对角线,但不适用于次对角线,我不明白问题是什么。任何帮助将不胜感激。

#include <iostream>
using namespace std;

int main() {
    int r, c, j,i,k,sum1,sum2;
    cout << "Enter the size of matrix: ";
    cin >> j;
    int matrix[j][j];

    for (r = 0; r < j; r++) {
        for (c = 0; c < j; c++) {
            cout << "matrix" << "[" << r << "]" << "[" << c << "] = ";
            cin >> matrix[r][c];
        }
    }
    cout << "\n";

    for ( i = 0; i < j; i++) {
        for ( k = 0; k < j; k++) {
            cout << " "<<matrix[i][k] << " ";
        }
        cout << "\n";
    }

cout<<"\n";

cout<<"Main diagonal is: ";
for( i=0;i<j;i++){
    cout<<matrix[i][i];
}
cout<<"\n";


for( i=0;i<j;i++){
    sum1=sum1+matrix[i][i];}
    cout<<"Sum of the elements of main diagonal is: "<<sum1;
    cout<<"\n";
    cout<<"\n";

cout<<"Secondary diagonal is: ";
for(i=0,k=j-1;i<j;i++,k--){
    cout<<matrix[i][k];
}


for(i=0,k=j-1;i<j;i++,k--){
    sum2=sum2+matrix[i][k];
    }

    cout<<"Sum of the elements of secondary diagonal is: "<<sum2;
    return 0;
}

2 个答案:

答案 0 :(得分:6)

对于初学者,可变长度数组不是标准的C ++功能。改用类模板std::vector

例如

std::vector<std::vector<int>> matrix( j, std::vector<int>( j ) );

或者至少例如动态分配数组

int **matrix = new int *[j];
for ( i = 0; i < j; i++ ) matrix[i] = new int[j];

在这种情况下,您应该以以下方式退出程序,然后释放所有已分配的内存

for ( i = 0; i < j; i++ ) delete [] matrix[i];
delete [] matrix;

第二,sum1和sum2都没有初始化。

int r, c, j,i,k,sum1,sum2;

要计算第二对角线元素的总和,您可以编写

sum2 = 0;
for ( i = 0; i < j; i++ ){
    sum2 += matrix[i][j - i - 1];
}

答案 1 :(得分:0)

我在代码中包含了一些注释。

#include <iostream>

/* try to avoid "using namespace ...", as it pollutes the global namespace */
using std::cout;
using std::cin;

int main() {
    /* I moved r and c into the block they belong to */
    int j,i,k,sum1,sum2;
    //cout << "Enter the size of matrix: ";
    //cin >> j;
    /* let's use a fixed-size array for now, otherwise you'll have to
     * dynamically allocate your array using "new" or better use std::vector */
    j = 4;
    int matrix[j][j];

    for (int r {0}; r < j; ++r) {
        for (int c {0}; c < j; ++c) {
            matrix[r][c] = r-c; /* this way, diagonals show very clearly */
            //cout << "matrix" << "[" << r << "]" << "[" << c << "] = ";
            //cin >> matrix[r][c];
        }
    }
    cout << "\n";

    for ( i = 0; i < j; ++i) {
        for ( k = 0; k < j; ++k) {
            cout << " "<< matrix[i][k] << " ";
        }
        cout << "\n";
    }

cout<<"\n";

cout<<"Main diagonal is: ";
for( i=0;i<j;++i){
    cout << matrix[i][i] << " ";
}
cout<<"\n";

/* initialise your sums */
sum1 = 0;
sum2 = 0;

for( i=0;i<j;i++){
    sum1=sum1+matrix[i][i];}

cout<<"Sum of the elements of main diagonal is: "<<sum1;
cout<<"\n";
cout<<"\n";

cout<<"Secondary diagonal is: ";
for(i=0,k=j-1;i<j; ++i, --k){
    cout << matrix[i][k] << " ";
}
cout<<"\n";
cout<<"\n";


for(i=0,k=j-1;i<j;i++,k--){
    sum2 = sum2+matrix[i][k];
    }

    cout<<"Sum of the elements of secondary diagonal is: "<<sum2;
    return 0;
}

运行它,您会发现它可以正常运行。您非常接近解决方案,主要问题是缺少和变量的初始化,您需要对动态大小的数组使用new或更好的std::vector之类的东西。 另外,请不要在循环(i++)中使用后递增。您的意思是预增量(++i)。