简体   繁体   English

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

[英]Sum of the elements of diagonal of matrix c++

I want to find the sum of the diagonals of matrix that defined by user with this code, it works for main diagonal but not for secondary diagonal and i don't understand what the problem is.我想找到用户使用此代码定义的矩阵的对角线之和,它适用于主对角线,但不适用于辅助对角线,我不明白问题是什么。 Any help would be appreciated.任何帮助,将不胜感激。

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

For starters Variable Length Arrays is not a standard C++ feature.对于初学者,可变长度 Arrays 不是标准 C++ 功能。 Use instead the class template std::vector .改用 class 模板std::vector

For example例如

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

Or at least allocate arrays dynamically as for example或者至少动态分配 arrays 例如

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

In this case you should free all allocated memory before exiting the program as在这种情况下,您应该在退出程序之前释放所有已分配的 memory

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

Secondly neither sum1 nor sum2 were initialized.其次 sum1 和 sum2 都没有被初始化。

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

To calculate the sum of elements of the secondary diagonal you can write要计算次对角线元素的总和,您可以编写

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

I included some comments in the code.我在代码中包含了一些注释。

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

Run it and you'll find that it works as expected.运行它,你会发现它按预期工作。 You were very close to the solution, the main problems were the lack of initialisations of your sum variables and that you need to use new or better yet something like std::vector for dynamically-sized arrays.您非常接近解决方案,主要问题是缺少 sum 变量的初始化,并且您需要使用new或更好的东西,例如std::vector用于动态大小的 arrays。 Also, please don't use post-increment in loops ( i++ ).另外,请不要在循环 ( i++ ) 中使用后增量。 What you mean is pre-increment ( ++i ).你的意思是预增量( ++i )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM