In: Computer Science
This has to be a C program -
Here is a simplified set of rules that show how a dive is judged at a competition like the Olympics:
As an example, suppose a diver performs a dive with degree of difficulty 3.2 and the judges' marks were 7, 8, 5, 4, 9.5, 6.5, 9.5 and 8. The two lowest marks are 4 and 5 and the two highest marks are 9.5 and 9.5. The judges will add up the remaining four marks: 7+8+6.5+8=29.5 and multiply by 3.2 to get the diver's score: 94.4. An equivalent approach would be to add up all 8 of the scores and subtract the two lowest and two highest from the total before multiplying by the difficulty. Either way, please note that the marks may not be reported to the program in sorted order.
You must hand in a file called diving.c. It must be a complete C program, including a main function, which will prompt the user for information about a dive and compute the score for the dive. Specifically, the program must prompt the user for the degree of difficulty followed by 8 judge's marks and then print the score for the dive.
Have a look at the sample run shown below and duplicate the look of it as closely as possible.
Your program must contain at least three functions (a main function and at least two others). At least two functions other than main must have at least one parameter and must use their parameters in a non-trivial way. (In other words, just writing a message saying "my parameter is 3" does not count!)
At least two functions other than main must return a result and the function that calls each of them must use the result in a non-trivial way. (In other words, having function f call function g and then having f write a message saying "function g returned 13" does not count!)
Consider using #define statements to create meaningful symbols for following numbers:
Using symbols will make your code easier to read, and therefore easier to debug. It will also make your code easier to change. For example, if you ever wanted to modify the program for use at a diving competition with 10 judges instead of 8, you would just have to change one #define. You wouldn't have to look through your whole program to find 8s and read the code carefully to distinguish between 8s used to mean number of judges and 8s used to mean the number of dives per competitor.
Error Hnadling:
Your program must check for the following kinds of errors:
If either of these errors occur, your program must print an informative error message and use a default value instead (0 for a score and 1 for a degree of difficulty). "Informative", means an error that tells the user what was wrong – for example "Error: scores must be between 0 and 10" instead of just "Error". You may assume that user will enter only numeric values in answer to your program's prompts.
#include <stdio.h>
float sort(float marks[] );
float result(float , float);
#define JUDGE_NUMBER 8
int main()
{
float marks[JUDGE_NUMBER],a,degreeofdifficulty,sum=0.00;
int i ,j ;
//enter degree of difficulty
printf("Enter degree of difficulty \n");
scanf("%f",°reeofdifficulty);
if(degreeofdifficulty <1 || degreeofdifficulty>5){
printf("Enter degree of difficulty between 1 and 5 \n");
scanf("%f",°reeofdifficulty);
}
// enter marks for the dive
printf("Enter marks for dive \n");
for(i=0;i<JUDGE_NUMBER;i++){
scanf("%f",&marks[i]);
if(marks[i]< 0 || marks[i]> 10 ){
printf("Enter degree of score between 0 and 10 \n");
scanf("%f",&marks[i]);
}
}
float p = sort(marks );
float t = result(degreeofdifficulty , p);
printf("Score of a dive is %f\n",t);
return 0;
}
// function to sort dive marks and return the sum
float sort(float marks[JUDGE_NUMBER] ){
float sum = 0;
float a;
for (int i = 0; i < JUDGE_NUMBER; ++i)
{
for (int j = i + 1; j < JUDGE_NUMBER; ++j)
{
if (marks[i] > marks[j])
{
a = marks[i];
marks[i] = marks[j];
marks[j] = a;
}
}
}
for(int j =2 ; j < JUDGE_NUMBER-2 ; j++){
sum+= marks[j];
}
return sum;
}
// function to calculate final result
float result(float fector , float sum){
float total = fector * sum;
return total;
}
Output :-
Enter degree of difficulty - 2.5
Enter marks for dive- [1,2,3,4,5,6,7,8]
Score for a dive is 45.000000