C++ 函数不在主要部分执行

标签 c++ function

我在处理不执行功能的程序时遇到困难,我似乎找不到问题所在。

我尝试编写的这段代码应该要求用户输入二维数组的大小,然后搜索每一行并计算行的平均值。

在计算结果出来之前,它执行得很好。

例子:

Enter the size of the array: 2
2
Enter the element of the 1 row and 1 column: 10
Enter the element of the 1 row and 2 column: 20
Enter the element of the 2 row and 1 column: 50
Enter the element of the 2 row and 2 column: 20
Program ended with exit code: 0

及程序代码:

    #include <iostream>
    #include <iomanip>
    using namespace std;

    void calculate(int n, int m, int matrix[10][10], double sum, double avg[10], int k); //to calculate the average of each row

    void input(int n, int m, int matrix[10][10]); //to input the requirements

    void results(double avg[10],int n); //to output the results

    int main() {
         int matrix[10][10]; //the array
         int n,m; //rows and columns entered by the user
         double avg[10]; //average of the array rows, which will be calculated later
         int k; //number of positive elements
         double sum; //to calculate sum
         input(n, m, matrix);
         calculate(n, m, matrix, sum, avg, k);
         results(avg, n);
         return 0;
      }

    void input(int n, int m, int matrix[10][10]) {
         cout<<"Enter the size of the array: ";
         cin>>n>>m; //the real elements of the array
         for (int i=0; i<n; i++) {
             for (int j=0; j<m; j++) {
                 cout<<"Enter the element of the "<<i+1<<" row and "<<j+1<<" column: ";  //entering each element of the array
                 cin>>matrix[i][j];
             }
         }
     }

void calculate(int n, int m, int matrix[10][10], double sum, double avg[10], int k) {
    for (int i=0; i<n; i++) {
    k=0;
    sum=0;
    avg=0;
    for (int j=0; j<m; j++) {
        if (matrix[i][j]>0) {
            sum+=static_cast<double>(matrix[i][j]);
            k++;
        }
    }
    if (k>0) {
        avg[i]=sum/static_cast<double>(k);
    }
}
}

void results(double avg[10], int n) {
    for (int i=0; i<n; i++) { //
        cout<<"Average of "<<i<<" row is equal to: "<<avg[i]<<"\n";
    }
}

最佳答案

您的代码存在多个问题:

  1. 您向用户询问数组的大小,但您的数组定义为常量:

    int matrix[10][10];
    

    如果用户输入 11 会发生什么?这将导致未定义的行为。考虑使用 std::vector如果您想拥有真正的动态数组。

  2. 当您读取 nm 值时 void input(int n, int m, int matrix[10][10]) 过程中,您正在对这些变量的拷贝 进行更改(即它们按值传递),因此更改仅对函数内部可见。当您离开该功能的范围时,您对它们所做的所有更改都将丢失。您需要通过引用传递这些参数,即:

    void input(int& n, int& m, int matrix[10][10]);
    

    这样一来,编译器就不会进行复制,而您将从 main 中更改相同的变量。

    考虑到这一点,您需要以类似的方式更改您的calculate 过程:

    void calculate(int n, int m, int matrix[10][10], double& sum, double avg[10], int& k);
    

    不需要通过引用传递变量nm,因为在这种情况下,它们是输入参数,不需要更改.

关于C++ 函数不在主要部分执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34817575/

相关文章:

Python:如何根据返回值调整函数的一部分的执行

c++ - Boost.Asio 仅作为 header

c++ - C++ minsweeper 原型(prototype)上的段错误

c++ - 为什么float**不能用float(*)[5]初始化?

c++ - 如何在 Mac OS 上设置 opencv 的路径?

r - 在 R 中使用 fitdist 时出错 - 必须定义 dllogis 函数

c++ - 删除未在 std::for_each 中调用,但在基于范围的 for 循环中调用?

arrays - 创建声明预定义文本数组的函数

function - 函数的返回值

c - IAR 中 C 函数的别名