題目:撰寫一個程式統計某班 3 門課程的成績,設課程是 VB 、Access 和PhotoShop ,有學生人數 6 人,依次輸入學生成績,最后統計每門課程全班的總成績和平均成績,
解題思路:定義三個函式來計算VB 、Access 和PhotoShop 的總成績和平均成績,定義主函式來呼叫這三個函式,實作功能(注意:因為函式不能同時回傳多個值,所以需要通過函式來實作),
程式代碼:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
float sum_score1 = 0; // 函式不能有多個回傳值,所以通過全域變數來計算總成績
float sum_score2 = 0;
float sum_score3 = 0;
int main()
{
float chengji1(float score[6][3], int n); // 宣告函式chengji1
float chengji2(float score[6][3], int n);
float chengji3(float score[6][3], int n);
float score[6][3];
int i, j;
float avg1,avg2, avg3;
printf("請輸入6名學生的成績:\n");
for(i = 0; i < 6; i++)
{
for (j = 0; j < 3; j++)
scanf("%f", &score[i][j]);
}
avg1 = chengji1(score,6); // 呼叫函式 chengji1
avg2 = chengji2(score, 6);
avg3 = chengji3(score, 6);
printf("VB的總成績為:%.2f 平均成績為:%.2f\n", sum_score1, avg1);
printf("Access的總成績為:%.2f 平均成績為:%.2f\n", sum_score2, avg2);
printf("PhotoShop的總成績為:%.2f 平均成績為:%.2f\n", sum_score3, avg3);
return 0;
}
float chengji1(float score[6][3], int n)
{
int i, j;
float avg = 0;
for (i = 0; i < 6; i++)
sum_score1 += score[i][0];
avg = sum_score1 / n;
return avg;
}
float chengji2(float score[6][3], int n)
{
int i, j;
float avg1 = 0;
for (i = 0; i < 6; i++)
sum_score2 += score[i][1];
avg1 = sum_score2 / n;
return avg1;
}
float chengji3(float score[6][3], int n)
{
int i, j;
float avg2;
for (i = 0; i < 6; i++)
sum_score3 += score[i][2];
avg2 = sum_score3 / n;
return avg2;
}
運行之后如下圖:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237546.html
標籤:其他
