#include<iostream> using namespace std; const int max = 10; int grade_in(double grade[],int limit); void show(const double grade[],int n); double grade_av(const double grade[],int n); int main() { double scores[max]; int size=grade_in(scores,max); show(scores,size); if (size > 0) { cout << "Average Grade = " << grade_av(scores, size) << endl; } return 0; } int grade_in(double grade[],int limit) { double temp; int i = 0; cout << "Enter your grade(enter a negetive to end)" << endl; for (i = 0; i < limit; i++) { cout << "Enter your grade #" << i + 1 << " : " << endl; cin >> temp; if (!cin) // { // cin.clear(); //这几句的作用是什么?删掉对程序运行结果没有影响 while (cin.get() != '\n')// continue; // cout << "Bad input;enter a number: "; break; } if (temp < 0) break; grade[i] = temp; } return i; } void show(const double grade[], int n) { for (int i = 0; i < n; i++) { cout << "Score #" << i + 1 << " : " << grade[i] << endl; } } double grade_av(const double grade[], int n) { double sum=0; int i = 0; for (i = 0; i < n; i++) { sum += grade[i]; } double av; av = sum / n; return av; }
onemoo