基于用户大小的数组函数,如果值大于90,则打印出一些内容

时间:2011-12-15 07:23:34

标签: c++ function boolean

设置基于用户大小的数组以输入分数并打印出最高和最低值以及平均值。我还想使用boolen函数打印出是否有任何A等级(分数高于90)但是我不太确定如何编码它。我尝试使用getAScores函数,但是它不起作用。

#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

1 个答案:

答案 0 :(得分:0)

您需要使用循环来查看所有索引(就像您在getAverage中所做的那样)。这要求getAGrades知道数组的大小。

另外,请注意在cout之后放置return是没有意义的 - 它永远不会被执行。