I am a complete beginner in C language.
I want to make a program that three student enter their student ID, name, score then this program print their student ID, name, score, total score and highest score.
I want to also use a subprogram like below:
#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;
}
this program's error code is like below: enter image description here
CodePudding user response:
best_score
and all_scores
should take an array and do the looping themselves, they shouldn't be called in a loop.
The array should be struct student
, not float
, since you don't have an array of just float
.
The functions should return float
, not 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;
}
CodePudding user response:
There are many issues:
- score is a
double
, therefore you need to usedouble
everytime a score is involved. Right now you're usingint
(like inint best_score(float a, float b);
),float
(like infloat max=0;
) anddouble
(like indouble grade;
). - the declaration
int best_score(float a, float b);
doesn't match the implementationint best_score(float list[i], int i)
- The implementation
best_score
cannot work. You need to scan allSIZE
scores. - It's unclear what
all_score
is supposed to do (maybe return the sum of all scores?)
You probably want some something like this for 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;
}
call it like this:
double max = best_score(list);
Now you should be able to write all_score
yourself.