通过引用传递数组 C++
Passing array by reference C++
我无法通过 getAverage 函数获取正确的平均值。我究竟做错了什么?我不能使用指针。这是原始问题:
一个会提示成绩并计算平均分的程序。成绩将存储在一个名为 GradesInput 的数组中,该数组在 main 中定义。数组中可存储的最大成绩数为 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;
}
函数 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;
}
您的问题出在 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。
我无法通过 getAverage 函数获取正确的平均值。我究竟做错了什么?我不能使用指针。这是原始问题:
一个会提示成绩并计算平均分的程序。成绩将存储在一个名为 GradesInput 的数组中,该数组在 main 中定义。数组中可存储的最大成绩数为 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;
}
函数 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;
}
您的问题出在 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。