You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++自定义函数求二维方阵左对角线和报错排查

二维矩阵左对角线元素求和编译错误修复

编译错误信息

.\sum_matrix.cpp: In function 'int main()':
.\sum_matrix.cpp:44:33: error: invalid conversion from 'int' to 'int (*)[10]' [-fpermissive]
  cout<<sum_diagonal(arr[MAX][MAX]);
                     ~~~~~~~~~~~~^
.\sum_matrix.cpp:7:5: note:   initializing argument 1 of 'int sum_diagonal(int (*)[10])'
 int sum_diagonal(int arr_[][MAX]){
     ^~~~~~~~~~~~

原代码

#include <iostream>
using namespace std;

const int MAX = 10;

//adding the elements of the left diagonal
int sum_diagonal(int arr_[][MAX]){
    int sum = 0;
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            if(i == j){ 
                sum += arr_[i][j];
            }   
        }
    }
    return sum;
}

int main()
{   
    int arr[MAX][MAX];

    //entering array elements
    cout<<"Enter the values of rows and columns of array:\n";
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            cout<<"arr["<<i<<"]"["<<j<<"] = ";
            cin>>arr[i][j];
        }
    }

    //displaying array elements
    cout<<"The array is:\n";
    for(int i = 0; i<MAX; i++){
        for (int j = 0; i < MAX; j++)
        {
            cout<<"\t"<<arr[i][j];
        }
    }   
    cout<<endl;

    //sum of the left diagonal
    cout<<"Sum of diagonal elements: ";
    cout<<sum_diagonal(arr[MAX][MAX]);
    return 0;
}

错误分析与修复

1. 核心编译错误:函数传参错误

sum_diagonal函数要求传入二维数组(类型为int (*)[10]),但调用时传的arr[MAX][MAX]是数组中单个int元素,且属于越界访问(数组索引范围是0到9,MAX=10),类型完全不匹配。

修复:直接传入数组名arr,数组名会自动退化为指向第一行的指针,符合函数参数要求:

cout<<sum_diagonal(arr);

2. 其他潜在问题修复

  • 输入提示语法错误:cout<<"arr["<<i<<"]"["<<j<<"] = "; 中"]"["语法错误,改为"][":
    cout<<"arr["<<i<<"]["<<j<<"] = ";
    
  • 数组打印循环条件错误:内层循环条件写成i < MAX,会导致死循环,改为j < MAX:
    for (int j = 0; j < MAX; j++)
    
  • 对角线求和效率优化:无需两层循环遍历所有元素,利用i == j的特性,一层循环即可完成:
    int sum_diagonal(int arr_[][MAX]){
        int sum = 0;
        for(int i = 0; i<MAX; i++){
            sum += arr_[i][i];
        }
        return sum;
    }
    

修复后的完整代码

#include <iostream>
using namespace std;

const int MAX = 10;

// 计算左对角线元素总和
int sum_diagonal(int arr_[][MAX]){
    int sum = 0;
    // 优化:直接遍历对角线元素,无需两层循环
    for(int i = 0; i<MAX; i++){
        sum += arr_[i][i];
    }
    return sum;
}

int main()
{   
    int arr[MAX][MAX];

    // 输入数组元素
    cout<<"Enter the values of the "<<MAX<<"x"<<MAX<<" array:\n";
    for(int i = 0; i<MAX; i++){
        for(int j = 0; j<MAX; j++){
            cout<<"arr["<<i<<"]["<<j<<"] = ";
            cin>>arr[i][j];
        }
    }

    // 打印数组元素
    cout<<"The array is:\n";
    for(int i = 0; i<MAX; i++){
        for (int j = 0; j < MAX; j++)
        {
            cout<<"\t"<<arr[i][j];
        }
        // 每行结束换行
        cout<<endl;
    }   

    // 计算并输出对角线总和
    cout<<"Sum of diagonal elements: ";
    cout<<sum_diagonal(arr)<<endl;
    return 0;
}

内容的提问来源于stack exchange,提问作者Shreyash Kashyap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:40:28