輸入10個學生5門課的成績,分別用函式實作下列功能:
(1)計算每個學生的平均分;
(2)計算每門課的平均分;
(3)找出所有50個分數中最高的分數所對應的學生和課程。
uj5u.com熱心網友回復:
用個結構體陣列去做吧uj5u.com熱心網友回復:
寫了一個供參考
#include <iostream>
using namespace std;
const int N = 3;
typedef struct Stud
{
char name[20];
float score[5], average;
}STU;
//輸入成績
void InputSTU(STU *array, int n)
{
int i, j;
float temp = 0;
for (i=0; i<n; i++)
{
cout << "輸入第" << i+1;
cout << "個學生姓名 課程一 課程二 課程三 課程四 課程五的成績:" << endl;
cin >> array[i].name;
for (j=0; j<5; j++)
{
cin >> array[i].score[j];
temp += array[i].score[j];
}
array[i].average = temp / 5;
temp = 0;
}
}
//輸出
void PrintSTU(STU *array, int n)
{
int i, j;
cout << endl << "姓名\t課程一\t課程二\t課程三\t課程四\t課程五\t平均分\n" << endl;
for (i=0; i<n; i++)
{
cout << array[i].name << "\t";
for (j=0; j<5; j++)
{
cout << array[i].score[j] << "\t";
}
cout << array[i].average << endl;
}
cout << endl;
}
//每分課平均分
void ScoreAver(STU *array, int n)
{
float aver[5] = {0};
int i, j;
for (j=0; j<5; j++)
{
for (i=0; i<n; i++)
{
aver[j] += array[i].score[j];
}
}
cout << endl << "各門課平均分:" << endl;
cout << "課程一" << "\t" << "課程二" << "\t";
cout << "課程三" << "\t" << "課程四" << "\t";
cout << "課程五" << endl << endl;
for (i=0; i<5; i++)
{
cout << aver[i] / N << "\t";
}
cout << endl << endl;
}
//最高分
void MaxSTU(STU *array, int n)
{
int i, j, index_i, index_j;
float smax;
smax = array[0].score[0];
for (i=0; i<n; i++)
{
for (j=0; j<5; j++)
{
if (smax < array[i].score[j])
{
smax = array[i].score[j];
index_i = i;
index_j = j;
}
}
}
cout << endl << "最高分:" << array[index_i].score[index_j] << endl;
cout << endl << "學生姓名:" << array[index_i].name << endl;
cout << endl << "最高分課程是:課程" << index_j + 1 << endl;
}
int main()
{
STU sArr[N];
InputSTU(sArr, N);
PrintSTU(sArr, N);
ScoreAver(sArr, N);
MaxSTU(sArr, N);
return 0;
}
uj5u.com熱心網友回復:
看博客。。的講解,有很詳細的解答轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/35906.html
標籤:C++ 語言
上一篇:麻煩大佬推薦書籍
