How to Write a C Program reads in the number of judges and the score from each judge. Then it calculates the average score without regard to the lowest and highest judge score. Finally it prints the results (the highest, the lowest and the final average score).
Solution:
#include <stdio.h> void printInfo(void) { 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"); } int readJudges(void) { int number; while (1) { printf("Number of judges (min 3 and max 10 judges)? "); scanf("%d", &number); if (number >= 3 && number <= 10) { return number; } } return 0; } /*Reads judge scores from user */ void readScore(int number, double judgeScore[]) { printf("\n"); double points; for(int i = 0 ; i < number ; i++) { printf("Score from judge %d? ", i+1); scanf("%lf", &points); judgeScore[i] = points; } } void printScore(int number, double judgeScore[]) { printf("\n"); printf("Loaded scores:\n"); for(int i = 0 ; i < number ; i++) { printf("Judge %d: %.1f\n", i+1, judgeScore[i]); } } /* Returns highest value of 'score' as a pointer */ void highestValue(double* max, int number, double judgeScore[]) { max = judgeScore; for(int i = 0 ; i < number ; i++) { if (judgeScore[i] > *max) { max = judgeScore + i; } } } /* Returns lowest value of 'score' as a pointer */ void lowestValue(double* min, int number, double judgeScore[]) { min = judgeScore; for(int i = 0 ; i < number ; i++) { if (judgeScore[i] < *min) { min = judgeScore + i; } } } void average(int number, double judgeScore[], double* avg, double* min, double* max) { double sum; for(int i = 0 ; i < number ; i++) { sum += judgeScore[i]; } *avg = (sum - *max - *min)/(number - 2); /* Beräknar "medel" utan att ta hänsyn till minsta och största */; } void printResult(double max, double low, double avg) { printf("Final result:\n"); printf("Highest judge score: %.1f\n", max); printf("Lowest judge score: %.1f\n", low); printf("Final average score: %.1f\n", avg); } int main(void) { printInfo(); printf("\n"); int number; number = readJudges(); double score[number]; /* Arrayens storlek beror på readJudges */ readScore(number, score); printScore(number, score); /* Efter readScore exekverats innehåller score alla poäng */ printf("\n"); double avg, low, max; lowestValue(&low, number, score); highestValue(&max, number, score); average(number, score, &avg, &low, &max); printResult(max, low, avg); return 0; }