Question

In: Computer Science

This has to be a C program - Here is a simplified set of rules that...

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:

  • Each different kind of dive is given a "degree of difficulty". The limits on degrees of difficulty keep changing as competitors perform increasingly difficult dives, but for this assignment the degree of difficulty of each dive must be between 1 and 5 inclusive.
  • The number of judges varies with the event, but for this assignment there will be 8 judges.
  • When a diver makes a dive, each judge gives a mark between 0 to 10 to describe how well the diver executed the dive.
  • To compute the score for a dive, you take the marks from the judges and discard the highest two scores and lowest two scores. Add up the remaining four marks and multiply the sum by the degree of difficulty.

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:

  • the minimum score that can be given for a dive (0)
  • the maximum score that can be given for a dive (10)
  • the minimum degree of difficulty (1)
  • the maximum degree of difficulty (5)
  • the number of dives per competitor (8)
  • the number of judges (8)

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:

  • A score that is less than 0 or greater than 10.
  • A degree of difficulty that is less than 1 or greater than 5.

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.

Solutions

Expert Solution

#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",&degreeofdifficulty);
         if(degreeofdifficulty <1 || degreeofdifficulty>5){
              printf("Enter degree of difficulty between 1 and 5 \n");
              scanf("%f",&degreeofdifficulty);
         }
    // 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


Related Solutions

1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
Write a C++ program that inputs two simplified poker hands (as defined in homework #5) and...
Write a C++ program that inputs two simplified poker hands (as defined in homework #5) and determines which (if either) of the hands is the winner, according to the following rules: • If the two hands have different types (3-of-kind, straight, pair, high-card), the hand with the better type (i.e., appears earlier in this list) wins. • If they have the same type, the higher significant card wins. • If the hands have the same type and significant card, there...
C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
Set up an integral for the following scenarios: Set up the integral in simplified form, do...
Set up an integral for the following scenarios: Set up the integral in simplified form, do not integrate a) Arc length of y = ln x , 2 ≤ x ≤ 4 . b) The surface area generated by rotating y = sin x with respect to the x-axis, 0 ≤ x ≤ π . c) The arc length of y = x 2 + 4 , 1≤ x ≤ 3 . d) The surface area generated by revolving y...
You are asked to write a simple C++ phonebook application program. Here are the requirements for...
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application. read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing...
Write a  program in c++ using a map to create the following output. Here is the list...
Write a  program in c++ using a map to create the following output. Here is the list of students: 100: Tom Lee 101: Joe Jones 102: Kim Adams 103: Bob Thomas 104: Linda Lee Enter a student an ID to get a student's name: ID:  103 students[103] - Bob Thomas # of students: 5 Delete a student (Y or N)?  Y Enter ID of student to be deleted:  103 Here is the list of students after the delete: 100: Tom Lee 101: Joe Jones...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
Write a C++ program that takes in a set of daily average temperatures (up to a...
Write a C++ program that takes in a set of daily average temperatures (up to a maximum of 30): 1.Ask the user for a temperature 2.If the user enters a -1 then stop asking for temperatures. 3. After the user is done entering temperatures: a. Print out the temperatures entered. b. print out the average, high and low temperatures. To get average, use: average = (sum of temps) divided by (count of temps) to get max or min, either keep...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in a user-specified file using the tree sort algorithm explained in the lectures while maintaining a balanced binary tree at all times. The following will test your program with an input file containing: Max Hank Jet Frisky Chata Richard Nan Sam Thomas Karen Gerri Ingrid Alan Dana When done print out the contents of the tree with inorder traversal in a tabular format similar to...
lineage program to Memheck-clean. The program is a simplified family tree manager. It maintains a list...
lineage program to Memheck-clean. The program is a simplified family tree manager. It maintains a list of Persons in a PersonList. Each Person object maintains a set of pointers to his/her parents as well as to his/her children. The code contains several memory leaks. To compile: please highlight the changes $g++ -g -O0 -fno-inline *.cpp -o lineage code linage.cpp // Adapted from http://people.cs.ksu.edu/~sherrill/labs/lab05/lineage.cpp #include "person.h" #include "personList.h" int main() {     PersonList theList;     theList.addPerson("Bob", "Mark", "Betty");     theList.addPerson("Jim", "Bob",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT