Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Programmeringsteknik med C och Matlab
- * Fall 18
- * Assignment 2
- * File: ou2.c
- * Description: The 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).
- * Author:
- * CS username:
- * Date: 1810XX
- * Input:
- * Output:
- * Limitations:
- */
- #include <stdio.h>
- /* Declaration of functions. */
- void printInformation(void);
- int enterNumberOfJudges(void);
- double enterScore(int nrOfJudges, double array[]);
- void printScore(int nrOfJudges, double array[]);
- double calculateResult(int nrOfJudges, double array[], double *max, double *min, double *average);
- double printResult(double *max, double *min, double *average);
- /* Function: main
- * Description:
- * Input:
- * Output:
- */
- int main(void) {
- int nrOfJudges = 0;
- double arr[nrOfJudges];
- double max;
- double min;
- double average;
- printInformation();
- nrOfJudges = enterNumberOfJudges();
- printf("\n");
- enterScore(nrOfJudges, arr);
- printf("\n");
- printScore(nrOfJudges, arr);
- calculateResult(nrOfJudges, arr, &max, &min, &average);
- printf("\n");
- printResult(&max, &min, &average);
- return 0;
- }
- /* Function: printInformation.
- * Description: Prints information at start of the program.
- * Input: Non.
- * Output: Information on the screen.
- */
- void printInformation(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\n");
- }
- /* Function: enterNumberOfJudges.
- * Description: Lets the user enter the amount of judges.
- * Input: Non.
- * Output: Number of judges.
- */
- int enterNumberOfJudges(void) {
- int nrOfJudges;
- do {
- printf("Number of judges (min 3 and max 10 judges)? ");
- scanf("%d", &nrOfJudges);
- } while (nrOfJudges < 3 || nrOfJudges > 10);
- return nrOfJudges;
- }
- /* Function: enterScore.
- * Description: Lets the user enter score for each judge.
- * Input: The array and it's size.
- * Output: Elements for the array.
- */
- double enterScore(int nrOfJudges, double array[]) {
- for (int i = 0; i < nrOfJudges; i++) {
- printf("Score from judge %d? ", i+1);
- scanf("%lf", &array[i]);
- }
- return 0;
- }
- /* Function: printScore.
- * Description: Prints scores entered previously for each judge.
- * Input: The array and it's size.
- * Output: Scores entered for each judge.
- */
- /* Fungerar ej för värde 6 eller större */
- void printScore(int nrOfJudges, double array[]) {
- printf("Loaded scores:\n");
- for (int i = 0; i < nrOfJudges; i++) {
- printf("Judge %d: %.1f\n", i+1, array[i]);
- }
- }
- /* Function: calculateResult.
- * Description: Calculates highest score, lowest score and average score excluding highest and lowest score.
- * Input: The array and it's size.
- * Output: Higest value, lowest value and average value.
- */
- double calculateResult(int nrOfJudges, double array[], double *max, double *min, double *average) {
- const int n = nrOfJudges;
- *max = array[0];
- *min = array[0];
- double sum = 0;
- for (int i = 0; i < n; i++) {
- if (array[i] > *max) {
- *max = array[i];
- }
- if (array[i] < *min) {
- *min = array[i];
- }
- }
- for (int i = 0; i < n; i++) {
- if (array[i] > *min && array[i] < *max) {
- sum += array[i];
- }
- }
- *average = sum / (n - 2);
- return *max;
- return *min;
- return *average;
- }
- /* Function: printResult.
- * Description: Prints results and ends program.
- * Input: Max value, min value and average value.
- * Output: Max value, min value and average value.
- */
- double printResult(double *max, double *min, double *average) {
- printf("Final result:\n");
- printf("Highest judge score: %.1f\n", *max);
- printf("Lowest judge score: %.1f\n", *min);
- printf("Final average score: %.1f\n", *average);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement