我是 C 語言的完全初學者。
我想做一個程式,三個學生輸入他們的學生 ID、姓名、分數,然后這個程式列印他們的學生 ID、姓名、分數、總分和最高分。
我還想使用如下子程式:
#include<stdio.h>
#define SIZE 3
int best_score(float a, float b);
struct student {
int number;
char name[20];
double grade;
};
int main(){
struct student list[SIZE];
int i;
for(i = 0; i < SIZE; i ){
printf("enter your student ID : ");
scanf("%d", &list[i].number);
printf("enter your name : ");
scanf("%s", list[i].name);
printf("enter your score : ");
scanf("%lf", &list[i].grade);
printf("\n");
}
printf("\n");
for(i = 0; i< SIZE; i ){
printf("ID: %d, name: %s, score: %f\n", list[i].number, list[i].name, list[i].grade);
}
for(i = 0; i< SIZE; i ){
float max = best_score(list[i], i);
float temp = all_score(list[i], i);
}
return 0;
}
int best_score(float list[i], int i){
float max=0;
if (max < list[i]) {
max = list[i];
}
return max;
}
int all_score(float list[i], int i){
float temp=0;
temp = list[i];
return temp;
}
該程式的錯誤代碼如下: 在此處輸入影像描述
uj5u.com熱心網友回復:
best_score并且all_scores應該使用一個陣列并自己進行回圈,它們不應該在回圈中被呼叫。
該陣列應該是struct student,不是float,因為您沒有一個僅包含float.
函式應該回傳float,而不是int。
#include<stdio.h>
#define SIZE 3
struct student {
int number;
char name[20];
double grade;
};
float best_score(struct student a[], size_t n);
float all_score(struct student a[], size_t n);
int main(){
struct student list[SIZE];
int i;
for(i = 0; i < SIZE; i ){
printf("enter your student ID : ");
scanf("%d", &list[i].number);
printf("enter your name : ");
scanf("%s", list[i].name);
printf("enter your score : ");
scanf("%lf", &list[i].grade);
printf("\n");
}
printf("\n");
for(i = 0; i< SIZE; i ){
printf("ID: %d, name: %s, score: %f\n", list[i].number, list[i].name, list[i].grade);
}
float max = best_score(list, SIZE);
float temp = all_score(list, SIZE);
printf("Best score: %f, All scores: %f\n", max, temp);
return 0;
}
float best_score(struct student list[], size_t n){
float max=0;
for (size_t i = 0; i < n; i ) {
if (max < list[i].grade) {
max = list[i].grade;
}
}
return max;
}
float all_score(struct student list[], size_t n){
float temp=0;
for (size_t i = 0; i < n; i ) {
temp = list[i].grade;
}
return temp;
}
uj5u.com熱心網友回復:
有很多問題:
- score 是 a
double,因此double每次涉及到score 時都需要使用。現在您正在使用int(如 inint best_score(float a, float b);)、float(如 infloat max=0;)和double(如 indouble grade;)。 - 宣告
int best_score(float a, float b);與實作不匹配int best_score(float list[i], int i) - 實施
best_score無法作業。您需要掃描所有SIZE分數。 - 目前還不清楚
all_score應該做什么(也許回傳所有分數的總和?)
你可能想要一些這樣的東西 best_score
double best_score(struct student list[])
{
double max = 0;
for (int i = 0; i < SIZE; i ) {
if (max < list[i].grade) {
max = list[i].grade;
}
}
return max;
}
像這樣稱呼它:
double max = best_score(list);
現在你應該可以all_score自己寫了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/357598.html
標籤:C
