矩阵c ++对角线元素之和

问题描述 投票:1回答:2

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

#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;
}

c++ visual-studio loops for-loop vector
2个回答
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)。


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];
}
© www.soinside.com 2019 - 2024. All rights reserved.