C++ 基于用户大小的数组函数,用于在值大于90时打印出内容
我正在设置一个基于用户大小的数组,以输入一些分数,并打印出最高和最低值以及平均值。我还想使用boolen函数打印出是否有任何a等级(分数高于90),但我不太确定如何编码它。我在下面尝试使用getAScores函数,但它不起作用C++ 基于用户大小的数组函数,用于在值大于90时打印出内容,c++,function,boolean,C++,Function,Boolean,我正在设置一个基于用户大小的数组,以输入一些分数,并打印出最高和最低值以及平均值。我还想使用boolen函数打印出是否有任何a等级(分数高于90),但我不太确定如何编码它。我在下面尝试使用getAScores函数,但它不起作用 #include <iostream> using namespace std; #include <iomanip> #include <cstdlib> int compare(const void* pa, const
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
int compare(const void* pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1; // negative if a<b
if (a > b) return 1; // positive if a>b
return 0; // 0 for tie
} // compare
double getAverage(int* score, int n)
{
int sum = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += score[i];
double average = double(sum) / n;
return average;
} // getAverage
//Boolen Function to see if there are A grades present
bool getAGrades(int* score)
{
int i = 0;
if (score[i] >= 90){
return true;
cout << "there is at least one A"<<endl;
}else {
return false;
cout<<" No A Grades "<<endl;
}
}
int main()
{
int size;
cout << "How many scores? ";
cin >> size;
cin.ignore(1000, 10);
int* score = new int[size];
int i; // loop counter
for (i = 0; i < size; i++)
{
cout<< "Enter a number: ";
cin >> score[i];
cin.ignore(1000, 10);
} // for
qsort(score, size, sizeof(int), compare);
for (int i = 0; i < size; i++) {
cout << score[i] << ' ';
}
cout <<endl;
cout << "Lowest score = " << score[0] << endl;
cout << "Highest score = " << score[size-1] << endl;
cout << fixed << setprecision(1);
cout << "Average = " << getAverage(score, size) << endl;
//this is where i want it to print if there are a grades or not
getAGrades(score);
return 0;
} // main
#包括
使用名称空间std;
#包括
#包括
整数比较(常数无效*pa,常数无效*pb)
{
常数int&a=*静态(pa);
常数int&b=*静态(pb);
if(ab则返回正
返回0;//0表示平局
}//比较
双倍平均值(整数*分数,整数n)
{
整数和=0;
int i=0;
对于(i=0;i=90){
返回true;
cout您需要使用循环查看所有索引(就像您在getAverage
中所做的那样)。这要求getAGrades
知道数组的大小
另外,请注意,将cout
放在返回
之后是没有意义的-它永远不会执行。请从这个函数中选择您想要的:bool getAGrades(int*score)。目前它只查看一个索引(0),这没有意义。此外,请尝试不要使用cin.ignore()。我假设这是一个基本的作业,您不需要执行复杂的输入筛选。如果这是作业,请添加作业标签。