【问题标题】:Passing array by reference C++通过引用传递数组 C++
【发布时间】:2020-07-30 08:50:01
【问题描述】:

我无法让 getAverage 函数获得正确的平均值。我究竟做错了什么?我不能使用指针。这是原始问题:

一个会提示成绩并计算平均值的程序。成绩将存储在 main 中定义的名为 GradesInput 的数组中。可以存储在数组中的最大成绩数为 100。保存用户输入的实际成绩数的变量应在 main 中定义,并应称为 GradeCount。该程序将具有除 main 之外的两个功能。第一个函数应该从 main 调用,并且应该一直提示用户输入成绩,直到输入哨兵,捕获这些成绩并将它们存储在 GradesInput 中。这个函数还应该更新 main 中的变量 GradeCount,GradeCount 应该是通过引用传递的。第二个函数应该从 main 调用,并且应该在 GradesInput 中找到平均成绩。平均值应该从 main 返回并打印出来。

//Lab7D This program will get the grades of students and average
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//The 1st function should be called from main and should keep prompting the user for grades till a sentinel is entered,
//capture those grades and store them in GradesInput.
//This function should also update the variable GradeCount in main, GradeCount should have been passed by reference.
void store(int arr[], int &GradeCount) //for taking input by user pass by reference
{
    int i = 0;

    while (arr[i] != -1)
    {
        cout << "Enter grade : ";
        cin >> arr[i];
        GradeCount++;
    }
}

//The 2nd function should be called from main and should find the average of the grades in GradesInput.
//he average should be returned to and printed out from main.
double getAverage(int arr[], int size)
{
    int i;
    double avg, sum = 0;

    for (i = 0; i < size; i++)
    {
        sum += arr[i];
    }
    avg = sum / size;

    return avg;
}

//The variable holding the actual number of grades the user entered should be defined in main and should be called GradeCount
int main()
{
    int GradeCount = 0;
    int grades[100];
    store(grades, GradeCount);

    cout << "Average : " << getAverage(grades, GradeCount) << endl;
}

【问题讨论】:

  • 这个循环 while (arr[i]!=-1) 调用未定义的行为,因为数组尚未初始化。

标签: c++ loops for-loop do-while function-definition


【解决方案1】:

函数store 具有未定义的行为,因为数组尚未初始化。所以循环中的条件

while (arr[i]!=-1)

无效。此外,变量i 不会在循环中更改。

函数可以这样定义/

void store( int arr[],int &GradeCount ) //for taking input by user pass by reference
{
    const int Sentinel = -1;

    GradeCount = 0;

    bool success = true;

    do
    {
        cout << "Enter grade : ";

        int value = Sentinel;

        if ( ( success = ( cin >> value && value != Sentinel ) ) )
        {
            arr[GradeCount++] = value;
        } 
    } while ( success ); 
}

但最好用下面的方式声明和定义函数

size_t store( int arr[], size_t n, int sentinel = -1 )
{
    size_t GradeCount = 0;

    if ( n != 0 )
    {    
        bool success = true;;

        do
        {
            cout << "Enter grade : ";

            int value = sentinel;

            if ( ( success = ( cin >> value && value != sentinel ) ) )
            {
                arr[GradeCount++] = value;
            }
        } while ( success && GradeCount < n ); 
    }

    return GradeCount;
}

然后这样称呼它

int grades[100];

size_t GradeCount = store( grades, 100 );

函数getAverage应按以下方式声明和定义

double getAverage( const int arr[], size_t n )
{
    double sum = 0.0;

    for ( size_t i = 0; i < n; i++ )
    {
        sum += arr[i];
    }

    return n == 0 ? 0.0 : sum / n;
}

【讨论】:

    【解决方案2】:

    您的问题在于store() 函数。在初始化之前,您正在将 a[i] 中保存的值与 -1 进行比较。另一种方法是执行以下操作:

    int store( int arr[] )
    {
        int i = 0;
        int grade = 0 ;
        while ( true )
        {
            std::cout<< "Enter grade: " ;
            std::cin >> grade ;
            if ( grade == -1 )
                break ;
    
            arr[ i ] = grade ;
            ++i ;
        }
        return i ;
    }
    

    store() 返回的值将是您以后可以用来平均成绩的 GradeCount。

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 2019-06-29
      • 1970-01-01
      • 2018-09-01
      • 2012-05-13
      • 2013-11-07
      • 2010-11-09
      相关资源
      最近更新 更多