How to write a C Program Array Example: Average in C Programming Language ?
Solution For C Program :
Learn To Write A Program. Learn Programming Online. Find Programming Solutions.
#include <limits.h> int avg(int a, int b) { int max = INT_MAX; int min = INT_MIN; int hmax = max / 2; int hmin = min / 2; if ((a == max) && (b == max)) { return max; } else if ((a == min) && (b == min)) { return min; } else if (a == b) { return a; } // 2 conditions of failure left to cover // 1 A == max , B > hmax // 2 B == max , A > hmax // Fuck if(a > hmax ) { if (b > hmax) { return hmax + (((a - hmax) + (b - hmax)) / 2); } else if (b > 0) { return (a / 2) + (b / 2); } else { return ((a + b) / 2); } } else if (a < hmin) { if (b < hmin) { return hmin + (((a - hmin) + (b - hmin)) / 2); } else if (b < 0) { return (a / 2) + (b / 2); } else { return ((a + b) / 2); } } else { return ((a + b) / 2); } }
#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; }