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

如何将矩阵所有对角线元素设为0?C++作业求助

Solution: Set Matrix Diagonals to Zero with Function Parameter

Hey there! Let's get your code working properly to set matrix diagonals to zero and pass the matrix into a function. First, let's fix a critical bug and then add the required functionality step by step.

Step 1: Fix Array Indexing

C++ uses 0-based indexing for arrays, so your loops using i <= 3 and j <= 3 are accessing out-of-bounds memory (since matrix[3][3] only has valid indices 0, 1, 2). We'll adjust all loops to start at 0 and run while the index is less than 3, while keeping user-friendly 1-based prompts for input.

Step 2: Create a Function to Modify Diagonals

To pass a 2D array to a function, you need to specify the size of the second dimension (number of columns). Here's a function that sets the main diagonal (where row index = column index) to 0:

void setDiagonalsToZero(int mat[][3], int size) {
    for (int i = 0; i < size; i++) {
        // Set main diagonal elements to 0
        mat[i][i] = 0;
        // Uncomment below if you also want to set the anti-diagonal to 0
        // mat[i][size - 1 - i] = 0;
    }
}

Step 3: Update Main Function to Use the Function

We'll call the function after matrix input, then print the modified matrix. Here's the full updated code:

#include <iostream>
using namespace std;

// Function declaration
void setDiagonalsToZero(int mat[][3], int size);

int main() {
    // Problem 1: Input and print original matrix, plus transpose
    int matrix[3][3];
    
    // Input matrix (fixed indexing, user-friendly prompts)
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << "Row " << i+1 << " column " << j+1 << ": ";
            cin >> matrix[i][j];
        }
    }
    
    // Print original matrix
    cout << "\nOriginal matrix:\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
    
    // Print transposed matrix
    cout << "\nTransposed matrix:\n";
    for (int j = 0; j < 3; j++) {
        for (int i = 0; i < 3; i++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    // Problem 2: Set diagonals to zero and print
    cout << "\nMatrix with diagonals set to 0:\n";
    // Call the function to modify the matrix directly
    setDiagonalsToZero(matrix, 3);
    // Print the modified matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

// Function definition
void setDiagonalsToZero(int mat[][3], int size) {
    for (int i = 0; i < size; i++) {
        mat[i][i] = 0;
        // Uncomment for anti-diagonal:
        // mat[i][size - 1 - i] = 0;
    }
}

Key Notes:

  • The function modifies the original matrix directly because arrays are passed by reference in C++ (no need to return a new matrix).
  • If you need to handle matrices of variable sizes later, you can use vector<vector<int>> instead of fixed-size arrays for more flexibility.

内容的提问来源于stack exchange,提问作者J.A. Aranda

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:46:07