använda returnerade värdet istället för funktionen i c
Hej!
Håller på med en uppgift i c och kört fast.
Uppgiften går ut på att skriva ett program som läser in ett antal domarpoäng,
beräkna medelpoängen utan att ta hänsyn till den högsta och den lägsta poängen.
Felet ligger i att jag i funktionerna "printScores" och "find_Max_Min_Average" använder mig av judges=nrJudges()
vilket resulterar i att funktionen "nrJudges()" upprepas ett antal gånger.
Jag kan inte heller slå ihop funktionerna "printScores" och "find_Max_Min_Average" som jag så gärna skulle vilja göra
då uppgiften går ut på att varje det ska finnas minst en funktion för varje steg i programmet :/
min fråga är då; Hur ska jag använda mig utav det returnerade värdet "judges" i resterande funktioner utan att det ska bli upprepningar hela tiden?
All hjälp uppskattas!
#include <stdio.h>
/*prints program info*/
void programInfo(){
printf("Program information\n");
printf("The program reads in the number of judges and the score from each judge.\n");
printf("Then it calculates the average score without regard to the lowest and\n");
printf("highest judge score. Finally it prints the results (the highest, the\n");
printf("lowest and the final average score).\n");
printf("\n");
}
/*scans and returns the number of judges*/
int nrJudges(){
int judges;
do {
printf("Number of judges (min 3 and max 10 judges)? ");
scanf("%d", &judges);
}
while (judges < 3 || judges > 10);
printf("\n");
return judges;
}
/*prints the score from each judge*/
double printScores (double scores[]){
int judges;
judges=nrJudges();
for (int i= 0; i < judges; i++){
printf("Score from judge %d? ", i+1);
scanf("%lf", &scores[i]);
}
printf("\n");
printf("Loaded scores: \n");
for (int i= 0; i < judges; i++){
printf ("Judge %d: %.1lf\n", i+1, scores[i]);
}
return *scores;
}
/*Finds and prints the smallest and largest scores
and calculates the average score without regard to
the largest and the smallest score*/
void find_Max_Min_Average(double scores[]){
double max;
double min;
double average=0;
double sum=0;
int judges;
judges= nrJudges();
*scores=printScores(scores);
max = scores[0];
for (int i = 1; i < judges; ++i) {
if (scores[i]> max){
max=scores[i];
}
}
min = scores[0];
for (int i = 1; i < judges; ++i) {
if (scores[i] < min){
min=scores[i];
}
}
for (int i = 1; i < judges; ++i) {
sum+=scores[i];
average=(sum-max)/(i-1);
}
printf("\n");
printf("Final result: \n");
printf("Highest judge score: %.1lf\n", max);
printf("Lowest judge score: %.1lf\n", min);
printf("Final average score: %0.1lf\n", average);
}
int main(void){
double scores;
programInfo();
find_Max_Min_Average(&scores);
return 0;
}