首页 > 解决方案 > 奇数有问题

问题描述

此代码未显示正确的输出。

我需要帮助,奇怪。我不确定向量是否正常。我需要做的是“计算并显示一个向量,该向量具有作为产品元素的第一行和第一列的相应元素。”

是运行时带有代码的图像。

The input and the output are:
Let A be a quadratic matrix with a maximum of 20 rows and columns, with integer elements.
Enter the number of rows and columns of the matrix: M =4
A[1,1]=2
A[1,2]=3
A[1,3]=4
A[1,4]=5
A[2,1]=6
A[2,2]=2
A[2,3]=3
A[2,4]=4
A[3,1]=5
A[3,2]=6
A[3,3]=2
A[3,4]=3
A[4,1]=4
A[4,2]=5
A[4,3]=6
A[4,4]=2
2       3       4       5
6       2       3       4
5       6       2       3
4       5       6       2

Enter the number of the column to be summed = 3

The sum of the items on the column is32779 // here should be 24 . 
The number of odd elements in A, located below the main diagonal, is 1 //here should be 2 ,not 1
4       3       2       5
3       2       6       4
2       6       5       3
6       5       4       2
16 9 4 30
#include <iostream>

using namespace std;
int main() {
    cout << "Let A be a quadratic matrix A with a maximum of 20 rows and columns, with integer elements." << endl;
    cout << "Enter the number of rows and columns of the matrix A: M = ";
    int rows_and_columns;
    cin >> rows_and_columns;

    // Reading the elements of the matrix A
    int matrix_A[20][20];
    for (int i = 1; i <= rows_and_columns; i++) {
        for (int j = 1; j <= rows_and_columns; j++) {
            cout << "A[" << i << "," << j << "]=";
            cin >> matrix_A[i][j];
        }
    }
    // Matrix display
    for (int i = 1; i <= rows_and_columns; i++) {
        for (int j = 1; j <= rows_and_columns; j++) {
            cout << matrix_A[i][j] << '\t';
        }
        cout << endl;
    }
    // the sum of the elements on column sum_column, sum_column being which column to make the sum
    cout << endl << "Enter the number of the column to be summed = ";
    int sum_column;
    cin >> sum_column;
    int sum = 0;
    for (int i = 0; i <= rows_and_columns; i++) {
        sum += matrix_A[i][sum_column];
    }
    cout << endl << "The sum of the items on the column is" << sum << endl;

    // below the main diagonal, count how many odd numbers there are
    constexpr int n = 0;
    int number_of_odd_numbers = 0;
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= i - 1; j++) {
            if (i + j < rows_and_columns + 1) {
                if (matrix_A[i][j] % 2 != 0) {
                    number_of_odd_numbers++;
                }
            }
        }
    }
    cout << "The number of odd elements in the matrix A, located below the main diagonal, is " << number_of_odd_numbers << endl;

    // changing the elements between them
    int x = rows_and_columns - 1;
    for (int j = 1; j <= rows_and_columns; j++) {
        std::swap(matrix_A[j][1], matrix_A[j][x]);
    }
    for (int i = 1; i <= rows_and_columns; i++) {
        for (int j = 1; j <= rows_and_columns; j++) {
            cout << matrix_A[i][j] << '\t';
        }
        cout << endl;
    }
    // calculation and display of vector B
    int vector_B[20];
    for (int k = 1; k <= rows_and_columns; k++) {
        vector_B[k] = matrix_A[1][k] * matrix_A[k][1];
        cout << vector_B[k] << " ";
    }
    cout << endl;
    return 0;
}

标签: c++

解决方案


无论玩家输入如何,您都定义了一个调整大小的 20x20 矩阵。
我会推荐一个动态大小的数组使用std::vector或类似的东西。
额外的大小通常没问题,但在这个程序中,它导致程序在第一个 for 循环时不崩溃,而它应该崩溃。

C++ 中的数组的范围是 [0, size)。您当前正在使用范围 [1, size]。您的所有 for 循环都应从索引 0 开始,并使用<运算符而不是<=运算符。
如果您的数组大小正确,您将超出数组范围并导致崩溃。

在评论下//count how many odd numbers there are,假设n0i总是会比n你设置i的更大,2所以循环永远不会运行。

除此之外,我不知道该程序要做什么,但请记住,数组的范围是 [0, size),您应该能够弄清楚。


推荐阅读