- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题是
设计成绩处理程序以使用函数和数组。让用户输入等级数(将最大值设置为 50)。将成绩存储在数组中。为每个计算创建一个单独的函数(总共 3 个函数):1) 平均成绩,2) 最高成绩,3) 高于平均成绩的成绩数。显示所有结果。
我想我已经掌握了主要部分,但是我在如何编写函数时遇到了问题,函数出于某种原因正在输出一个函数,而且我在编写 max 函数或如何启动它时遇到了问题。
#include <iostream>
using namespace std;
double average(double x[], int n);
double maximum(double x[], int n);
int nAboveAvg(double x[], int n);
int main()
{
double grades[50];
int ngrades;
cout<<"How many grades? (max = 50)";
cin>>ngrades;
//create for loop to get grades from user
for(int i = 0; i<ngrades; i++)
{
cout<<"Enter grade ";
cin>> grades[i];
}
//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
int nAbove = nAboveAvg(grades, ngrades);
//display results
cout<<"Average = "<<avg<<endl;
cout<<"# above average = "<<nAbove<<endl;
}
double average(double x[], int npts) //define the functon to recieve the array
{
double sum = 0;
for(int k = 0; k<npts; k++)
{
sum = sum +x[k];
}
return sum / npts;
}
double maximum(double x[], int npts)
{
double max = 0;
for(int i = 0; i<npts; i++)
{
if(max == npts)
{
return max;
}
if(max < npts)
{
return npts;
}
}
}
int nAboveAvg(double x[], int npts)
{
int nAboveAvg = 0;
for(int i = 0; i<npts;i++)
{
if(x[i] > npts)
{
nAboveAvg++;
}
}
return nAboveAvg;
}
最佳答案
//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
int nAbove = nAboveAvg(grades, ngrades);
请注意,您定义了名为 avg
和 nAbove
的变量。
//display results
cout<<"Average = "<<average<<endl;
cout<<"# above average = "<<nAboveAvg<<endl;
但是当您尝试打印结果时,您会使用 average
和 nAboveAvg
(函数)。
这里的正确版本是:
cout << "Average = " << avg << endl;
cout << "# above average = " << nAbove << endl;
当我尝试 compile this ,编译器会发出许多警告。例如
main.cpp: In function 'int main()':
main.cpp:29:24: warning: the address of 'double average(double*, int)' will always evaluate as 'true' [-Waddress]
cout<<"Average = "<<average<<endl;
或
main.cpp:24:11: warning: unused variable 'avg' [-Wunused-variable]
double avg = average(grades, ngrades);
最好不要忽略这些警告。
if(x[i] > npts)
{
nAboveAvg++;
}
您将位置 i 的值与输入值的数量 进行比较。但是,您应该将位置i 的值与所有值的平均值 进行比较。因此
int nAboveAvg(double x[], int npts)
{
int count = 0;
double avg = average(x, npts);
for (int i(0); i < npts; ++i) {
if (x[i] > avg) {
++count;
}
}
return count;
}
您现在可能会注意到,在我们的程序中,我们最终计算了两次平均值。我们可以通过使我们的函数更通用来解决这个问题——我们不计算高于平均值的值的数量,而是计算作为参数传递的高于任意目标的值的数量。
int count_above(double x[], int npts, double target)
{
int count = 0;
for (int i(0); i < npts; ++i) {
if (x[i] > target) {
++count;
}
}
return count;
}
现在我们可以写了
//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
int nAbove = count_above(grades, ngrades, avg);
让我们考虑算法,从最简单的情况开始——数组中只有一个值。在这种情况下,第一个值也是最大值。
double max = x[0];
知道了这一点,让我们考虑一下当输入数组中有 2 个值时如何找到最大值。我们已经知道第二个值之前所有值的最大值。因此
if (x[1] > max) {
max = x[1];
}
下一步,输入包含 3 个值的数组。同样,我们已经知道第三个值之前所有值的最大值。因此
if (x[2] > max) {
max = x[2];
}
我们可以看到一个模式在这里重复,我们可以将其包装在一个循环中。
double maximum(double x[], int npts)
{
if (npts <= 0) {
exit(-1); // Error...
}
double max = x[0];
for (int i(1); i < npts; ++i) {
if (x[i] > max) {
max = x[i];
}
}
return max;
}
您没有对传递给函数的输入进行任何验证,但有一些明显的情况需要考虑。
npts
为负时会发生什么情况?处理这些情况的一种非常简单的方法是返回一些特殊值作为结果。然后每次调用该函数时都必须检查该值的结果。在许多情况下,可能很难为此选择合适的值。
初学者可以接受的另一种可能性是简单地向控制台打印一些错误消息,然后退出程序。例如
if (npts <= 0) {
cerr << "Too few values in input array." << endl;
exit(-1); // Error...
}
正确的 C++ 方法是抛出异常,例如 std::invalid_argument .例如
#include <stdexcept>
// ....
if (npts <= 0) {
throw std::invalid_argument("Too few values in input array.);
}
关于c++ - 函数和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36107251/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!