noob here...
Been trying to write c programming for a group assignment. Got everything working smoothly except when it calculates scores above 100 ( for example 101). My question ask for categorizing scores and giving average, and finally, if it's outside the ranges, should give output informing the score is invalid and they need to re-enter. Here's my code:
#include<stdio.h>
main()
{
int n=0,i=0;
int scores[100],average=0, total=0;
int outstanding=0,satisfactory=0,unsatisfactory=0;
printf("Number of scores you want to enter:\n");
scanf("%d",&n);//how many times the score needed to be entered
printf("Please enter the scores:\n");
for(i=0;i<n;i++)//store user input in array
{
scanf("%d", &scores[i]);
if ((scores[i]<0) && (scores[i]>100))
{
printf("\nScores invalid. Please re-enter:\n");
i--;
}
}
for(i=0;i<n;i++)// to categorize
{
if((scores[i]>=0) && (scores[i]<=59))
{
unsatisfactory++;
}
else if((scores[i]>=60) && (scores[i]<=89))
{
satisfactory++;
}
else if((scores[i]>=90) && (scores[i]<=100))
{
outstanding++;
}
}
for (i=0;i<n;i++)
{
total = total + scores[i];
average = total / n;
}
//print out results
printf("\n The number of unsatisfactory scores is : %d\n ", unsatisfactory);
printf("\n The number of satisfactory scores is : %d\n", satisfactory);
printf("\n The number of outstanding scores is : %d\n", outstanding);
printf("\n The average of the score is : %d\n ", average);
return 0;
}
I suspected the problem would be at:
if ((scores[i]<0) && (scores[i]>100))
{
printf("\nScores invalid. Please re-enter:\n");
i--;
}
But not sure how to correct it. Even if I put return main();, it still don't give out the intended output ('Scores invalid. Please Re-enter.`) and return, instead continued with categorizing and averaging.
Help?With cherry on top?
Aucun commentaire:
Enregistrer un commentaire