#include <stdio.h>
int main(){
int max=0;
int min=0;
int stu=0;
int i=0;
int sum=0;
float avg=0;
int num[i];
printf ("Please enter the number of students: ");
scanf("%d",&stu);
for(i=0;i<stu;i++){
a:
printf("Please enter the %dth student's score: ",(i+1));"
scanf("%d",num[i]);
if(num[i]>100||num[i]<0){
printf ("Enter your score to fit the range");
goto a;
}
sum+=num[i];
if(i==0){
max=num[0];
min=num[0];
}
else{
if(num[i]>max){
max=num[i];
}
else{
if(num[i]<min){
min=num[i];
}
}
}
}
avg=sum/stu;
printf("Max: %d", max);
printf ("minimum value: %d", min);
printf ("average: %f", avg);
}
I think it's a problem from the num[i] part.
I'd like to adjust the size of the arrangement according to the number of students entering the scanf below, what should I do?
That's all we need to do.
c
I was writing to answer the previous question, but I'm writing a similar new question here.
To get re-entered without increasing the i
, you can superimpose the repeat statement with the while statement within the for statement, or reduce the i with i--;
once and restart the loop through continue.
It is not recommended to use the goto keyword because it ignores the flow of logic and forces it to move.
a:
printf("Please enter the %dth student's score: ",(i+1));"
scanf("%d",num[i]);
if(num[i]>100||num[i]<0){
printf ("Enter your score to fit the range");
goto a;
}
If it's possible, please.
printf("Enter the score of the %dth student: ",(i+1));
scanf("%d",num[i]);
if(num[i]>100||num[i]<0){
printf ("Enter your score to fit the range");
i--;
continue;
}
For int num[];
, the maximum allowed value of the number of students should be included in the size of the array.
This is because the array must be sized in c language.
And when you enter the number of students in stu
, you should turn the loop so that you don't receive more than the maximum limit so that you can re-enter it. See exception handling when entering strings in c language integer input scanf.
For example,
int num[500];
stu = 0;
while (stu <= 0 || stu > 500) { // exit when the stu value is 1 to 500
printf ("Please enter the number of students: ");
scanf("%d",&stu);
while(getchar()!='\n'); // for releasing input buffer (see link above)
}
If you really need an array of flexible sizes rather than a fixed size, you have no choice but to use pointers and dynamic assignments. (C++ is a simple library, but you'll have to make it yourself on c++.)
© 2024 OneMinuteCode. All rights reserved.