繁体   English   中英

通过引用传递数组 C++

[英]Passing array by reference C++

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

将提示成绩并计算平均值的程序。 成绩将存储在 main 中定义的名为 GradesInput 的数组中。 可以存储在数组中的最大成绩数为 100。保存用户输入的实际成绩数的变量应在 main 中定义,并应称为 GradeCount。 该程序将具有除 main 之外的两个功能。 第一个 function 应该从 main 调用,并且应该一直提示用户输入成绩,直到输入哨兵,捕获这些成绩并将它们存储在 GradesInput 中。 这个 function 也应该更新 main 中的变量 GradeCount,GradeCount 应该是通过引用传递的。 第二个 function 应该从 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;
}

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

while (arr[i]!=-1)

是无效的。 此外,变量i在循环中没有被改变。

function 可以通过以下方式定义/

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 ); 
}

但最好通过以下方式声明和定义 function

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 );

function 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;
}

您的问题出在store() function 中。 您甚至在初始化之前将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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM