21.C++實現計算八個學生的平均成績

Lily__Hu發表於2020-12-16
#include <iostream>
#include <string>
using namespace std;
#define N 8

float grades[N];//存放成績的陣列
int main() {
	int i;
	float total, average;
	//提示輸入成績
	for (i = 0; i < N; i++) {
		cout << "Enter grades #" << (i + 1) << " :";
		cin >> grades[i];
	}
	total = 0;
	for (i = 0; i < N; i++) {
		total += grades[i];
	}
	average = total / N;
	cout << "\nAverage grades: " << average << endl;
	return 0;
}

相關文章