0

我无法让 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;
}
4

2 回答 2

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;
}
于 2020-04-16T23:29:45.777 回答
0

您的问题出在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。

于 2020-04-17T02:43:40.380 回答